TraceDeps/Scan.pm

filter: Statementstmt 95.5% 86 / 90 Branchbran 59.3% 19 / 32 Conditioncond 56.2% 9 / 16 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 14 / 14 Podpod 100% 7 / 7 totaltotal 81.5% 137 / 168 CC 24 SCAR 23.2
CC
24
Cov
83%
load
19.2
scan
4.8
required
4.2
CRAP 10.2
« TraceDeps.pm ↑ Summary eps.pm »
1 package Devel::TraceDeps::Scan;
2 2 $VERSION = v0.0.3;
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8  
9 2 ▶use Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 with 'new';
11 2 ▶no Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 =head1 NAME
14  
15 Devel::TraceDeps::Scan - frontend and data access
16  
17 =head1 SYNOPSIS
18  
19 my $scan = Devel::TraceDeps::Scan->load($filehandle);
20  
21 =cut
22  
23 =head1 Acquisition
24  
25  
26 =head2 scan
27  
28 my $scan = Devel::TraceDeps::Scan->scan(file => $filename, %opts);
29  
30 =cut
31  
32 2 ▶sub scan {
Subroutine scan: called CC=4 SCAR=15.8
Pod documented
33 2 my $me = shift;
34 2 my (%opts) = @_;
35  
36 2 my @cmd;
37 2 ▶ if(my $file = $opts{file}) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $file = $opts{'file'}) { } 2 0 2
elsif (my $code = $opts{'code'}) { } 0 0 0
38 2 @cmd = ($file);
39 }
40 elsif(my $code = $opts{code}) {
41 0 @cmd = ('-e' => $code);
42 }
43 else {
44 0 croak("must have something (code|file) to scan");
45 }
46  
47 # bah IPC::Cmd gives me invalid free or something
48 2 ▶ open(my $fh, '-|', $^X, '-MDevel::TraceDeps', @cmd) or
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, "-|", $^X, "-MDevel::TraceDeps", @cmd or croak("cannot run @cmd $!") 2 0 2
49 croak("cannot run @cmd $!");
50  
51 2 my $self = $me->load($fh);
52 2 return($self);
53 } # end subroutine scan definition
54 ########################################################################
55  
56 =head1 Retrieval
57  
58 =head2 load
59  
60 C<$source> may be a filename, or a reference to an open filehandle or
61 string.
62  
63 my $scan = Devel::TraceDeps::Scan->load($source);
64  
65 =cut
66  
67 3 ▶sub load {
Subroutine load: called CC=17 SCAR=29.6
Pod documented
68 3 my $package = shift;
69 3 my ($source) = @_;
70  
71 3 ▶ my $self = ref($package) ? $package : $package->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $package ? : 0 3 3
72  
73 3 my $fh;
74 3 ▶ if(my $r = ref($source)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $r = ref $source) { } 3 0 3
75 3 ▶ if($r eq 'GLOB' or
Branch: 1 branch
BranchTrueFalseTotal
if ($r eq 'GLOB' or $source =~ /=GLOB\(0x[0-9a-f]+\)$/ or eval { do { overload::Method($source, '<>') } }) { } 2 1 3
Condition66%4/6
$r eq "GLOB" or $source =~ /=GLOB\(0x[0-9a-f]+\)$/ or eval { do { overload::Method($source, "<>") } }
l!l&&r!l&&!r
201
$r eq "GLOB" or $source =~ /=GLOB\(0x[0-9a-f]+\)$/
l!l&&r!l&&!r
201
MC/DC33%1/3
$r eq "GLOB" or $source =~ /=GLOB\(0x[0-9a-f]+\)$/ or eval { do { overload::Method($source, "<>") } }
$r eq "GLOB" $source =~ /=GLOB\(0x[0-9a-f]+\)$/ eval { do { overload::Method($source, "<>") } }
Truth table50%2/4
A or B or C
A= $r eq "GLOB"B= $source =~ /=GLOB\(0x[0-9a-f]+\)$/C= eval { do { overload::Method($source, "<>") } }
ABCresult
1XX1
01X1
0011
0000
76 $source =~ m/=GLOB\(0x[0-9a-f]+\)$/ or
77 1 eval {overload::Method($source, '<>')}
78 ) {
79 2 $fh = $source;
80 }
81 else {
82 1 ▶ open($fh, '<', $source) or die "open string failed $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $source or die "open string failed $!" 1 0 1
83 }
84 }
85 else {
86 0 ▶ open($fh, '<', $source) or die "open file '$source' failed $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, "<", $source or die "open file '${source}' failed $!" 0 0 0
87 }
88  
89 3 ▶ $self->{order} ||= [];
Condition50%1/2
$self->{"order"} ||= []
l!l
03
MC/DC0%0/1
$self->{"order"} ||= []
$self->{"order"}
Truth table50%1/2
A
A= $self->{"order"}
Aresult
11
00
90 3 ▶ $self->{store} ||= {};
Condition50%1/2
$self->{"store"} ||= {}
l!l
03
MC/DC0%0/1
$self->{"store"} ||= {}
$self->{"store"}
Truth table50%1/2
A
A= $self->{"store"}
Aresult
11
00
91  
92 3 my $pack;
93 my $current;
94 3 ▶ while(my $line = <$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $line = readline $fh)) 10 3 13
95 10 chomp($line);
96 10 my ($mod, $rest) = split(/ /, $line, 2);
97 #warn "$mod|$rest\n";
98 10 ▶ if(length($mod)) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $mod) { } 1 9 10
99 1 ▶ push(@{$self->{order}}, $mod) unless($self->{store}{$mod});
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"store"}{$mod} 1 0 1
100 1 $current = '';
101 1 $pack = $mod;
102 }
103 else {
104 9 ▶ if($rest eq '-----') {
Branch: 1 branch
BranchTrueFalseTotal
if ($rest eq "-----") 2 7 9
105 2 $current = Devel::TraceDeps::Scan::Item->new(by => $pack);
106 2 push(@{$self->{store}{$pack}}, $current);
107 2 next;
108 }
109 7 my ($key, $val) = split(/: /, $rest, 2);
110 # pretend every .pm was loaded with the :: form
111 7 ▶ $val =~ s#/+#::#g if($key eq 'req' and $val =~ s/\.pm$//);
Branch: 1 branch
BranchTrueFalseTotal
if $key eq "req" and $val =~ s/\.pm$// 2 5 7
Condition66%2/3
$key eq "req" and $val =~ s/\.pm$//
!ll&&!rl&&r
502
MC/DC50%1/2
$key eq "req" and $val =~ s/\.pm$//
$key eq "req" $val =~ s/\.pm$//
Truth table66%2/3
A and B
A= $key eq "req"B= $val =~ s/\.pm$//
ABresult
0X0
100
111
112 7 ▶ $current or croak("out-of-sequence in $pack");
Branch: 1 branch
BranchTrueFalseTotal
$current or croak("out-of-sequence in $pack") 7 0 7
113 7 $current->{$key} = $val;
114 }
115 }
116 3 return($self);
117 } # end subroutine load definition
118 ########################################################################
119  
120 =head1 Querying the Data
121  
122 =head2 callers
123  
124 The list of all packages which called use(), require(), or do().
125  
126 my @callers = $scan->callers;
127  
128 =cut
129  
130 5 ▶sub callers {
Subroutine callers: called CC=1 SCAR=0.0
Pod documented
131 5 my $self = shift;
132 5 return(@{$self->{order}});
133 } # end subroutine callers definition
134 ########################################################################
135  
136 =head2 items
137  
138 Return all of the use/require/do events.
139  
140 my @items = $scan->items;
141  
142 =cut
143  
144 4 ▶sub items {
Subroutine items: called CC=1 SCAR=0.0
Pod documented
145 4 my $self = shift;
146 4 return(map({@{$self->{store}{$_}}} $self->callers));
147 } # end subroutine items definition
148 ########################################################################
149  
150 =head2 items_for
151  
152 Return all of the use/require/do events for a given package.
153  
154 my @items_for = $scan->items_for($caller);
155  
156 =cut
157  
158 1 ▶sub items_for {
Subroutine items_for: called CC=2 SCAR=7.6
Pod documented
159 1 my $self = shift;
160 1 my ($pack) = @_;
161  
162 1 ▶ my $array = $self->{store}{$pack} or return();
Branch: 1 branch
BranchTrueFalseTotal
my $array = $self->{"store"}{$pack} or return () 0 1 1
163 0 return(@$array);
164 } # end subroutine items_for definition
165 ########################################################################
166  
167 =head2 required
168  
169 A unique list of use/require/do items.
170  
171 my @required = $scan->required;
172  
173 =cut
174  
175 2 ▶sub required {
Subroutine required: called CC=4 SCAR=14.3
Pod documented
176 2 my $self = shift;
177  
178 2 my @out;
179 my %seen;
180 2 foreach my $item ($self->items) {
181 4 ▶ my $key = $item->req || $item->did;
Condition33%1/3
$item->req || $item->did
l!l&&r!l&&!r
400
MC/DC0%0/2
$item->req || $item->did
$item->req $item->did
Truth table33%1/3
A || B
A= $item->reqB= $item->did
ABresult
1X1
011
000
182 4 ▶ $seen{$key} and next;
Branch: 1 branch
BranchTrueFalseTotal
$seen{$key} and next 0 4 4
183 4 $seen{$key} = 1;
184 4 push(@out, $item);
185 }
186 2 return(@out);
187 } # end subroutine required definition
188 ########################################################################
189  
190 =head2 loaded
191  
192 Everything from required() which did not fail to load.
193  
194 my @loaded = $scan->loaded;
195  
196 =cut
197  
198 1 ▶sub loaded {
Subroutine loaded: called CC=1 SCAR=0.0
Pod documented
199 1 my $self = shift;
200 1 return(grep({not $_->fail} $self->required));
201 } # end subroutine loaded definition
202 ########################################################################
203  
204  
205 =head1 Item objects
206  
207 C<Devel::TraceDeps::Scan::Item> objects are returned by several of the
208 above methods. The have the following attributes:
209  
210 =over
211  
212 =item by
213  
214 The package which required this item (caller).
215  
216 =item trace
217  
218 A string of sequence numbers indicating the tree of this call.
219  
220 =item file
221  
222 Filename from caller().
223  
224 =item line
225  
226 Line number from caller().
227  
228 =item req
229  
230 The require()d module filename (as found in C<keys %INC> -- e.g. 'use
231 Foo::Bar' would appear as 'Foo/Bar.pm'.)
232  
233 This will typically be a relative path (within @INC), but might be
234 absolute or ever relative to './' (depending on how use/require/do was
235 called.
236  
237 =item ver
238  
239 Item is a requirement for a minimum perl version. If 'req' is empty,
240 look here.
241  
242 =item did
243  
244 The argument to do("filename").
245  
246 =item fail
247  
248 True if the require() failed. This probably means that the file does
249 not exist (e.g. C<eval {require thing}> or C<do("whatever")>)
250  
251 =item err
252  
253 Any $@ or $! found.
254  
255 =back
256  
257 =cut
258  
259 {
260 package Devel::TraceDeps::Scan::Item;
261 2 ▶use Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
262 2 with 'new';
263 2 ro qw(
264 by
265 trace
266 req
267 ver
268 did
269 file
270 line
271 fail
272 err
273 );
274 2 ▶no Class::Accessor::Classy;
Subroutine BEGIN: called CC=1 SCAR=0.0
275 }
276  
277 =head1 AUTHOR
278  
279 Eric Wilhelm @ <ewilhelm at cpan dot org>
280  
281 http://scratchcomputing.com/
282  
283 =head1 BUGS
284  
285 If you found this module on CPAN, please report any bugs or feature
286 requests through the web interface at L<http://rt.cpan.org>. I will be
287 notified, and then you'll automatically be notified of progress on your
288 bug as I make changes.
289  
290 If you pulled this development version from my /svn/, please contact me
291 directly.
292  
293 =head1 COPYRIGHT
294  
295 Copyright (C) 2008 Eric L. Wilhelm, All Rights Reserved.
296  
297 =head1 NO WARRANTY
298  
299 Absolutely, positively NO WARRANTY, neither express or implied, is
300 offered with this software. You use this software at your own risk. In
301 case of loss, no person or entity owes you anything whatsoever. You
302 have been warned.
303  
304 =head1 LICENSE
305  
306 This program is free software; you can redistribute it and/or modify it
307 under the same terms as Perl itself.
308  
309 =cut
310  
311 # vi:ts=2:sw=2:et:sta
312 2 1;
« TraceDeps.pm ↑ Summary eps.pm »