kprasanna_79 has asked for the wisdom of the Perl Monks concerning the following question:

Greatings Monk,
I found the below code somewhr in net.. and has a doubt, wat this $UNIVERSAL::AUTOLOAD and /::DESTROY/ will do. Can any one just give an brief intro abt this..which will be useful for me to proceed..
sub UNIVERSAL::AUTOLOAD { my $class = shift; my $method = $UNIVERSAL::AUTOLOAD; unless ($method =~ /::DESTROY$/) { ... } }
Thanks in Advance --prasanna.k

Replies are listed 'Best First'.
Re: wat ::DESTROY will do
by davido (Cardinal) on May 23, 2005 at 07:03 UTC

    It allows UNIVERSAL::AUTOLOAD to do something unless it's being invoked as a destructor.

    $UNIVERSAL::AUTOLOAD contains the name of the function or method name call that resulted in UNIVERSAL::AUTOLOAD() (the autoload function) being invoked. The programmer who wrote this sub decided he wanted UNIVERSAL::AUTOLOAD() to skip whatever it does if it is being invoked as a destructor.


    Dave

Re: wat ::DESTROY will do
by Thilosophy (Curate) on May 23, 2005 at 07:22 UTC
    AUTOLOAD is a subroutine with a special function: If you try to invoke a non-existing subroutine, you usually get an error. However, if the package has a subroutine called AUTOLOAD, the subroutine is invoked instead. The name of the subroutine that was requested is in the package global $AUTOLOAD.

    This AUTOLOAD subroutine can then do any number of useful things, such as creating the missing subroutine. This is a common pattern in OO Perl, where accessors for instance variables are created that way.

    When trying to find an AUTOLOAD, Perl starts to look in the package that contains the subroutine you asked for, but it also digs its way through @ISA. So, if you define AUTOLOAD in the UNIVERSAL package (the base class for all Perl objects), this AUTOLOAD gets applied for all packages (unless they have their own AUTOLOAD).

    One thing your AUTOLOAD probably does not want to handle is the DESTROY subroutine, which gets called as a destructor method when objects (blessed references) get garbage-collected. This is the reason why above code checks if the requested subroutine name is DESTROY. It will only do its magic for other subroutines.

    Check out perltoot for details on AUTOLOAD.

Re: what ::DESTROY will do
by bart (Canon) on May 23, 2005 at 07:35 UTC

    See perlobj for what AUTOLOAD is for:

    If neither the current class, its named base classes, nor the UNIVERSAL class contains the requested method, these three places are searched all over again, this time looking for a method named AUTOLOAD(). If an AUTOLOAD is found, this method is called on behalf of the missing method, setting the package global $AUTOLOAD to be the fully qualified name of the method that was intended to be called.
    This guy is trying to override AUTOLOAD for any class/object, and ignoring DESTROY method calls, which would be called whenever a object is destroyed, and no DESTROY method exists.

    It's usually considered bad to use AUTOLOAD and not have an explicit DESTROY method. Note that if you do, AUTOLOAD won't be called. It's faster, too.

Re: wat ::DESTROY will do
by brian_d_foy (Abbot) on May 23, 2005 at 07:43 UTC

    The AUTOLOAD subroutine does what the other posters said it will do, but by putting it in the UNIVERSAL package makes it available to every class which does not already have it own AUTOLOAD subroutine anywhere else in its @ISA tree.

    The code to skip the DESTROY method is a bit odd. If one is going to mess around with a UNIVERSAL::AUTOLOAD, one can also just create a UNIVERSAL::DESTROY that doesn't do anything.

    --
    brian d foy <brian@stonehenge.com>
      Your last sentence is interesting: do you mean this for performance reasons or just for added readability? Or (as it always is with my guesses) for something different from both?

      Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

      Don't fool yourself.

        Once perl finds a real method, it doesn't have to go back through @ISA looking for AUTOLOADs. Also, you don't have to make DESTROY a special case. Avoid special cases when possible.

        --
        brian d foy <brian@stonehenge.com>