File Coverage

blib/lib/Sub/Identify.pm
Criterion Covered Total %
statement 44 45 97.7
branch 17 26 65.3
condition 6 12 50.0
subroutine 9 9 100.0
pod 0 3 0.0
total 76 95 80.0


line stmt bran cond sub pod time code
1             package Sub::Identify;
2              
3 13     13   185453 use strict;
  13         24  
  13         343  
4 13     13   50 use Exporter;
  13         15  
  13         5317  
5              
6             BEGIN {
7 13     13   36 our $VERSION = '0.13';
8 13         92 our @ISA = ('Exporter');
9 13         58 our %EXPORT_TAGS = (
10             all => [
11             our @EXPORT_OK = qw(
12             sub_name
13             stash_name
14             sub_fullname
15             get_code_info
16             get_code_location
17             is_sub_constant
18             )
19             ]
20             );
21              
22 13         17 our $IsPurePerl = 1;
23 13 100       52 unless ($ENV{PERL_SUB_IDENTIFY_PP}) {
24 8 50       10 if (
25             eval {
26 8         32 require XSLoader;
27 8         2851 XSLoader::load(__PACKAGE__, $VERSION);
28 8         43 1;
29             }
30             ) {
31 8         9 $IsPurePerl = 0;
32             }
33             else {
34 0 0 0     0 die $@ if $@ && $@ !~ /object version|loadable object/;
35             }
36             }
37              
38 13 100       35 if ($IsPurePerl) {
39 5         24 require B;
40             *get_code_info = sub ($) {
41 32     32   35 my ($coderef) = @_;
42 32 50       61 ref $coderef or return;
43 32         85 my $cv = B::svref_2object($coderef);
44 32 50       127 $cv->isa('B::CV') or return;
45             # bail out if GV is undefined
46 32 50       132 $cv->GV->isa('B::SPECIAL') and return;
47              
48 32         248 return ($cv->GV->STASH->NAME, $cv->GV->NAME);
49 5         19 };
50             *get_code_location = sub ($) {
51 4     4   1549 my ($coderef) = @_;
52 4 50       14 ref $coderef or return;
53 4         13 my $cv = B::svref_2object($coderef);
54 4 100 66     95 $cv->isa('B::CV') && $cv->START->isa('B::COP')
55             or return;
56              
57 2         16 return ($cv->START->file, $cv->START->line);
58 5         18 };
59             }
60 13 100 66     800 if ($IsPurePerl || $] < 5.016) {
61 5         19 require B;
62             *is_sub_constant = sub ($) {
63 5     5   130 my ($coderef) = @_;
64 5 50       11 ref $coderef or return 0;
65 5         12 my $cv = B::svref_2object($coderef);
66 5 50       30 $cv->isa('B::CV') or return 0;
67 5         6 my $p = prototype $coderef;
68 5 100 66     20 defined $p && $p eq "" or return 0;
69 3         18 return ($cv->CvFLAGS & B::CVf_CONST()) == B::CVf_CONST();
70 5         391 };
71             }
72             }
73              
74 22     22 0 98 sub stash_name ($) { (get_code_info($_[0]))[0] }
75 28     28 0 1803 sub sub_name ($) { (get_code_info($_[0]))[1] }
76 30     30 0 3776 sub sub_fullname ($) { join '::', get_code_info($_[0]) }
77              
78             1;
79              
80             __END__