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?


In reply to AUTOLOAD called on every $object->SUPER::method() by markniew2

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.