I think that it is really a question of context. The built in variables that ambrus lists from perlvar are independent of which package namespace you are in. This is not the case with $AUTOLOAD. Conceivably there could be different $AUTOLOAD subs nested, and in different namespaces if one AUTOLOAD sub causes another one to be called (if it calls a non-existent sub elsewhere). Similarly, @ISA lives in package namespace and needs declaring with use vars or our.

Specifically, the $Foo::Bar::AUTOLOAD symbol table entry does not exist except when AUTOLOAD has been invoked (but &Foo::Bar::AUTOLOAD is the subroutine). The scalar is set up with an implicit local declaration, hence the entry is removed when you return out of the call frame.

In theory you could have nested calls to the same AUTOLOAD if the sub is itself invoking non-existent subs in the same namespace. But this is a very bizarre way of doing things.


Something the OP did not mention is that code to autogenerate the subroutine usually needs to turn off strict 'refs' in order to put the new sub into package name space. For example: (warning: untested)

package Foo::Bar; use strict; use vars qw/$AUTOLOAD/; sub AUTOLOAD { my ($self) = @_; #Don't use shift as we need to preserve @_ my ($meth) = $AUTOLOAD =~ /(\w+)$/; # Simple example: getter and setter for hash members # Note that the eval is here to stop the sub becoming a closure on + $meth my $acc = eval qq( sub { my \$self = shift; \@_ ? \$self->{$meth}=shift : \$self->{$meth}; } ); { no strict 'refs'; *$AUTOLOAD = $acc; } goto $acc; }

--
I'm Not Just Another Perl Hacker


In reply to Re: Use of $AUTOLOAD and strict by rinceWind
in thread Use of $AUTOLOAD and strict by jira0004

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.