| File | /usr/local/lib/perl5/site_perl/5.10.1/darwin-2level/Sub/Identify.pm |
| Statements Executed | 112 |
| Statement Execution Time | 2.27ms |
| Calls | P | F | Exclusive Time |
Inclusive Time |
Subroutine |
|---|---|---|---|---|---|
| 90 | 1 | 1 | 1.56ms | 1.64ms | Sub::Identify::sub_fullname |
| 90 | 1 | 2 | 82µs | 82µs | Sub::Identify::get_code_info (xsub) |
| 1 | 1 | 1 | 23µs | 194µs | Sub::Identify::BEGIN@6 |
| 1 | 1 | 1 | 14µs | 18µs | Sub::Identify::BEGIN@3 |
| 1 | 1 | 1 | 8µs | 26µs | Sub::Identify::BEGIN@4 |
| 0 | 0 | 0 | 0s | 0s | Sub::Identify::__ANON__[:44] |
| 0 | 0 | 0 | 0s | 0s | Sub::Identify::stash_name |
| 0 | 0 | 0 | 0s | 0s | Sub::Identify::sub_name |
| Line | State ments |
Time on line |
Calls | Time in subs |
Code |
|---|---|---|---|---|---|
| 1 | package Sub::Identify; | ||||
| 2 | |||||
| 3 | 3 | 22µs | 2 | 21µs | # spent 18µs (14+3) within Sub::Identify::BEGIN@3 which was called
# once (14µs+3µs) by namespace::clean::BEGIN@15 at line 3 # spent 18µs making 1 call to Sub::Identify::BEGIN@3
# spent 3µs making 1 call to strict::import |
| 4 | 3 | 243µs | 2 | 44µs | # spent 26µs (8+18) within Sub::Identify::BEGIN@4 which was called
# once (8µs+18µs) by namespace::clean::BEGIN@15 at line 4 # spent 26µs making 1 call to Sub::Identify::BEGIN@4
# spent 18µs making 1 call to Exporter::import |
| 5 | |||||
| 6 | # spent 194µs (23+170) within Sub::Identify::BEGIN@6 which was called
# once (23µs+170µs) by namespace::clean::BEGIN@15 at line 46 | ||||
| 7 | 7 | 14µs | our $VERSION = '0.04'; | ||
| 8 | our @ISA = ('Exporter'); | ||||
| 9 | our %EXPORT_TAGS = (all => [ our @EXPORT_OK = qw(sub_name stash_name sub_fullname get_code_info) ]); | ||||
| 10 | |||||
| 11 | my $loaded = 0; | ||||
| 12 | 4 | 1µs | unless ($ENV{PERL_SUB_IDENTIFY_PP}) { | ||
| 13 | local $@; | ||||
| 14 | 1 | 900ns | eval { | ||
| 15 | 2 | 176µs | if ($] >= 5.006) { | ||
| 16 | require XSLoader; | ||||
| 17 | XSLoader::load(__PACKAGE__, $VERSION); # spent 170µs making 1 call to XSLoader::load | ||||
| 18 | } | ||||
| 19 | else { | ||||
| 20 | require DynaLoader; | ||||
| 21 | push @ISA, 'DynaLoader'; | ||||
| 22 | __PACKAGE__->bootstrap($VERSION); | ||||
| 23 | } | ||||
| 24 | }; | ||||
| 25 | |||||
| 26 | die $@ if $@ && $@ !~ /object version|loadable object/; | ||||
| 27 | |||||
| 28 | $loaded = 1 unless $@; | ||||
| 29 | } | ||||
| 30 | |||||
| 31 | our $IsPurePerl = !$loaded; | ||||
| 32 | |||||
| 33 | if ($IsPurePerl) { | ||||
| 34 | require B; | ||||
| 35 | *get_code_info = sub ($) { | ||||
| 36 | my ($coderef) = @_; | ||||
| 37 | ref $coderef or return; | ||||
| 38 | my $cv = B::svref_2object($coderef); | ||||
| 39 | $cv->isa('B::CV') or return; | ||||
| 40 | # bail out if GV is undefined | ||||
| 41 | $cv->GV->isa('B::SPECIAL') and return; | ||||
| 42 | |||||
| 43 | return ($cv->GV->STASH->NAME, $cv->GV->NAME); | ||||
| 44 | }; | ||||
| 45 | } | ||||
| 46 | 1 | 108µs | 1 | 194µs | } # spent 194µs making 1 call to Sub::Identify::BEGIN@6 |
| 47 | |||||
| 48 | sub stash_name ($) { (get_code_info($_[0]))[0] } | ||||
| 49 | sub sub_name ($) { (get_code_info($_[0]))[1] } | ||||
| 50 | 90 | 1.70ms | 90 | 82µs | # spent 1.64ms (1.56+82µs) within Sub::Identify::sub_fullname which was called 90 times, avg 18µs/call:
# 90 times (1.56ms+82µs) by namespace::clean::__ANON__[/usr/local/lib/perl5/site_perl/5.10.1/namespace/clean.pm:211] at line 181 of namespace/clean.pm, avg 18µs/call # spent 82µs making 90 calls to Sub::Identify::get_code_info, avg 909ns/call |
| 51 | |||||
| 52 | 1 | 2µs | 1; | ||
| 53 | |||||
| 54 | __END__ | ||||
| 55 | |||||
| 56 | =head1 NAME | ||||
| 57 | |||||
| 58 | Sub::Identify - Retrieve names of code references | ||||
| 59 | |||||
| 60 | =head1 SYNOPSIS | ||||
| 61 | |||||
| 62 | use Sub::Identify ':all'; | ||||
| 63 | my $subname = sub_name( $some_coderef ); | ||||
| 64 | my $p = stash_name( $some_coderef ); | ||||
| 65 | my $fully_qualified_name = sub_fullname( $some_coderef ); | ||||
| 66 | defined $subname | ||||
| 67 | and print "this coderef points to sub $subname in package $p\n"; | ||||
| 68 | |||||
| 69 | =head1 DESCRIPTION | ||||
| 70 | |||||
| 71 | C<Sub::Identify> allows you to retrieve the real name of code references. For | ||||
| 72 | this, it uses perl's introspection mechanism, provided by the C<B> module. | ||||
| 73 | |||||
| 74 | It provides four functions : C<sub_name> returns the name of the | ||||
| 75 | subroutine (or C<__ANON__> if it's an anonymous code reference), | ||||
| 76 | C<stash_name> returns its package, and C<sub_fullname> returns the | ||||
| 77 | concatenation of the two. | ||||
| 78 | |||||
| 79 | The fourth function, C<get_code_info>, returns a list of two elements, | ||||
| 80 | the package and the subroutine name (in case of you want both and are worried | ||||
| 81 | by the speed.) | ||||
| 82 | |||||
| 83 | In case of subroutine aliasing, those functions always return the | ||||
| 84 | original name. | ||||
| 85 | |||||
| 86 | =head1 LICENSE | ||||
| 87 | |||||
| 88 | (c) Rafael Garcia-Suarez (rgarciasuarez at gmail dot com) 2005, 2008 | ||||
| 89 | |||||
| 90 | This program is free software; you may redistribute it and/or modify it under | ||||
| 91 | the same terms as Perl itself. | ||||
| 92 | |||||
| 93 | =cut | ||||
# spent 82µs within Sub::Identify::get_code_info which was called 90 times, avg 909ns/call:
# 90 times (82µs+0s) by Sub::Identify::sub_fullname at line 50 of Sub/Identify.pm, avg 909ns/call |