in reply to Re: Detecting which class a method is defined in
in thread Detecting which class a method is defined in

The stash name is the name of the current package in which perl compiled the method. It can be wrong in cases where a method in a class comes from elsewhere, such as a role or a mixin.

  • Comment on Re^2: Detecting which class a method is defined in

Replies are listed 'Best First'.
Re^3: Detecting which class a method is defined in
by educated_foo (Vicar) on May 05, 2011 at 21:34 UTC
    No, this is all compiled in main:
    #!perl -l $y='main';$A::y='A';$C::y='C'; sub A::foo { $y } sub A::bar { $y } @C::ISA = 'A'; sub C::bar { $y } use B 'svref_2object'; $x = bless {}, 'C'; for (qw(foo bar)) { print "$_ -> ", $x->$_, " from ", svref_2object(UNIVERSAL::can($x, + $_))->GV->STASH->NAME; }
    I guess the role and mixin stuff on CPAN must do something else if it breaks this.

      I mean this behavior:

      #!/usr/bin/perl -l $y='main';$A::y='A';$C::y='C'; package main; sub foo { $y } sub bar { $y } package A; *foo = \&main::foo; *bar = \&main::bar; package C; @C::ISA = 'A'; *bar = \&main::bar; package main; use B 'svref_2object'; my $x = bless {}, 'C'; for (qw(foo bar)) { print "$_ -> ", $x->$_, " from ", svref_2object(UNIVERSAL::can($x, + $_))->GV->STASH->NAME; }

      If you know the name of the intended destination stash at the time of writing the code, your example works fine. It's not always possible to know that, unfortunately.