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   187318 use strict;
  13         22  
  13         323  
4 13     13   41 use Exporter;
  13         17  
  13         5236  
5              
6             BEGIN {
7 13     13   36 our $VERSION = '0.14';
8 13         106 our @ISA = ('Exporter');
9 13         56 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         21 our $IsPurePerl = 1;
23 13 100       50 unless ($ENV{PERL_SUB_IDENTIFY_PP}) {
24 8 50       12 if (
25             eval {
26 8         34 require XSLoader;
27 8         3046 XSLoader::load(__PACKAGE__, $VERSION);
28 8         46 1;
29             }
30             ) {
31 8         14 $IsPurePerl = 0;
32             }
33             else {
34 0 0 0     0 die $@ if $@ && $@ !~ /object version|loadable object/;
35             }
36             }
37              
38 13 100       40 if ($IsPurePerl) {
39 5         24 require B;
40             *get_code_info = sub ($) {
41 32     32   34 my ($coderef) = @_;
42 32 50       61 ref $coderef or return;
43 32         69 my $cv = B::svref_2object($coderef);
44 32 50       114 $cv->isa('B::CV') or return;
45             # bail out if GV is undefined
46 32 50       130 $cv->GV->isa('B::SPECIAL') and return;
47              
48 32         220 return ($cv->GV->STASH->NAME, $cv->GV->NAME);
49 5         22 };
50             *get_code_location = sub ($) {
51 4     4   1981 my ($coderef) = @_;
52 4 50       15 ref $coderef or return;
53 4         15 my $cv = B::svref_2object($coderef);
54 4 100 66     99 $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         16 };
59             }
60 13 100 66     880 if ($IsPurePerl || $] < 5.016) {
61 5         20 require B;
62             *is_sub_constant = sub ($) {
63 5     5   141 my ($coderef) = @_;
64 5 50       13 ref $coderef or return 0;
65 5         13 my $cv = B::svref_2object($coderef);
66 5 50       38 $cv->isa('B::CV') or return 0;
67 5         8 my $p = prototype $coderef;
68 5 100 66     22 defined $p && $p eq "" or return 0;
69 3         19 return ($cv->CvFLAGS & B::CVf_CONST()) == B::CVf_CONST();
70 5         419 };
71             }
72             }
73              
74 22     22 0 94 sub stash_name ($) { (get_code_info($_[0]))[0] }
75 28     28 0 1958 sub sub_name ($) { (get_code_info($_[0]))[1] }
76 30     30 0 4088 sub sub_fullname ($) { join '::', get_code_info($_[0]) }
77              
78             1;
79              
80             __END__