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

First, from perlsub
...if an AUTOLOAD subroutine is defined in the package or packages used to locate the original subroutine, then that AUTOLOAD subroutine is called with the arguments that would have been passed to the original subroutine. The fully qualified name of the original subroutine magically appears in the global $AUTOLOAD variable of the same package as the AUTOLOAD routine
My question has to do with the "magically appears in the global $AUTOLOAD variable of the same package as the AUTOLOAD routine" section.

If $AUTOLOAD is initialized by perl as a global in the package the routine is in, why must it be fully qualified or explicitly declared with our when used within that package when strict is in use.

# the following code throws a compile time error package TestPackage; use strict; sub AUTOLOAD { print $AUTOLOAD, " is not available\n"; } # this code is fine package TestPackage; use strict; our $AUTOLOAD; # 'use vars qw( $AUTOLOAD )' works too sub AUTOLOAD { print $AUTOLOAD, " is not available\n"; }
This error:
Global symbol "$AUTOLOAD" requires explicit package name
seems to make a liar out of perlsub. I get from the docs that $AUTOLOAD will be set to be a part of the package the routine is in, but it doesn't seem to behave that way.

I'm sure I've misinterpreted, but I just don't see where.




Amel

Replies are listed 'Best First'.
Re: Fully qualifying TT$AUTOLOAD/TT
by demerphq (Chancellor) on May 02, 2002 at 15:53 UTC
    This is a very common misunderstanding about what 'our' does, so common that a number of senior monks (tilly for one) have written huge posts (sorry no link :-) about it.

    Basically all our does is tell the compiler that the variable you are using is _not_ a typo when you are running under strict. It doesnt declare it (or initialize it unless you do so specifically) in the sense of a my or anything like that, just marks it as being ok to use. Yes it is a bit counter intuitive. and Yes sometimes you have to our a vairable a number of times, particularly if the statements are inside a scoped block.

    If it seems confusing just use the use vars pragma:

    use vars qw/$AUTOLOAD/;
    and itll be declared usable for the whole module and at compile time too.

    Hope that helps.

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

(crazyinsomniac: qualify it) Re: Fully qualifying $AUTOLOAD
by crazyinsomniac (Prior) on May 03, 2002 at 07:59 UTC
    please read perldata.
    Global symbol "$AUTOLOAD" requires explicit package name seems to make a liar out of perlsub. I get from the docs that $AUTOLOAD will be set to be a part of the package the routine is in, but it doesn't seem to behave that way.
    $AUTOLOAD is not a fully qualified and does not have an explicit package name.
    perlsub is not a liar.
    $PACKAGE::AUTOLOAD is fully qualified.
    $main::AUTOLOAD is fully qualified.
    $AUTOLOAD is not.
    Try it out.
    perl -MData::Dumper -e"package FOXY;$FOXY;print Dumper \%FOXY::" perl -MData::Dumper -we"package FOXY;$FOXY;print Dumper \%FOXY::" perl -MData::Dumper -e"package FOXY;my $FOXY;print Dumper \%FOXY::" perl -MData::Dumper -we"package FOXY;my $FOXY;print Dumper \%FOXY::" perl -MData::Dumper -e"package FOXY;$FOXY::FOXY;print Dumper \%FOXY::" perl -MData::Dumper -we"package FOXY;$FOXY::FOXY;print Dumper \%FOXY:: +" perl -MData::Dumper -e"package FOXY;our $FOXY;print Dumper \%FOXY::" perl -MData::Dumper -we"package FOXY;our $FOXY;print Dumper \%FOXY::"

     
    ______crazyinsomniac_____________________________
    Of all the things I've lost, I miss my mind the most.
    perl -e "$q=$_;map({chr unpack qq;H*;,$_}split(q;;,q*H*));print;$q/$q;"

Re: Fully qualifying $AUTOLOAD
by broquaint (Abbot) on May 02, 2002 at 15:58 UTC
    I'm hazarding an educated guess that $AUTOLOAD 'magically appears' at run-time. So when perl is compiling your code, it doesn't know where $AUTOLOAD is coming from and moans appropriately. This is why you have to declare it before hand so as to not make perl worry unnecessarily.
    HTH

    _________
    broquaint

Re: Fully qualifying $AUTOLOAD
by rinceWind (Monsignor) on May 02, 2002 at 16:04 UTC
    $AUTOLOAD is a package namespace variable and can be declared as such in the following way:
    use vars qw($AUTOLOAD);
    hth

    --rW

    Update: demerphq has posted a more complete answer. and recalled the caveat with using our.