Using Perl Class Variables

I am trying to understand how to use an instance variable in Perl OO - more specifically, in combination with external resources. Let me explain:

We have a DLL that provides some functionality that I would like to open through the Perl API. I am using Win32 :: OLE to access this dll. Therefore, 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 assign the instance variable MY_DLL a link to $my_dll. I have a couple of questions:

1) How do I call an instance variable since it points to a link. Thus, in other words, how can I call the methods for the dll created in this way as follows:

my $dll_class = new MY_CLASS;
$dll_class->{MY_DLL}->launch();

, start() - , DLL. {MY_DLL} , Perl , . ?

2) undef DESTROY? Perl, undef ?

+3
2

1) :

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

\$, . ${...} - .

, - MY_DLL $my_dll, :

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

:

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

2) Perl , . , \$my_dll , $my_dll, .

, , . DESTROY ; undef.

. perlref. , DESTROY . "" perlobj.

+9

$my_dll, , Win32::OLE.

, :

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

:

$object->my_dll->launch;

, , @rjh , dll,

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

$object->dll_launch;

, Win32::OLE:

, Win32::OLE->new. , Excel , Perl . , OLE Excel. , OLE- !

+2
source

Source: https://habr.com/ru/post/1735508/


All Articles