markniew2 has asked for the wisdom of the Perl Monks concerning the following question:
Oh monks, enlighten me. Why is sub AUTOLOAD called for each call to $object->SUPER::method(), even after the initial call to sub AUTOLOAD installs an appropriate sub within *{$AUTOLOAD}? This changed in perl 5.18 and baffles me.
pre 5.18: If you installed your own getter/setter then later calls skipped sub AUTOLOAD.
post 5.18 (to 5.25.5): sub AUTOLOAD is called for every $bar->SUPER::method() invocation, and warns about redefinition.
#!/usr/bin/perl use strict; use warnings; package Bar; our @ISA = qw(Foo); sub data { my $self = shift; return $self->SUPER::data(@_); } package Foo; our $AUTOLOAD; sub AUTOLOAD { my $self = shift; return if $AUTOLOAD =~ /^(.*::)?DESTROY$/; no strict 'refs'; my $old = defined *{$AUTOLOAD} ? *{$AUTOLOAD}{CODE}//'' : ''; my $force_new_code_ref = ''; *{$AUTOLOAD} = sub { warn "running sub $force_new_code_ref"; }; # usually a getter/setter, but simplified here. my $new = *{$AUTOLOAD}{CODE}; warn "installed sub AUTOLOAD=$AUTOLOAD new=$new old=$old"; &$new($self, @_); } package main; my $bar = bless {}, 'Bar'; $bar->data('baz'); $bar->data('bip'); $bar->data('bop'); $bar->data('boo');
Here are the results for select versions (5.14.4, 5.16.3, 5.18.4, 5.25.5)
$ perlbrew switch 5.14.4 $ perl ./doit.pl installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x25e5d48) old= at ./ +doit.pl line 29. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. $ perlbrew switch 5.16.3 $ perl ./doit.pl # v5.16.3 installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1f32970) old= at ./ +doit.pl line 29. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. running sub at ./doit.pl line 25. $ perlbrew switch 5.18.4 $ perl ./doit.pl installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x16138e8) old= at ./ +doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1613858) old=CODE(0 +x16138e8) at ./doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1613768) old=CODE(0 +x1613858) at ./doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1613888) old=CODE(0 +x1613768) at ./doit.pl line 29. running sub at ./doit.pl line 25. $ perlbrew switch perl-blead $ perl -v | grep built This is perl 5, version 25, subversion 5 (v5.25.5 (v5.25.4-61-g483efd0 +)) built for x86_64-linux $ perl ./doit.pl installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1aeedc8) old= at ./ +doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1aeed38) old=CODE(0 +x1aeedc8) at ./doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1aeec48) old=CODE(0 +x1aeed38) at ./doit.pl line 29. running sub at ./doit.pl line 25. Subroutine Bar::SUPER::data redefined at ./doit.pl line 25. installed sub AUTOLOAD=Bar::SUPER::data new=CODE(0x1aeed68) old=CODE(0 +x1aeec48) at ./doit.pl line 29. running sub at ./doit.pl line 25.
Adding no warnings 'redefine'; within sub AUTOLOAD bothers me. Creating a sub on every getter/setter invocation seems far from ideal.
I could avoid creating the subs by doing so only if ( ( not defined *{$AUTOLOAD} ) && ( not defined *{$AUTOLOAD}{CODE} ) ), but isn't that exactly what perl itself should be checking before calling sub AUTOLOAD? If a method exists there, why is sub AUTOLOAD being called?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: AUTOLOAD called on every $object->SUPER::method()
by Anonymous Monk on Sep 02, 2016 at 03:43 UTC | |
by markniew2 (Novice) on Sep 02, 2016 at 04:09 UTC |