开发者

Perl class variable usage

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:

${$dll_class->{MY_DLL}}->launch();

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:

# ...
my $self = {
    MY_DLL => $my_dll,  # note, the \ is no longer in front of $my_dll
};
# ...

and then you could call it with your original syntax:

$dll_class->{MY_DLL}->launch();

2) Perl will automatically clean up anything that has no references pointing to it. When your object is destroyed, \$my_dll will 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 DESTROY to 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.


There is no need to store a reference to $my_dll which itself is a reference returned by Win32::OLE.

Writing an accessor for it will make the syntax sweeter and allow you to change the internals of your class without changing code that uses it:

sub my_dll { $_[0]->{MY_DLL} }

Later:

$object->my_dll->launch;

or, if, as @rjh notes in the comments, you do not wish to expose the dll handle,

sub dll_launch {
   my $self = shift;
   $self->{MY_DLL}->launch();
   return;
}

and use

$object->dll_launch;

Further, note the following from Win32::OLE:

Please note the destructor specified on the Win32::OLE->new method. It ensures that Excel will shutdown properly even if the Perl program dies. Otherwise there could be a process leak if your application dies after having opened an OLE instance of Excel. It is the responsibility of the module user to make sure that all OLE objects are cleaned up properly!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜