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?
1) Dereference the reference before calling it:
By using
\$, you are taking a scalar reference.${...}is the scalar dereference operator.I’m unsure why you need to use a reference to a reference at all – you could simply set MY_DLL to
$my_dll, as it is already a reference:and then you could call it with your original syntax:
2) Perl will automatically clean up anything that has no references pointing to it. When your object is destroyed,
\$my_dllwill no longer have references to it, and therefore neither will$my_dll, so it will be destroyed automatically.In general you only need to worry if you have recursive data structures that point to themselves. In those cases you use
DESTROYto break the links manually; in your case, you do not need to explicitly set the handle to undef.For more information on references, see perlref. For information on garbage collecting, the DESTROY method and circular references, see the “Destructors” section of perlobj.