ExtractUse.pm

filter: Statementstmt 79.5% 132 / 166 Branchbran 72.4% 42 / 58 Conditioncond 65.2% 15 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 67.7% 21 / 31 Podpod 100% 21 / 21 totaltotal 75.4% 236 / 313 CC 41 SCAR 23.9
CC
41
Cov
77%
extract_use
25.2
required
6.0
required_in_eval
6.0
CRAP 10.9
« ExtractUse/Grammar.pm ↑ Summary
1 package Module::ExtractUse;
2  
3 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 12 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 12 ▶use Pod::Strip;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 12 ▶use Parse::RecDescent 1.967009;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use Module::ExtractUse::Grammar;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 12 our $VERSION = '0.345';
12  
13 # ABSTRACT: Find out what modules are used
14  
15 #$::RD_TRACE=1;
16 #$::RD_HINT=1;
17  
18  
19  
20 146 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
21 146 my $class=shift;
22 146 return bless {
23 found=>{},
24 files=>0,
25 },$class;
26 }
27  
28  
29 # Regular expression to detect eval
30 # On newer perl, you can use named capture groups and (?&name) for recursive regex
31 # However, it requires perl newer than 5.008 declared as requirement in this module
32 12 my $re_block;
33 $re_block = qr {
34 ( # eval BLOCK, corresponding to the group 10 in the entire regex
35 \{
36 ((?:
37 (?> [^{}]+ ) # Non-braces without backtracking
38 |
39 (??{$re_block}) # Recurse to group 10
40 )*)
41 \}
42 )
43 12 }xs;
44 my $re = qr{
45 \G(.*?) # group 1
46 eval
47 (?:
48 (?:\s+
49 (?:
50 qq?\((.*?)\) # eval q(), group 2
51 |
52 qq?\[(.*?)\] # eval q[], group 3
53 |
54 qq?{(.*?)} # eval q{}, group 4
55 |
56 qq?<(.*?)> # eval q<>, group 5
57 |
58 qq?(\S)(.*?)\6 # eval q'' or so, group 6, group 7
59 )
60 )
61 |
62 (?:\s*(?:
63 (?:(['"])(.*?)\8) # eval '' or eval "", group 8, group 9
64 |
65 ( # eval BLOCK, group 10
66 \{
67 ((?: # group 11
68 (?> [^{}]+ ) # Non-braces without backtracking
69 |
70 (??{$re_block}) # Recurse to group 10
71 )*)
72 \}
73 )
74 ))
75 )
76 12 }xs;
77  
78 383 ▶sub extract_use {
Subroutine extract_use: called CC=25 SCAR=32.3
Pod documented
79 383 my $self=shift;
80 383 my $code_to_parse=shift;
81  
82 383 my $podless;
83 383 my $pod_parser=Pod::Strip->new;
84 383 $pod_parser->output_string(\$podless);
85 383 ▶ $pod_parser->parse_characters(1) if $pod_parser->can('parse_characters');
Branch: 1 branch
BranchTrueFalseTotal
if $pod_parser->can("parse_characters") 383 0 383
86 383 ▶ if (ref($code_to_parse) eq 'SCALAR') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $code_to_parse eq 'SCALAR') { } 379 4 383
87 379 $pod_parser->parse_string_document($$code_to_parse);
88 }
89 else {
90 4 $pod_parser->parse_file($code_to_parse);
91 }
92  
93 # Strip obvious comments.
94 383 $podless =~ s/(^|[\};])\s*#.*$/$1/mg;
95  
96 # Strip __(DATA|END)__ sections.
97 383 $podless =~ s/\n__(?:DATA|END)__\b.*$//s;
98  
99 383 my @statements;
100 383 ▶ while($podless =~ /$re/gc) {
Branch: 1 branch
BranchTrueFalseTotal
if ($podless =~ /$re/cg) 51 383 434
101 # to keep parsing time short, split code in statements
102 # (I know that this is not very exact, patches welcome!)
103 51 my $pre = $1;
104 51 my $eval = join('', grep { defined $_ } ($2, $3, $4, $5, $7, $9, $11));
105 51 push @statements, map { [ 0, $_ ] } split(/;/, $pre); # non-eval context
106 51 push @statements, map { [ 1, $_ ] } split(/;/, $eval); # eval context
107 }
108 383 ▶ push @statements, map { [ 0, $_ ] } split(/;/, substr($podless, pos($podless) || 0)); # non-eval context
Condition100%2/2
pos $podless || 0
l!l
25358
MC/DC100%1/1
pos $podless || 0
pos $podless
Truth table100%2/2
A
A= pos $podless
Aresult
11
00
109  
110 383 foreach my $statement_ (@statements) {
111 1086 my ($eval, $statement) = @$statement_;
112 1086 $statement=~s/\n+/ /gs;
113 1086 my $result;
114  
115 # now that we've got some code containing 'use' or 'require',
116 # parse it! (using different entry point to save some more
117 # time)
118 my $type;
119 1086 ▶ if ($statement=~m/require_module|use_module|use_package_optimistically/) {
Branch: 5 branches
BranchTrueFalseTotal
if ($statement =~ /require_module|use_module|use_package_optimistically/) { } 69 1017 1086
elsif ($statement =~ /\buse/) { } 289 728 1017
elsif ($statement =~ /\brequire/) { } 54 674 728
elsif ($statement =~ /\bno/) { } 41 633 674
elsif ($statement =~ /load_class|try_load_class|load_first_existing_class|load_optional_class/) { } 73 560 633
120 69 $statement=~s/^(.*?)\b(\S+(?:require_module|use_module|use_package_optimistically)\([^)]*\))/$2/;
121 69 ▶ next if $1 && $1 =~ /->\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 =~ /->\s*$/ 0 69 69
Condition66%2/3
$1 and $1 =~ /->\s*$/
!ll&&!rl&&r
57120
MC/DC0%0/2
$1 and $1 =~ /->\s*$/
$1 $1 =~ /->\s*$/
Truth table66%2/3
A and B
A= $1B= $1 =~ /->\s*$/
ABresult
0X0
100
111
122 69 eval {
123 69 my $parser=Module::ExtractUse::Grammar->new();
124 69 $result=$parser->token_module_runtime($statement);
125 };
126 69 ▶ $type = $statement =~ m/require/ ? 'require' : 'use';
Branch: 1 branch
BranchTrueFalseTotal
$statement =~ /require/ ? : 17 52 69
127 }
128 elsif ($statement=~/\buse/) {
129 289 $statement=~s/^(.*?)use\b/use/;
130 289 ▶ next if $1 && $1 =~ /->\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 =~ /->\s*$/ 3 286 289
Condition100%3/3
$1 and $1 =~ /->\s*$/
!ll&&!rl&&r
206803
MC/DC100%2/2
$1 and $1 =~ /->\s*$/
$1 $1 =~ /->\s*$/
Truth table100%3/3
A and B
A= $1B= $1 =~ /->\s*$/
ABresult
0X0
100
111
131 286 eval {
132 286 my $parser=Module::ExtractUse::Grammar->new();
133 286 $result=$parser->token_use($statement.';');
134 };
135 286 $type = 'use';
136 }
137 elsif ($statement=~/\brequire/) {
138 54 $statement=~s/^(.*?)require\b/require/s;
139 54 ▶ next if $1 && $1 =~ /->\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 =~ /->\s*$/ 3 51 54
Condition100%3/3
$1 and $1 =~ /->\s*$/
!ll&&!rl&&r
19323
MC/DC100%2/2
$1 and $1 =~ /->\s*$/
$1 $1 =~ /->\s*$/
Truth table100%3/3
A and B
A= $1B= $1 =~ /->\s*$/
ABresult
0X0
100
111
140 51 eval {
141 51 my $parser=Module::ExtractUse::Grammar->new();
142 51 $result=$parser->token_require($statement.';');
143 };
144 51 $type = 'require';
145 }
146 elsif ($statement=~/\bno/) {
147 41 $statement=~s/^(.*?)no\b/no/s;
148 41 ▶ next if $1 && $1 =~ /->\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 =~ /->\s*$/ 0 41 41
Condition66%2/3
$1 and $1 =~ /->\s*$/
!ll&&!rl&&r
31100
MC/DC0%0/2
$1 and $1 =~ /->\s*$/
$1 $1 =~ /->\s*$/
Truth table66%2/3
A and B
A= $1B= $1 =~ /->\s*$/
ABresult
0X0
100
111
149 41 eval {
150 41 my $parser=Module::ExtractUse::Grammar->new();
151 41 $result=$parser->token_no($statement.';');
152 };
153 41 $type = 'no';
154 }
155 elsif ($statement=~m/load_class|try_load_class|load_first_existing_class|load_optional_class/) {
156 73 $statement=~s/^(.*?)\b(\S+(?:load_class|try_load_class|load_first_existing_class|load_optional_class)\([^)]*\))/$2/;
157 73 ▶ next if $1 && $1 =~ /->\s*$/;
Branch: 1 branch
BranchTrueFalseTotal
if $1 and $1 =~ /->\s*$/ 0 73 73
Condition66%2/3
$1 and $1 =~ /->\s*$/
!ll&&!rl&&r
61120
MC/DC0%0/2
$1 and $1 =~ /->\s*$/
$1 $1 =~ /->\s*$/
Truth table66%2/3
A and B
A= $1B= $1 =~ /->\s*$/
ABresult
0X0
100
111
158 73 eval {
159 73 my $parser=Module::ExtractUse::Grammar->new();
160 73 $result = $parser->token_class_load($statement.';');
161 };
162 73 $type = 'require';
163 }
164  
165 1080 ▶ next unless $result;
Branch: 1 branch
BranchTrueFalseTotal
unless $result 735 345 1080
166  
167 345 foreach (split(/\s+/,$result)) {
168 405 ▶ $self->_add($_, $eval, $type) if($_);
Branch: 1 branch
BranchTrueFalseTotal
if $_ 402 3 405
169 }
170 }
171  
172 # increment file counter
173 383 $self->_inc_files;
174  
175 383 return $self;
176 }
177  
178  
179  
180 18 ▶sub used {
Subroutine used: called CC=2 SCAR=6.9
Pod documented
181 18 my $self=shift;
182 18 my $key=shift;
183 18 ▶ return $self->{found}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 17 1 18
184 1 return $self->{found};
185 }
186  
187  
188 18 ▶sub used_in_eval {
Subroutine used_in_eval: called CC=2 SCAR=6.9
Pod documented
189 18 my $self=shift;
190 18 my $key=shift;
191 18 ▶ return $self->{found_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 17 1 18
192 1 return $self->{found_in_eval};
193 }
194  
195  
196 18 ▶sub used_out_of_eval {
Subroutine used_out_of_eval: called CC=2 SCAR=6.9
Pod documented
197 18 my $self=shift;
198 18 my $key=shift;
199 18 ▶ return $self->{found_not_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 17 1 18
200 1 return $self->{found_not_in_eval};
201 }
202  
203  
204 0 ▶sub required {
Subroutine required: not called CC=2 SCAR=17.9
Pod documented
205 0 my $self=shift;
206 0 my $key=shift;
207 0 ▶ return $self->{require}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
208 0 return $self->{require};
209 }
210  
211  
212 0 ▶sub required_in_eval {
Subroutine required_in_eval: not called CC=2 SCAR=17.9
Pod documented
213 0 my $self=shift;
214 0 my $key=shift;
215 0 ▶ return $self->{require_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
216 0 return $self->{require_in_eval};
217 }
218  
219  
220 0 ▶sub required_out_of_eval {
Subroutine required_out_of_eval: not called CC=2 SCAR=17.9
Pod documented
221 0 my $self=shift;
222 0 my $key=shift;
223 0 ▶ return $self->{require_not_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
224 0 return $self->{require_not_in_eval};
225 }
226  
227  
228 0 ▶sub noed {
Subroutine noed: not called CC=2 SCAR=17.9
Pod documented
229 0 my $self=shift;
230 0 my $key=shift;
231 0 ▶ return $self->{no}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
232 0 return $self->{no};
233 }
234  
235  
236 0 ▶sub noed_in_eval {
Subroutine noed_in_eval: not called CC=2 SCAR=17.9
Pod documented
237 0 my $self=shift;
238 0 my $key=shift;
239 0 ▶ return $self->{no_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
240 0 return $self->{no_in_eval};
241 }
242  
243  
244 0 ▶sub noed_out_of_eval {
Subroutine noed_out_of_eval: not called CC=2 SCAR=17.9
Pod documented
245 0 my $self=shift;
246 0 my $key=shift;
247 0 ▶ return $self->{no_not_in_eval}{$key} if ($key);
Branch: 1 branch
BranchTrueFalseTotal
if $key 0 0 0
248 0 return $self->{no_not_in_eval};
249 }
250  
251  
252 21 ▶sub string {
Subroutine string: called CC=2 SCAR=7.0
Pod documented
253 21 my $self=shift;
254 21 ▶ my $sep=shift || ' ';
Condition50%1/2
shift() || ' '
l!l
021
MC/DC0%0/1
shift() || ' '
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
255 21 return join($sep,sort keys(%{$self->{found}}));
256 }
257  
258  
259 0 ▶sub string_in_eval {
Subroutine string_in_eval: not called CC=2 SCAR=17.9
Pod documented
260 0 my $self=shift;
261 0 ▶ my $sep=shift || ' ';
Condition0%0/2
shift() || ' '
l!l
00
MC/DC0%0/1
shift() || ' '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
262 0 return join($sep,sort keys(%{$self->{found_in_eval}}));
263 }
264  
265  
266 0 ▶sub string_out_of_eval {
Subroutine string_out_of_eval: not called CC=2 SCAR=17.9
Pod documented
267 0 my $self=shift;
268 0 ▶ my $sep=shift || ' ';
Condition0%0/2
shift() || ' '
l!l
00
MC/DC0%0/1
shift() || ' '
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
269 0 return join($sep,sort keys(%{$self->{found_not_in_eval}}));
270 }
271  
272  
273 111 ▶sub array {
Subroutine array: called CC=1 SCAR=0.0
Pod documented
274 111 return keys(%{shift->{found}})
275 }
276  
277  
278 111 ▶sub array_in_eval {
Subroutine array_in_eval: called CC=1 SCAR=0.0
Pod documented
279 111 return keys(%{shift->{found_in_eval}})
280 }
281  
282  
283 111 ▶sub array_out_of_eval {
Subroutine array_out_of_eval: called CC=1 SCAR=0.0
Pod documented
284 111 return keys(%{shift->{found_not_in_eval}})
285 }
286  
287  
288 110 ▶sub arrayref {
Subroutine arrayref: called CC=2 SCAR=6.9
Pod documented
289 110 my @a=shift->array;
290 110 ▶ return \@a if @a;
Branch: 1 branch
BranchTrueFalseTotal
if @a 82 28 110
291 28 return;
292 }
293  
294  
295 110 ▶sub arrayref_in_eval {
Subroutine arrayref_in_eval: called CC=2 SCAR=6.9
Pod documented
296 110 my @a=shift->array_in_eval;
297 110 ▶ return \@a if @a;
Branch: 1 branch
BranchTrueFalseTotal
if @a 3 107 110
298 107 return;
299 }
300  
301  
302 110 ▶sub arrayref_out_of_eval {
Subroutine arrayref_out_of_eval: called CC=2 SCAR=6.9
Pod documented
303 110 my @a=shift->array_out_of_eval;
304 110 ▶ return \@a if @a;
Branch: 1 branch
BranchTrueFalseTotal
if @a 80 30 110
305 30 return;
306 }
307  
308  
309 0 ▶sub files {
Subroutine files: not called CC=1 SCAR=6.9
Pod documented
310 0 return shift->{files};
311 }
312  
313 # Internal Accessor Methods
314 402 ▶sub _add {
Subroutine _add: called CC=2 SCAR=6.9
315 402 my $self=shift;
316 402 my $found=shift;
317 402 my $eval=shift;
318 402 my $type=shift;
319 402 $self->{found}{$found}++;
320 402 $self->{$type}{$found}++;
321 402 ▶ if ($eval) {
Branch: 1 branch
BranchTrueFalseTotal
if ($eval) { } 29 373 402
322 29 $self->{found_in_eval}{$found}++;
323 29 $self->{"${type}_in_eval"}{$found}++;
324 } else {
325 373 $self->{found_not_in_eval}{$found}++;
326 373 $self->{"${type}_not_in_eval"}{$found}++;
327 }
328 }
329  
330 0 ▶sub _found {
Subroutine _found: not called CC=1 SCAR=6.9
331 return shift->{found}
332 0 }
333  
334 383 ▶sub _inc_files {
Subroutine _inc_files: called CC=1 SCAR=0.0
335 383 shift->{files}++
336 }
337  
338 12 1;
339  
340 __END__
« ExtractUse/Grammar.pm ↑ Summary