Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 650273
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T22:02:06+00:00 2026-05-13T22:02:06+00:00

I am trying to understand how to use instance variable in Perl OO –

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-13T22:02:06+00:00Added an answer on May 13, 2026 at 10:02 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have recently started to use Twitter Bootstrap and trying to understand how it
I have been trying to understand the use of primitives in Java and C#
I'm learning Python and have been trying to understand more about the details of
I'm trying to understand how strategies some folks use to distinguish instance vars vs.
I am learning Django and I am trying to understand the use of models.py
I'm trying to understand how to use firebug to debug my Javascript. So I
I am trying to understand how to use reference parameters. There are several examples
I'm trying to understand why one would use Spring Batch over a scripting language
I'm trying to understand whether and under what circs one should use Python classes
I am trying to understand my embedded Linux application's memory use. The /proc/pid/maps utility/file

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.