in reply to Re: AUTOLOAD & mod_perl memory
in thread AUTOLOAD & mod_perl memory

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.

Replies are listed 'Best First'.
Re^3: AUTOLOAD & mod_perl memory
by adrianh (Chancellor) on Dec 04, 2002 at 00:54 UTC
    $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.

    "Doctor, it hurts when I do this" :-)

    Put it in the base class - this should do what you want:

    sub AUTOLOAD { my ($fn) = ($AUTOLOAD =~ /^.*::(.+)$/); return if $fn eq 'DESTROY'; *{$fn} = sub { print "$AUTOLOAD $fn\n"; }; goto &$fn; }
      Yep. Noted in the original post, 2nd para.

      perrin's reply made the path clear. Your AUTOLOAD does address pike's problem and is properly inheritable.

      *{$base."::".$fn} = sub { print "$AUTOLOAD $fn\n"; };
Re: Re: Re: AUTOLOAD & mod_perl memory
by pike (Monk) on Dec 04, 2002 at 20:27 UTC
    OK, I have to admit that I checked only part of the story, and it is trickier than I thought. The point is that the full name of the variable is $base::AUTOLOAD, but its contents is "sub::function", not "base::function", as I thought.

    My apologies,

    pike