Created
September 7, 2011 23:29
-
-
Save tene/1202136 to your computer and use it in GitHub Desktop.
find and print the source definition of a method
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env perl | |
use strict; | |
use warnings; | |
use B; | |
use IO::Socket; | |
use IO::Interface qw/:flags/; | |
use Data::Dumper; | |
sub find_sub_definition { | |
my ($sub) = @_; | |
my $cv = B::svref_2object($sub); | |
return ($cv->STASH->NAME, $cv->FILE, $cv->GV->LINE); | |
} | |
sub find_method_definition { | |
my ($obj, $name) = @_; | |
# using UNIVERSAL::can this way is slightly evil | |
my $meth = UNIVERSAL::can($obj, $name); | |
find_sub_definition($meth); | |
} | |
my $s = IO::Socket::INET->new(Proto => 'udp'); | |
my @info = find_method_definition($s, 'if_list'); | |
print Dumper(\@info); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment