[Kc] curious $AUTOLOAD package binding

David Nicol davidnicol at gmail.com
Fri Jul 16 08:31:55 PDT 2010


with perl v5.10.1 on cygwin:

Documentation about the $AUTOLOAD variable states that it is a package
variable in the same package where the AUTOLOAD routine is found. This
is not always true. Apparently when an AUTOLOAD routine is added by
assignment to glob instead of by declaration, $AUTOLOAD within it is
bound to the package in effect at the assignment.


$ perl -le 'sub z::AUTOLOAD { warn "called $z::AUTOLOAD"} @x::ISA =
qw/z/;x->abc'
called x::abc at -e line 1.

$ perl -le '*z::AUTOLOAD =sub{ warn "called $z::AUTOLOAD"}; @x::ISA =
qw/z/;x->abc'
called  at -e line 1.

$ perl -le '*z::AUTOLOAD =sub{ warn "called $main::AUTOLOAD"}; @x::ISA
= qw/z/;x->abc'
called x::abc at -e line 1.

__PACKAGE__ doesn't get changed when a subroutine is declared normally though

$ perl -le '*z::AUTOLOAD =sub{ warn __PACKAGE__}; @x::ISA = qw/z/;x->abc'
main at -e line 1.

$ perl -le 'sub z::AUTOLOAD { warn __PACKAGE__}; @x::ISA = qw/z/;x->abc'
main at -e line 1.

$ perl -le 'sub z::AUTOLOAD { eval "warn __PACKAGE__"}; @x::ISA = qw/z/;x->abc'
main at (eval 1) line 1.


I don't know if this is a bug and if so in what.

Furthermore,

$ perl -le '*z::AUTOLOAD =sub{ package z; warn "called $AUTOLOAD"};
@x::ISA = qw/z/;x->abc'
called  at -e line 1.

$ perl -le 'sub z::AUTOLOAD { package z; warn "called $AUTOLOAD"};
@x::ISA = qw/z/;x->abc'
called x::abc at -e line 1.

$ perl -le '*z::AUTOLOAD =sub{  warn "called $AUTOLOAD"}; @x::ISA =
qw/z/;x->abc'
called x::abc at -e line 1.

$ perl -le 'sub z::AUTOLOAD { warn "called $AUTOLOAD"}; @x::ISA = qw/z/;x->abc'
called  at -e line 1.

It appears that what package $AUTOLOAD binds to is affected by the
subroutine declaration, while the resolution of $AUTOLOAD is governed
by normal scoping rules.

The documentation seems to say that the package's $AUTOLOAD that gets
the goods is the package where the AUTOLOAD routine was found, but the
above examples where AUTOLOAD is explicitly assigned to a glob instead
of getting declared indicate otherwise.





Thoughts? Should I try to escalate this?


More information about the kc mailing list