line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
3
|
|
|
3
|
|
214340
|
use 5.006; # our |
|
3
|
|
|
|
|
45
|
|
2
|
3
|
|
|
3
|
|
22
|
use strict; |
|
3
|
|
|
|
|
6
|
|
|
3
|
|
|
|
|
82
|
|
3
|
3
|
|
|
3
|
|
16
|
use warnings; |
|
3
|
|
|
|
|
18
|
|
|
3
|
|
|
|
|
239
|
|
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
package PPIx::DocumentName; |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
# ABSTRACT: Utility to extract a name from a PPI Document |
8
|
|
|
|
|
|
|
our $VERSION = '1.00_01'; # TRIAL VERSION |
9
|
|
|
|
|
|
|
$VERSION = eval $VERSION; ## no critic (BuiltinFunctions::ProhibitStringyEval) |
10
|
|
|
|
|
|
|
|
11
|
3
|
|
|
3
|
|
1498
|
use PPI::Util qw( _Document ); |
|
3
|
|
|
|
|
24218
|
|
|
3
|
|
|
|
|
637
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub log_info(&@); |
15
|
|
|
|
|
|
|
sub log_debug(&@); |
16
|
|
|
|
|
|
|
sub log_trace(&@); |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
my %callers; |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
BEGIN { |
21
|
3
|
50
|
|
3
|
|
19
|
if ( $INC{'Log/Contextual.pm'} ) { |
22
|
|
|
|
|
|
|
## Hide from autoprereqs |
23
|
0
|
|
|
|
|
0
|
require 'Log/Contextual/WarnLogger.pm'; ## no critic (Modules::RequireBarewordIncludes) |
24
|
0
|
|
|
|
|
0
|
my $deflogger = Log::Contextual::WarnLogger->new( { env_prefix => 'PPIX_DOCUMENTNAME', } ); |
25
|
0
|
|
|
|
|
0
|
Log::Contextual->import( 'log_info', 'log_debug', 'log_trace', '-default_logger' => $deflogger ); |
26
|
|
|
|
|
|
|
} |
27
|
|
|
|
|
|
|
else { |
28
|
3
|
|
|
|
|
27
|
require Carp; |
29
|
3
|
|
|
0
|
|
21
|
*log_info = sub (&@) { Carp::carp( $_[0]->() ) }; |
|
0
|
|
|
|
|
0
|
|
30
|
3
|
|
|
26
|
|
16
|
*log_debug = sub (&@) { }; |
31
|
3
|
|
|
8
|
|
2697
|
*log_trace = sub (&@) { }; |
32
|
|
|
|
|
|
|
} |
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
sub import { |
36
|
4
|
|
|
4
|
|
1244
|
my(undef, %args) = @_; |
37
|
4
|
100
|
|
|
|
1567
|
if(defined $args{'-api'}) { |
38
|
2
|
100
|
66
|
|
|
54
|
if($args{'-api'} != 0 && $args{'-api'} != 1) { |
39
|
1
|
|
|
|
|
196
|
Carp::croak("illegal api level: $args{'-api'}"); |
40
|
|
|
|
|
|
|
} |
41
|
1
|
50
|
|
|
|
6
|
if($] < 5.010) { |
42
|
0
|
|
|
|
|
0
|
my($package) = caller; |
43
|
0
|
|
|
|
|
0
|
$callers{$package} = $args{'-api'}; |
44
|
0
|
|
|
|
|
0
|
require Carp; |
45
|
0
|
|
|
|
|
0
|
Carp::carp("Because of the age of your Perl, -api $args{'-api'} " . |
46
|
|
|
|
|
|
|
'will be package scoped instead of block scoped. ' . |
47
|
|
|
|
|
|
|
'Please upgrade to 5.10 or better.'); |
48
|
|
|
|
|
|
|
} else { |
49
|
1
|
|
|
|
|
41
|
$^H{'PPIx::DocumentName/api'} = $args{'-api'}; ## no critic (Variables::RequireLocalizedPunctuationVars) |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
} |
52
|
|
|
|
|
|
|
} |
53
|
|
|
|
|
|
|
|
54
|
|
|
|
|
|
|
sub _api { |
55
|
61
|
|
|
61
|
|
124
|
my ( $api ) = @_; |
56
|
61
|
50
|
|
|
|
174
|
if($] < 5.010) { |
57
|
0
|
|
|
|
|
0
|
my($package) = caller 1; |
58
|
0
|
0
|
|
|
|
0
|
$api = $callers{$package} unless defined $api; |
59
|
|
|
|
|
|
|
} else { |
60
|
61
|
|
|
|
|
401
|
my $hh = (caller 1)[10]; |
61
|
61
|
100
|
66
|
|
|
298
|
$api = $hh->{'PPIx::DocumentName/api'} if defined $hh && !defined $api; |
62
|
|
|
|
|
|
|
} |
63
|
61
|
100
|
|
|
|
148
|
$api = 0 unless defined $api; |
64
|
61
|
|
|
|
|
115
|
return $api; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
sub _result { |
68
|
10
|
|
|
10
|
|
25
|
my($name, $ppi_document, $node) = @_; |
69
|
10
|
|
|
|
|
682
|
require PPIx::DocumentName::Result; |
70
|
10
|
|
|
|
|
37
|
PPIx::DocumentName::Result->_new($name, $ppi_document, $node); ## no critic (Subroutines::ProtectPrivateSubs) |
71
|
|
|
|
|
|
|
} |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
## OO |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
sub extract { |
77
|
13
|
|
|
13
|
1
|
265235
|
my ( $self, $ppi_document ) = @_; |
78
|
13
|
|
|
|
|
49
|
my $api = _api(undef); |
79
|
13
|
|
100
|
|
|
76
|
my $result = $self->extract_via_comment($ppi_document, $api) || $self->extract_via_statement($ppi_document, $api); |
80
|
13
|
|
|
|
|
718
|
return $result; |
81
|
|
|
|
|
|
|
} |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
|
84
|
|
|
|
|
|
|
sub extract_via_statement { |
85
|
22
|
|
|
22
|
1
|
15466
|
my ( undef, $ppi_document, $api ) = @_; |
86
|
|
|
|
|
|
|
|
87
|
22
|
|
|
|
|
70
|
$api = _api($api); |
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
# Keep alive until done |
90
|
|
|
|
|
|
|
# https://github.com/adamkennedy/PPI/issues/112 |
91
|
22
|
|
|
|
|
97
|
my $dom = _Document($ppi_document); |
92
|
22
|
|
|
|
|
25887
|
my $pkg_node = $dom->find_first('PPI::Statement::Package'); |
93
|
22
|
100
|
|
|
|
4451
|
if ( not $pkg_node ) { |
94
|
8
|
|
|
0
|
|
45
|
log_debug { "No PPI::Statement::Package found in <<$ppi_document>>" }; |
|
0
|
|
|
|
|
0
|
|
95
|
|
|
|
|
|
|
# The old API was inconsistant here, for just this method, returns |
96
|
|
|
|
|
|
|
# empty list on failure. This is unfortunately different from |
97
|
|
|
|
|
|
|
# extract_via_comment. |
98
|
8
|
100
|
|
|
|
49
|
return 1 == $api ? undef : (); |
99
|
|
|
|
|
|
|
} |
100
|
14
|
50
|
|
|
|
77
|
if ( not $pkg_node->namespace ) { |
101
|
0
|
|
|
0
|
|
0
|
log_debug { "PPI::Statement::Package $pkg_node has empty namespace in <<$ppi_document>>" }; |
|
0
|
|
|
|
|
0
|
|
102
|
0
|
0
|
|
|
|
0
|
return 1 == $api ? undef : (); |
103
|
|
|
|
|
|
|
} |
104
|
14
|
|
|
|
|
415
|
my $name = $pkg_node->namespace; |
105
|
14
|
100
|
|
|
|
341
|
return 1 == $api ? _result($name, $dom, $pkg_node) : $name; |
106
|
|
|
|
|
|
|
} |
107
|
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
sub extract_via_comment { |
110
|
26
|
|
|
26
|
1
|
13270
|
my ( undef, $ppi_document, $api ) = @_; |
111
|
|
|
|
|
|
|
|
112
|
26
|
|
|
|
|
73
|
$api = _api($api); |
113
|
26
|
|
|
|
|
75
|
my $node; |
114
|
|
|
|
|
|
|
|
115
|
26
|
|
|
|
|
113
|
my $regex = qr{ ^ \s* \#+ \s* PODNAME: \s* (.+) $ }x; ## no critic (RegularExpressions) |
116
|
26
|
|
|
|
|
52
|
my $content; |
117
|
|
|
|
|
|
|
my $finder = sub { |
118
|
196
|
|
|
196
|
|
2104
|
my $maybe = $_[1]; |
119
|
196
|
100
|
|
|
|
670
|
return 0 unless $maybe->isa('PPI::Token::Comment'); |
120
|
8
|
|
|
|
|
56
|
log_trace { "Found comment node $maybe" }; |
|
0
|
|
|
|
|
0
|
|
121
|
8
|
50
|
|
|
|
42
|
if ( $maybe->content =~ $regex ) { |
122
|
8
|
|
|
|
|
109
|
$content = $1; |
123
|
8
|
|
|
|
|
16
|
$node = $maybe; |
124
|
8
|
|
|
|
|
22
|
return 1; |
125
|
|
|
|
|
|
|
} |
126
|
0
|
|
|
|
|
0
|
return 0; |
127
|
26
|
|
|
|
|
136
|
}; |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
# Keep alive until done |
130
|
|
|
|
|
|
|
# https://github.com/adamkennedy/PPI/issues/112 |
131
|
26
|
|
|
|
|
105
|
my $dom = _Document($ppi_document); |
132
|
26
|
|
|
|
|
33595
|
$dom->find_first($finder); |
133
|
|
|
|
|
|
|
|
134
|
26
|
100
|
|
0
|
|
470
|
log_debug { "<<$ppi_document>> has no PODNAME comment" } if not $content; |
|
0
|
|
|
|
|
0
|
|
135
|
|
|
|
|
|
|
|
136
|
26
|
100
|
100
|
|
|
211
|
return 1 == $api && defined $content ? _result($content, $dom, $node) : $content; |
137
|
|
|
|
|
|
|
} |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
1; |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
__END__ |