lib/Module/Info.pm

filter: Statementstmt 94.5% 244 / 258 Branchbran 63.2% 62 / 98 Conditioncond 54.0% 20 / 37 MC/DCmcdc 26.0% 6 / 23 Subroutinesub 100% 41 / 41 Podpod 100% 21 / 21 totaltotal 82.4% 394 / 478 CC 68 SCAR 20.5
CC
68
Cov
83%
has_pod
25.9
version
18.6
_find_all_installed
7.0
CRAP 7.8
« blib/lib/B/Module/Info.pm ↑ Summary
1 package Module::Info;
2  
3 6 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 6 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 6 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 6 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 6 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 6 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 6 ▶my $has_version_pm = eval 'use version; 1';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 our $AUTOLOAD;
13 7 our $VERSION;
14  
15 6 ▶$VERSION = eval 'use version; 1' ? 'version'->new('0.39') : '0.39';
Subroutine BEGIN: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
eval 'use version; 1' ? : 6 0 6
16 31 $VERSION = eval $VERSION;
17  
18  
19 =head1 NAME
20  
21 Module::Info - Information about Perl modules
22  
23 =head1 SYNOPSIS
24  
25 use Module::Info;
26  
27 my $mod = Module::Info->new_from_file('Some/Module.pm');
28 my $mod = Module::Info->new_from_module('Some::Module');
29 my $mod = Module::Info->new_from_loaded('Some::Module');
30  
31 my @mods = Module::Info->all_installed('Some::Module');
32  
33 my $name = $mod->name;
34 my $version = $mod->version;
35 my $dir = $mod->inc_dir;
36 my $file = $mod->file;
37 my $is_core = $mod->is_core;
38  
39 # Only available in perl 5.6.1 and up.
40 # These do compile the module.
41 my @packages = $mod->packages_inside;
42 my @used = $mod->modules_used;
43 my @subs = $mod->subroutines;
44 my @isa = $mod->superclasses;
45 my @calls = $mod->subroutines_called;
46  
47 # Check for constructs which make perl hard to predict.
48 my @methods = $mod->dynamic_method_calls;
49 my @lines = $mod->eval_string; *UNIMPLEMENTED*
50 my @lines = $mod->gotos; *UNIMPLEMENTED*
51 my @controls = $mod->exit_via_loop_control; *UNIMPLEMENTED*
52 my @unpredictables = $mod->has_unpredictables; *UNIMPLEMENTED*
53  
54 # set/get Module::Info options
55 $self->die_on_compilation_error(1);
56 my $die_on_error = $mod->die_on_compilation_error;
57 $self->safe(1);
58 my $safe = $mod->safe;
59  
60 =head1 DESCRIPTION
61  
62 Module::Info gives you information about Perl modules B<without
63 actually loading the module>. It actually isn't specific to modules
64 and should work on any perl code.
65  
66 =head1 METHODS
67  
68 =head2 Constructors
69  
70 There are a few ways to specify which module you want information for.
71 They all return Module::Info objects.
72  
73 =over 4
74  
75 =item new_from_file
76  
77 my $module = Module::Info->new_from_file('path/to/Some/Module.pm');
78  
79 Given a file, it will interpret this as the module you want
80 information about. You can also hand it a perl script.
81  
82 If the file doesn't exist or isn't readable it will return false.
83  
84 =cut
85  
86 6 ▶sub new_from_file {
Subroutine new_from_file: called CC=3 SCAR=11.1
Pod documented
87 6 my($proto, $file) = @_;
88 6 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
89  
90 6 ▶ return unless -r $file;
Branch: 1 branch
BranchTrueFalseTotal
unless -r $file 1 21 22
91  
92 6 my $self = {};
93 6 $self->{file} = File::Spec->rel2abs($file);
94 6 $self->{dir} = '';
95 8 $self->{name} = '';
96 8 $self->{safe} = 0;
97 8 $self->{use_version} = 0;
98  
99 8 return bless $self, $class;
100 }
101  
102 =item new_from_module
103  
104 my $module = Module::Info->new_from_module('Some::Module');
105 my $module = Module::Info->new_from_module('Some::Module', @INC);
106  
107 Given a module name, @INC will be searched and the first module found
108 used. This is the same module that would be loaded if you just say
109 C<use Some::Module>.
110  
111 If you give your own @INC, that will be used to search instead.
112  
113 =cut
114  
115 6 ▶sub new_from_module {
Subroutine new_from_module: called CC=1 SCAR=0.0
Pod documented
116 8 my($class, $module, @inc) = @_;
117 8 return ($class->_find_all_installed($module, 1, @inc))[0];
118 }
119  
120 =item new_from_loaded
121  
122 my $module = Module::Info->new_from_loaded('Some::Module');
123  
124 Gets information about the currently loaded version of Some::Module.
125 If it isn't loaded, returns false.
126  
127 =cut
128  
129 5 ▶sub new_from_loaded {
Subroutine new_from_loaded: called CC=3 SCAR=11.0
Pod documented
130 5 my($class, $name) = @_;
131  
132 5 my $mod_file = join('/', split('::', $name)) . '.pm';
133 5 ▶ my $filepath = $INC{$mod_file} || '';
Condition100%2/2
$INC{$mod_file} || ''
l!l
11
MC/DC100%1/1
$INC{$mod_file} || ''
$INC{$mod_file}
Truth table100%2/2
A
A= $INC{$mod_file}
Aresult
11
00
134  
135 5 ▶ my $module = Module::Info->new_from_file($filepath) or return;
Branch: 1 branch
BranchTrueFalseTotal
my $module = "Module::Info"->new_from_file($filepath) or return 1 1 2
136 5 $module->{name} = $name;
137 5 ($module->{dir} = $filepath) =~ s|/?\Q$mod_file\E$||;
138 2 $module->{dir} = File::Spec->rel2abs($module->{dir});
139 2 $module->{safe} = 0;
140 2 $module->{use_version} = 0;
141  
142 2 return $module;
143 }
144  
145 =item all_installed
146  
147 my @modules = Module::Info->all_installed('Some::Module');
148 my @modules = Module::Info->all_installed('Some::Module', @INC);
149  
150 Like new_from_module(), except I<all> modules in @INC will be
151 returned, in the order they are found. Thus $modules[0] is the one
152 that would be loaded by C<use Some::Module>.
153  
154 =cut
155  
156 5 ▶sub all_installed {
Subroutine all_installed: called CC=1 SCAR=0.0
Pod documented
157 2 my($class, $module, @inc) = @_;
158 2 return $class->_find_all_installed($module, 0, @inc);
159 }
160  
161 # Thieved from Module::InstalledVersion
162 5 ▶sub _find_all_installed {
Subroutine _find_all_installed: called CC=7 SCAR=19.5
163 15 my($proto, $name, $find_first_one, @inc) = @_;
164 15 ▶ my($class) = ref $proto || $proto;
Condition33%1/3
ref $proto || $proto
l!l&&r!l&&!r
0150
MC/DC0%0/2
ref $proto || $proto
ref $proto $proto
Truth table33%1/3
A || B
A= ref $protoB= $proto
ABresult
1X1
011
000
165  
166 15 ▶ @inc = @INC unless @inc;
Branch: 1 branch
BranchTrueFalseTotal
unless @inc 13 2 15
167 18 my $file = File::Spec->catfile(split /::/, $name) . '.pm';
168  
169 18 my @modules = ();
170 18 DIR: foreach my $dir (@inc) {
171 # Skip the new code ref in @INC feature.
172 24 ▶ next if ref $dir;
Branch: 1 branch
BranchTrueFalseTotal
if ref $dir 1 44 45
173  
174 24 my $filename = File::Spec->catfile($dir, $file);
175 24 ▶ if( -r $filename ) {
Branch: 1 branch
BranchTrueFalseTotal
if (-r $filename) 17 27 44
176 21 my $module = $class->new_from_file($filename);
177 13 $module->{dir} = File::Spec->rel2abs($dir);
178 13 $module->{name} = $name;
179 16 push @modules, $module;
180 16 ▶ last DIR if $find_first_one;
Branch: 1 branch
BranchTrueFalseTotal
if $find_first_one 13 4 17
181 }
182 }
183  
184 16 return @modules;
185 }
186  
187  
188 =back
189  
190 =head2 Information without loading
191  
192 The following methods get their information without actually compiling
193 the module.
194  
195 =over 4
196  
197 =item B<name>
198  
199 my $name = $module->name;
200 $module->name($name);
201  
202 Name of the module (ie. Some::Module).
203  
204 Module loaded using new_from_file() won't have this information in
205 which case you can set it yourself.
206  
207 =cut
208  
209 2 ▶sub name {
Subroutine name: called CC=2 SCAR=6.9
Pod documented
210 5 my($self) = shift;
211  
212 4 ▶ $self->{name} = shift if @_;
Branch: 1 branch
BranchTrueFalseTotal
if @_ 2 7 9
213 4 return $self->{name};
214 }
215  
216 =item B<version>
217  
218 my $version = $module->version;
219  
220 Divines the value of $VERSION. This uses the same method as
221 ExtUtils::MakeMaker and all caveats therein apply.
222  
223 =cut
224  
225 # Thieved from ExtUtils::MM_Unix 1.12603
226 5 ▶sub version {
Subroutine version: called CC=17 SCAR=29.2
Pod documented
227 4 my($self) = shift;
228 4 local($_, *MOD);
229  
230 4 my $parsefile = $self->file;
231 1 my $safe = $self->safe;
232  
233 2 ▶ open(MOD, $parsefile) or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open MOD, $parsefile or die $! 14 0 14
234  
235 2 my $inpod = 0;
236 7 my $result;
237 7 ▶ while (<MOD>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline MOD)) 108 0 108
238 7 ▶ $inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
Branch: 2 branches
BranchTrueFalseTotal
/^=(?!cut)/ ? : 0 108 108
/^=cut/ ? : 0 108 108
239 12 ▶ next if $inpod || /^\s*#/;
Branch: 1 branch
BranchTrueFalseTotal
if $inpod or /^\s*#/ 5 103 108
Condition66%2/3
$inpod or /^\s*#/
l!l&&r!l&&!r
05103
MC/DC50%1/2
$inpod or /^\s*#/
$inpod /^\s*#/
Truth table66%2/3
A or B
A= $inpodB= /^\s*#/
ABresult
1X1
011
000
240  
241 12 chomp;
242 # taken from ExtUtils::MM_Unix 6.63_02
243 12 ▶ next if /^\s*(if|unless|elsif)/;
Branch: 1 branch
BranchTrueFalseTotal
if /^\s*(if|unless|elsif)/ 0 103 103
244 31 ▶ if (m{^\s*package\s+\w[\w\:\']*\s+(v?[0-9._]+)\s*;}) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*package\s+\w[\w\:\']*\s+(v?[0-9._]+)\s*;/) 1 102 103
245 30 local $^W = 0;
246 42 $result = $1;
247 31 last;
248 }
249 29 ▶ next unless /([\$*])(([\w\:\']*)\bVERSION)\b.*\=/;
Branch: 1 branch
BranchTrueFalseTotal
unless /([\$*])(([\w\:\']*)\bVERSION)\b.*\=/ 89 13 102
250 29 ▶ my $eval = sprintf qq{
Branch: 1 branch
BranchTrueFalseTotal
$safe ? : 2 11 13
251 package Module::Info::_version;
252 %s
253  
254 local $1$2;
255 \$$2=undef; do {
256 %s
257 }; \$$2
258 }, ( $safe ? '' : 'no strict;' ), $_;
259 24 local $^W = 0;
260 11 $result = $self->_eval($eval);
261 9 ▶ warn "Could not eval '$eval' in $parsefile: $@" if $@ && !$safe;
Branch: 1 branch
BranchTrueFalseTotal
if $@ and not $safe 0 13 13
Condition66%2/3
$@ and not $safe
!ll&&!rl&&r
1210
MC/DC0%0/2
$@ and not $safe
$@ not $safe
Truth table66%2/3
A and not(B)
A= $@B= not $safe
ABresult
0X0
100
111
262 14 ▶ $result = "undef" unless defined $result;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $result 1 12 13
263 27 last;
264 }
265 17 close MOD;
266 7 ▶ $result = 'version'->new($result) # quotes for 5.004
Branch: 1 branch
BranchTrueFalseTotal
if $self->use_version and not ref $result && UNIVERSAL::isa($result, "version") 4 10 14
Condition50%3/6
$self->use_version and not ref $result && UNIVERSAL::isa($result, "version")
!ll&&!rl&&r
1004
ref $result && UNIVERSAL::isa($result, 'version')
!ll&&!rl&&r
400
MC/DC33%1/3
$self->use_version and not ref $result && UNIVERSAL::isa($result, "version")
$self->use_version ref $result UNIVERSAL::isa($result, 'version')
Truth table50%2/4
A and not(B && C)
A= $self->use_versionB= ref $resultC= UNIVERSAL::isa($result, 'version')
ABCresult
0XX0
1110
10X1
1101
267 if $self->use_version
268 && (!ref($result) || !UNIVERSAL::isa($result, "version"));
269 7 return $result;
270 }
271  
272  
273 =item B<inc_dir>
274  
275 my $dir = $module->inc_dir;
276  
277 Include directory in which this module was found. Module::Info
278 objects created with new_from_file() won't have this info.
279  
280 =cut
281  
282 2 ▶sub inc_dir {
Subroutine inc_dir: called CC=1 SCAR=0.0
Pod documented
283 7 my($self) = shift;
284  
285 14 return $self->{dir};
286 }
287  
288 =item B<file>
289  
290 my $file = $module->file;
291  
292 The absolute path to this module.
293  
294 =cut
295  
296 20 ▶sub file {
Subroutine file: called CC=1 SCAR=0.0
Pod documented
297 19 my($self) = shift;
298  
299 19 return $self->{file};
300 }
301  
302 =item B<is_core>
303  
304 my $is_core = $module->is_core;
305  
306 Checks if this module is the one distributed with Perl.
307  
308 B<NOTE> This goes by what directory it's in. It's possible that the
309 module has been altered or upgraded from CPAN since the original Perl
310 installation.
311  
312 =cut
313  
314 7 ▶sub is_core {
Subroutine is_core: called CC=1 SCAR=0.0
Pod documented
315 14 my($self) = shift;
316  
317 return scalar grep $self->{dir} eq File::Spec->canonpath($_),
318 ($Config{installarchlib},
319 $Config{installprivlib},
320 $Config{archlib},
321 14 $Config{privlib});
322 }
323  
324 =item B<has_pod>
325  
326 my $has_pod = $module->has_pod;
327  
328 Returns the location of the module's pod, which can be the module file itself,
329 if the POD is inlined, the associated POD file, or nothing if there is no POD
330 at all.
331  
332 =cut
333  
334 10 ▶sub has_pod {
Subroutine has_pod: called CC=9 SCAR=32.5
Pod documented
335 67 my $self = shift;
336  
337 67 my $filename = $self->file;
338
339 70 ▶ open my $file, "<", $filename or return; # the file won't even open
Branch: 1 branch
BranchTrueFalseTotal
open my $file, "<", $filename or return 0 0 0
340
341 96 ▶ while( <$file> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 0 0 0
342 93 ▶ return $filename if /^=[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if /^=[a-z]/ 0 0 0
343 }
344  
345 # nothing found? Try a companion POD file
346  
347 30 ▶ $filename =~ s/\.[^.]+$/.pod/ or return;
Branch: 1 branch
BranchTrueFalseTotal
$filename =~ s/\.[^.]+$/.pod/ or return 0 0 0
348  
349 30 ▶ return unless -f $filename;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $filename 0 0 0
350  
351 30 ▶ open $file, "<", $filename or return;
Branch: 1 branch
BranchTrueFalseTotal
open $file, "<", $filename or return 0 0 0
352
353 61 ▶ while( <$file> ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $file)) 0 0 0
354 5 ▶ return $filename if /^=[a-z]/;
Branch: 1 branch
BranchTrueFalseTotal
if /^=[a-z]/ 0 0 0
355 }
356
357 5 return;
358 }
359  
360 =back
361  
362 =head2 Information that requires loading.
363  
364 B<WARNING!> From here down reliability drops rapidly!
365  
366 The following methods get their information by compiling the module
367 and examining the opcode tree. The module will be compiled in a
368 separate process so as not to disturb the current program.
369  
370 They will only work on 5.6.1 and up and requires the B::Utils module.
371  
372 =over 4
373  
374 =item B<packages_inside>
375  
376 my @packages = $module->packages_inside;
377  
378 Looks for any explicit C<package> declarations inside the module and
379 returns a list. Useful for finding hidden classes and functionality
380 (like Tie::StdHandle inside Tie::Handle).
381  
382 B<KNOWN BUG> Currently doesn't spot package changes inside subroutines.
383  
384 =cut
385  
386 5 ▶sub packages_inside {
Subroutine packages_inside: called CC=1 SCAR=0.0
Pod documented
387 36 my $self = shift;
388  
389 13 my %packs = map {$_, 1} $self->_call_B('packages');
390 12 return keys %packs;
391 }
392  
393 =item B<package_versions>
394  
395 my %versions = $module->package_versions;
396  
397 Returns a hash whose keys are the packages contained in the module
398 (these are the same as what's returned by C<packages_inside()>), and
399 whose values are the versions of those packages.
400  
401 =cut
402  
403 4 ▶sub package_versions {
Subroutine package_versions: called CC=4 SCAR=14.5
Pod documented
404 10 my $self = shift;
405  
406 10 my @packs = $self->packages_inside;
407  
408 # To survive the print(), we translate undef into '~' and then back again.
409 10 (my $quoted_file = $self->file) =~ s/(['\\])/\\$1/g;
410 15 my $command = qq{-le "require '$quoted_file';};
411 15 foreach (@packs) {
412 36 $command .= " print defined $_->VERSION ? $_->VERSION : '~';"
413 }
414 29 $command .= qq{"};
415  
416 5 my ($status, @versions) = $self->_call_perl($command);
417 12 chomp @versions;
418 7 foreach (@versions) {
419 0 ▶ $_ = undef if $_ eq '~';
Branch: 1 branch
BranchTrueFalseTotal
if $_ eq "~" 3 35 38
420 }
421  
422 0 my %map;
423 0 @map{@packs} = @versions;
424  
425 0 return %map;
426 }
427  
428  
429 =item B<modules_used>
430  
431 my @used = $module->modules_used;
432  
433 Returns a list of all modules and files which may be C<use>'d or
434 C<require>'d by this module.
435  
436 B<NOTE> These modules may be conditionally loaded, can't tell. Also
437 can't find modules which might be used inside an C<eval>.
438  
439 =cut
440  
441 14 ▶sub modules_used {
Subroutine modules_used: called CC=1 SCAR=6.9
Pod documented
442 0 my($self) = shift;
443 0 my %used = $self->modules_required;
444  
445 0 return keys %used;
446 }
447  
448 =item B<modules_required>
449  
450 my %required = $module->modules_required;
451  
452 Returns a list of all modules and files which may be C<use>'d or
453 C<require>'d by this module, together with the minimum required version.
454  
455 The hash is keyed on the module/file name, the corrisponding value is
456 an array reference containing the requied versions, or an empty array
457 if no specific version was required.
458  
459 B<NOTE> These modules may be conditionally loaded, can't tell. Also
460 can't find modules which might be used inside an C<eval>.
461  
462 =cut
463  
464 5 ▶sub modules_required {
Subroutine modules_required: called CC=7 SCAR=19.5
Pod documented
465 1 my($self) = shift;
466  
467 1 my $mod_file = $self->file;
468 1 my @mods = $self->_call_B('modules_used');
469  
470 5 my @used_mods = ();
471 5 my %used_mods = ();
472 13 ▶ for (grep /^use \D/ && /at "\Q$mod_file\E" /, @mods) {
Condition66%2/3
/^use \D/ && /at "\Q$mod_file\E" /
!ll&&!rl&&r
16026
MC/DC50%1/2
/^use \D/ && /at "\Q$mod_file\E" /
/^use \D/ /at "\Q$mod_file\E" /
Truth table66%2/3
A && B
A= /^use \D/B= /at "\Q$mod_file\E" /
ABresult
0X0
100
111
473 10 my($file, $version) = /^use (\S+) \(([^\)]*)\)/;
474 8 ▶ $used_mods{_file2mod($file)} ||= [];
Condition100%2/2
$used_mods{_file2mod($file)} ||= []
l!l
818
MC/DC100%1/1
$used_mods{_file2mod($file)} ||= []
$used_mods{_file2mod($file)}
Truth table100%2/2
A
A= $used_mods{_file2mod($file)}
Aresult
11
00
475 8 ▶ next unless defined $version and length $version;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $version and length $version 20 6 26
Condition66%2/3
defined $version and length $version
!ll&&!rl&&r
0206
MC/DC50%1/2
defined $version and length $version
defined $version length $version
Truth table66%2/3
A and B
A= defined $versionB= length $version
ABresult
0X0
100
111
476  
477 5 push @{$used_mods{_file2mod($file)}}, $version;
478 }
479  
480 3 push @used_mods, map { my($file) = /^require bare (\S+)/; _file2mod($file) }
481 grep /^require bare \D/ , @mods;
482  
483 3 push @used_mods, map { /^require not bare (\S+)/; $1 }
484 grep /^require not bare \D/, @mods;
485  
486 39 foreach ( @used_mods ) { $used_mods{$_} = [] };
487 3 return %used_mods;
488 }
489  
490 26 ▶sub _file2mod {
Subroutine _file2mod: called CC=1 SCAR=0.0
491 13 my($mod) = shift;
492 14 $mod =~ s/\.pm//;
493 14 $mod =~ s|/|::|g;
494 14 return $mod;
495 }
496  
497  
498 =item B<subroutines>
499  
500 my %subs = $module->subroutines;
501  
502 Returns a hash of all subroutines defined inside this module and some
503 info about it. The key is the *full* name of the subroutine
504 (ie. $subs{'Some::Module::foo'} rather than just $subs{'foo'}), value
505 is a hash ref with information about the subroutine like so:
506  
507 start => line number of the first statement in the subroutine
508 end => line number of the last statement in the subroutine
509  
510 Note that the line numbers may not be entirely accurate and will
511 change as perl's backend compiler improves. They typically correspond
512 to the first and last I<run-time> statements in a subroutine. For
513 example:
514  
515 sub foo {
516 package Wibble;
517 $foo = "bar";
518 return $foo;
519 }
520  
521 Taking C<sub foo {> as line 1, Module::Info will report line 3 as the
522 start and line 4 as the end. C<package Wibble;> is a compile-time
523 statement. Again, this will change as perl changes.
524  
525 Note this only catches simple C<sub foo {...}> subroutine
526 declarations. Anonymous, autoloaded or eval'd subroutines are not
527 listed.
528  
529 =cut
530  
531 29 ▶sub subroutines {
Subroutine subroutines: called CC=1 SCAR=0.0
Pod documented
532 3 my($self) = shift;
533  
534 3 my $mod_file = $self->file;
535 3 my @subs = $self->_call_B('subroutines');
536 3 return map { /^(\S+) at "[^"]+" from (\d+) to (\d+)/;
537 3 ($1 => { start => $2, end => $3 }) }
538 grep /at "\Q$mod_file\E" /, @subs;
539 }
540  
541 24 ▶sub _get_extra_arguments { '' }
Subroutine _get_extra_arguments: called CC=1 SCAR=0.0
542  
543 4 ▶sub _call_B {
Subroutine _call_B: called CC=3 SCAR=11.0
544 24 my($self, $arg) = @_;
545  
546 24 my $mod_file = $self->file;
547 7 my $extra_args = $self->_get_extra_arguments;
548 7 my $command = qq{$extra_args "-MO=Module::Info,$arg" "$mod_file"};
549 7 my($status, @out) = $self->_call_perl($command);
550  
551 10 ▶ if( $status ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($status) 2 13 15
552 8 my $exit = $status >> 8;
553 5 my $msg = join "\n",
554 "B::Module::Info,$arg use failed with $exit saying:",
555 @out;
556  
557 5 ▶ if( $self->{die_on_compilation_error} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'die_on_compilation_error'}) { } 1 1 2
558 4 die $msg;
559 }
560 else {
561 4 warn $msg;
562 10 return;
563 }
564 }
565  
566 5 @out = grep !/syntax OK$/, @out;
567 31 chomp @out;
568 31 return @out;
569 }
570  
571  
572 =item B<superclasses>
573  
574 my @isa = $module->superclasses;
575  
576 Returns the value of @ISA for this $module. Requires that
577 $module->name be set to work.
578  
579 B<NOTE> superclasses() is currently cheating. See L<CAVEATS> below.
580  
581 =cut
582  
583 4 ▶sub superclasses {
Subroutine superclasses: called CC=2 SCAR=6.9
Pod documented
584 29 my $self = shift;
585  
586 29 my $mod_file = $self->file;
587 2 my $mod_name = $self->name;
588 2 ▶ unless( $mod_name ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($mod_name) 0 1 1
589 2 carp 'isa() requires $module->name to be set';
590 2 return;
591 }
592  
593 35 my $extra_args = $self->_get_extra_arguments;
594 35 my $command =
595 qq{-e "require q{$mod_file}; print join qq{\\n}, \@$mod_name\::ISA"};
596 12 my($status, @isa) = $self->_call_perl("$extra_args $command");
597 11 chomp @isa;
598 11 return @isa;
599 }
600  
601 =item B<subroutines_called>
602  
603 my @calls = $module->subroutines_called;
604  
605 Finds all the methods and functions which are called inside the
606 $module.
607  
608 Returns a list of hashes. Each hash represents a single function or
609 method call and has the keys:
610  
611 line line number where this call originated
612 class class called on if its a class method
613 type function, symbolic function, object method,
614 class method, dynamic object method or
615 dynamic class method.
616 (NOTE This format will probably change)
617 name name of the function/method called if not dynamic
618  
619  
620 =cut
621  
622 2 ▶sub subroutines_called {
Subroutine subroutines_called: called CC=3 SCAR=12.5
Pod documented
623 11 my($self) = shift;
624  
625 11 my @subs = $self->_call_B('subs_called');
626 11 my $mod_file = $self->file;
627  
628 11 @subs = grep /at "\Q$mod_file\E" line/, @subs;
629 0 my @out = ();
630 0 foreach (@subs) {
631 0 my %info = ();
632 0 ($info{type}) = /^(.+) call/;
633 0 ▶ $info{type} = 'symbolic function' if /using symbolic ref/;
Branch: 1 branch
BranchTrueFalseTotal
if /using symbolic ref/ 2 24 26
634 0 ($info{'name'}) = /to (\S+)/;
635 11 ($info{class})= /via (\S+)/;
636 11 ($info{line}) = /line (\d+)/;
637 11 push @out, \%info;
638 }
639 1 return @out;
640 }
641  
642 =back
643  
644 =head2 Information about Unpredictable Constructs
645  
646 Unpredictable constructs are things that make a Perl program hard to
647 predict what its going to do without actually running it. There's
648 nothing wrong with these constructs, but its nice to know where they
649 are when maintaining a piece of code.
650  
651 =over 4
652  
653 =item B<dynamic_method_calls>
654  
655 my @methods = $module->dynamic_method_calls;
656  
657 Returns a list of dynamic method calls (ie. C<$obj->$method()>) used
658 by the $module. @methods has the same format as the return value of
659 subroutines_called().
660  
661 =cut
662  
663 3 ▶sub dynamic_method_calls {
Subroutine dynamic_method_calls: called CC=1 SCAR=0.0
Pod documented
664 1 my($self) = shift;
665 1 return grep $_->{type} =~ /dynamic/, $self->subroutines_called;
666 }
667  
668 =back
669  
670 =head2 Options
671  
672 The following methods get/set specific option values for the
673 Module::Info object.
674  
675 =over 4
676  
677 =item B<die_on_compilation_error>
678  
679 $module->die_on_compilation_error(0); # default
680 $module->die_on_compilation_error(1);
681 my $flag = $module->die_on_compilation_error;
682  
683 Sets/gets the "die on compilation error" flag. When the flag is off
684 (default), and a module fails to compile, Module::Info simply emits a
685 watning and continues. When the flag is on and a module fails to
686 compile, Module::Info C<die()>s with the same error message it would use
687 in the warning.
688  
689 =cut
690  
691 5 ▶sub die_on_compilation_error {
Subroutine die_on_compilation_error: called CC=3 SCAR=11.7
Pod documented
692 3 my($self) = shift;
693  
694 2 ▶ $self->{die_on_compilation_error} = $_[0] ? 1 : 0 if @_;
Branch: 2 branches
BranchTrueFalseTotal
if @_ 2 0 2
$_[0] ? : 2 0 2
695 2 return $self->{die_on_compilation_error};
696 }
697  
698 =item B<safe>
699  
700 $module->safe(0); # default
701 $module->safe(1); # be safer
702 my $flag = $module->safe;
703  
704 Sets/gets the "safe" flag. When the flag is enabled all operations
705 requiring module compilation are forbidden and the C<version()> method
706 executes its code in a C<Safe> compartment.
707  
708 =cut
709  
710 36 ▶sub safe {
Subroutine safe: called CC=4 SCAR=14.1
Pod documented
711 8 my($self) = shift;
712  
713 8 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 67 69
714 1 ▶ $self->{safe} = $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 2 0 2
715 1 ▶ require Safe if $self->{safe};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"safe"} 2 0 2
716 }
717 8 return $self->{safe};
718 }
719  
720 9 ▶sub AUTOLOAD {
Subroutine AUTOLOAD: called CC=4 SCAR=13.9
721 9 ▶ my($super) = $_[0]->safe ? 'Module::Info::Safe' : 'Module::Info::Unsafe';
Branch: 1 branch
BranchTrueFalseTotal
$_[0]->safe ? : 4 46 50
722 23 my($method) = $AUTOLOAD;
723 23 $method =~ s/^.*::([^:]+)$/$1/;
724  
725 11 ▶ return if $method eq 'DESTROY';
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 19 31 50
726  
727 8 my($code) = $super->can($method);
728  
729 20 ▶ die "Can not find method '$method' in Module::Info" unless $code;
Branch: 1 branch
BranchTrueFalseTotal
unless $code 0 31 31
730  
731 36 goto &$code;
732 }
733  
734 =item B<use_version>
735  
736 $module->use_version(0); # do not use version.pm (default)
737 $module->use_version(1); # use version.pm, die if not present
738 my $flag = $module->use_version;
739  
740 Sets/gets the "use_version" flag. When the flag is enabled the 'version'
741 method always returns a version object.
742  
743 =cut
744  
745 26 ▶sub use_version {
Subroutine use_version: called CC=5 SCAR=17.2
Pod documented
746 32 my($self) = shift;
747  
748 32 ▶ if( @_ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_) 2 16 18
749 32 ▶ die "Can not use 'version.pm' as requested"
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] and not $has_version_pm 0 2 2
Condition33%1/3
$_[0] and not $has_version_pm
!ll&&!rl&&r
020
MC/DC0%0/2
$_[0] and not $has_version_pm
$_[0] not $has_version_pm
Truth table33%1/3
A and not(B)
A= $_[0]B= not $has_version_pm
ABresult
0X0
100
111
750 if $_[0] && !$has_version_pm;
751  
752 29 ▶ $self->{use_version} = $_[0] ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$_[0] ? : 2 0 2
753 }
754  
755 29 return $self->{use_version};
756 }
757  
758 =back
759  
760 =head1 REPOSITORY
761  
762 L<https://github.com/neilb/Module-Info>
763  
764 =head1 AUTHOR
765  
766 Michael G Schwern <schwern@pobox.com> with code from ExtUtils::MM_Unix,
767 Module::InstalledVersion and lots of cargo-culting from B::Deparse.
768  
769 Mattia Barbon <mbarbon@cpan.org> maintained
770 the module from 2002 to 2013.
771  
772 Neil Bowers <neilb@cpan.org> is the current maintainer.
773  
774 =head1 LICENSE
775  
776 This program is free software; you can redistribute it and/or
777 modify it under the same terms as Perl itself.
778  
779 =head1 THANKS
780  
781 Many thanks to Simon Cozens and Robin Houston for letting me chew
782 their ears about B.
783  
784 =head1 CAVEATS
785  
786 Code refs in @INC are currently ignored. If this bothers you submit a
787 patch.
788  
789 superclasses() is cheating and just loading the module in a separate
790 process and looking at @ISA. I don't think its worth the trouble to
791 go through and parse the opcode tree as it still requires loading the
792 module and running all the BEGIN blocks. Patches welcome.
793  
794 I originally was going to call superclasses() isa() but then I
795 remembered that would be bad.
796  
797 All the methods that require loading are really inefficient as they're
798 not caching anything. I'll worry about efficiency later.
799  
800 =cut
801  
802 package Module::Info::Safe;
803  
804 23 my $root = 'Module::Info::Safe::_safe';
805  
806 17 ▶sub _create_compartment {
Subroutine _create_compartment: called CC=1 SCAR=0.0
807 29 my $safe = Safe->new( $root );
808  
809 5 $safe->permit_only( qw(:base_orig :base_core) );
810  
811 4 return $safe;
812 }
813  
814 4 ▶sub _eval {
Subroutine _eval: called CC=2 SCAR=9.2
815 1 my($self, $code) = @_;
816 0 ▶ $self->{compartment} ||= _create_compartment;
Condition33%1/3
$self->{"compartment"} ||= _create_compartment()
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->{"compartment"} ||= _create_compartment()
$self->{"compartment"} _create_compartment()
Truth table33%1/3
A ||= B
A= $self->{"compartment"}B= _create_compartment()
ABresult
1X1
011
000
817  
818 3 return $self->{compartment}->reval( $code, 0 )
819 }
820  
821 4 ▶sub _call_perl {
Subroutine _call_perl: called CC=1 SCAR=0.0
822 2 die "Module::Info attemped an unsafe operation while in 'safe' mode.";
823 }
824  
825 package Module::Info::Unsafe;
826  
827 32 ▶sub _eval { eval($_[1]) }
Subroutine _eval: called CC=1 SCAR=0.0
828  
829 4 ▶sub _is_win95() {
Subroutine _is_win95: called CC=2 SCAR=9.2
830 36 ▶ return $^O eq 'MSWin32' && (Win32::GetOSVersion())[4] == 1;
Condition33%1/3
$^O eq 'MSWin32' && (Win32::GetOSVersion())[4] == 1
!ll&&!rl&&r
1800
MC/DC0%0/2
$^O eq 'MSWin32' && (Win32::GetOSVersion())[4] == 1
$^O eq 'MSWin32' (Win32::GetOSVersion())[4] == 1
Truth table33%1/3
A && B
A= $^O eq 'MSWin32'B= (Win32::GetOSVersion())[4] == 1
ABresult
0X0
100
111
831 }
832  
833 39 ▶sub _is_macos_classic() {
Subroutine _is_macos_classic: called CC=1 SCAR=0.0
834 10 return $^O eq 'MacOS';
835 }
836  
837 48 ▶sub _call_perl {
Subroutine _call_perl: called CC=5 SCAR=16.4
838 24 my($self, $args) = @_;
839  
840 54 ▶ my $perl = _is_macos_classic ? 'perl' : $^X;
Branch: 1 branch
BranchTrueFalseTotal
&_is_macos_classic() ? : 0 18 18
841 30 my $command = "$perl $args";
842 31 my @out;
843  
844 48 ▶ if( _is_win95 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (&_is_win95()) { } 0 18 18
elsif (&_is_macos_classic()) { } 0 18 18
845 37 require IPC::Open3;
846 29 local *OUTFH;
847 29 my($line, $in);
848 29 my $out = \*OUTFH;
849 10 my $pid = IPC::Open3::open3($in, $out, $out, $command);
850 10 close $in;
851 6 ▶ while( defined($line = <OUTFH>) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($line = readline OUTFH)) 0 0 0
852 11 $line =~ s/\r\n$/\n/; # strip CRs
853 15 push @out, $line;
854 }
855  
856 2 waitpid $pid, 0;
857 }
858 elsif( _is_macos_classic ) {
859 2 @out = `$command \xb7 Dev:Stdout`;
860 }
861 else {
862 15 @out = `$command 2>&1`;
863 }
864  
865 4 @out = grep !/^Using.*blib$/, @out;
866 4 return ($?, @out);
867 }
868  
869 18 return 'Stepping on toes is what Schwerns do best! *poing poing poing*';
« blib/lib/B/Module/Info.pm ↑ Summary