I am trying to understand how to use instance variable in Perl OO - more specifically in conjunction with external resources. Let me explain:
We have a DLL that exposes some functionality that I'd like to expose through a Perl API. I use Win32::OLE to get access to this DLL. So my constructor is simple:
package MY_CLASS;
use Win32::OLE;
sub new
{
my ($class) = @_;
# instantiate the dll control
my $my_dll = Win32::OLE->new("MY_DLL.Control");
my $self = {
MY_DLL => \$my_dll,
};
bless $self, $class or die "Can't bless $!";
return $self;
}
sub DESTROY
{
my ($self) = shift;
undef $sef->{MY_DLL};
}
As you can see, I am assigning the instance variable MY_DLL the reference to $my_dll
. I have couple of questions:
1)How do I call the instance variable, since it is points to a reference. So, in other words how can I invoke methods on the instantiated dll like this:
my $dll_class = new MY_CLASS;
$dll_class->{MY_DLL}->launch();
assuming launch() is a method exposed by the dll. But since {MY_DLL} points to a reference, Perl complains which is understandable. What is the syntax?
2)Do I need to specifically undef
in DESTROY? That is will Perl automatically clean up even if I don't specifically undef
it?