Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Find out who redefined a subroutine

by Dallaylaen (Friar)
on Jan 25, 2013 at 15:18 UTC ( #1015363=perlmeditation: print w/replies, xml ) Need Help??

I've been asked recently how to tell which perl module overrides a function. Here's an exapmle I managed to write. It's incomplete and most certainly buggy, but I don't think the task in question is worth a full-blown CPAN module anyway.
#!/usr/bin/perl -w

use strict;

BEGIN {
use Guard;
use Carp;
{
  my $guard = guard { Carp::cluck "Function replaced!" };
  my $code = \&time;
  *time = sub { return $code->(@_); undef $guard; };
};
};

BEGIN { print time(), "\n"; };

use Time::HiRes qw(time);

print time, "\n";
I welcome critique on this one.

Replies are listed 'Best First'.
Re: Find out who redefined a subroutine
by tobyink (Abbot) on Jan 25, 2013 at 15:33 UTC

    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
      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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlmeditation [id://1015363]
Front-paged by Arunbear
help
Chatterbox?
[marto]: "Eugenie Scott, executive director of the National Center for Science Education, dubbed this approach the Gish gallop, describing it as "where the creationist is allowed to run on for 45 minutes or an hour, spewing forth torrents of error that the
[marto]: evolutionist hasn't a prayer of refuting in the format of a debate." She also criticized Gish for failing to answer objections raised by his opponents"
[erix]: one would hope evolutionists haven't any prayers anyway
[marto]: obviously someone could be religious, but not creationist
[erix]: "Nothing in Intelligent Design makes sense except in the light of Creationism" <-- I made that one up myself (free after Dobzhansky )
[erix]: yes. Deplorable marto, deplorable.
[marto]: the situation seemed similar to this one, majority of the contributrions are nonsense, doesn't address any questions ...
[marto]: meh, I've been called worse :P

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (9)
As of 2017-07-28 15:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I came, I saw, I ...
























    Results (431 votes). Check out past polls.