Skip to content

Instantly share code, notes, and snippets.

@tene
Created September 7, 2011 23:29
Show Gist options
  • Save tene/1202136 to your computer and use it in GitHub Desktop.
Save tene/1202136 to your computer and use it in GitHub Desktop.
find and print the source definition of a method
#!/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