I guess it wasn't clear in my initial example code that I'm creating a new closure for each attribute not just aliasing the same sub.

$subclass->new_meth will set $AUTOLOAD to "SUBCLASS::new_meth" so will install the closure there. If later I call $other_subclass->new_meth then I'll get another sub in OTHERSUBCLASS::new_meth.

package A; sub AUTOLOAD { my ($fn) = $AUTOLOAD =~ /^.*::(.+)$/; return if $fn eq 'DESTROY'; *{$AUTOLOAD} = sub { print "$AUTOLOAD $fn"; }; goto &$AUTOLOAD; } package B; @ISA = "A"; package C; @ISA = "A"; package main; $a = bless {}, 'A'; $b = bless {}, 'B'; $c = bless {}, 'C'; $a->shared; # call via baseclass first $b->shared; $c->shared; $b->not_shared; # call via one subclas $c->not_shared; # then another $a->not_shared; # then base class print "\nshared CODE refs:"; print $a->can('shared'); print $b->can('shared'); print $c->can('shared'); print "\nnot_shared CODE refs:"; print $a->can('not_shared'); print $b->can('not_shared'); print $c->can('not_shared');

Produces output:

 perl -wl eg
A::shared shared
A::shared shared
A::shared shared
B::not_shared not_shared
C::not_shared not_shared
A::not_shared not_shared

shared CODE refs:
CODE(0x8105cb8)
CODE(0x8105cb8)
CODE(0x8105cb8)

not_shared CODE refs:
CODE(0x8105eb0)
CODE(0x8105db4)
CODE(0x8105e50)

So in the second case (which correspond to my usage) you get same closure created and installed in 3 places.


In reply to Re: Re: AUTOLOAD & mod_perl memory by bsb
in thread AUTOLOAD & mod_perl memory by bsb

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.