Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: Find out who redefined a subroutine

by tobyink (Abbot)
on Jan 25, 2013 at 15:33 UTC ( #1015364=note: print w/replies, xml ) Need Help??

in reply to Find out who redefined a subroutine

Ta da!

sub sub::source { require Sub::Identify; my ($class, $func) = @_; my $code = $class->can($func) or return; return Sub::Identify::stash_name($code); } { package MyApp; use Time::HiRes qw(time); use Carp; } # Where do MyApp's subs come from? # print MyApp->sub::source('carp'), "\n"; print MyApp->sub::source('time'), "\n"; # Where do Carp's subs come from? # print Carp->sub::source('carp'), "\n"; print Carp->sub::source('import'), "\n";

Though it's worth noting that Sub::Name is able to fool Sub::Identify. Also, Moose/Moo/etc do stringy eval stuff that fools Sub::Identify (deliberately).

package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Replies are listed 'Best First'.
Re^2: Find out who redefined a subroutine
by Dallaylaen (Hermit) on Jan 25, 2013 at 15:45 UTC
    Aha, so there *is* a full-blown CPAN module for this. Thanks for noting!

    My code can be fooled, too.

    Just copy original subref to a private variable and it would think the sub is still the original one.

      Maybe a way to prevent being fooled is to MD5 the sub, and sign it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1015364]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (6)
As of 2018-06-21 09:09 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (117 votes). Check out past polls.