File Coverage

blib/lib/Module/Info.pm
Criterion Covered Total %
statement 250 275 90.9
branch 59 88 67.0
condition 19 37 51.3
subroutine 47 49 95.9
pod 21 21 100.0
total 396 470 84.2


line stmt bran cond sub pod time code
1             package Module::Info;
2              
3 6     6   52910 use 5.006;
  6         21  
4 6     5   72 use strict;
  5         9  
  5         115  
5 5     5   35 use warnings;
  5         9  
  5         161  
6 5     5   27 use Carp;
  5         9  
  5         435  
7 5     5   27 use File::Spec;
  5         9  
  5         144  
8 5     5   22 use Config;
  5         10  
  5         19579  
9              
10 5     5   2828 my $has_version_pm = eval 'use version; 1';
  5         8745  
  5         32  
11              
12             our $AUTOLOAD;
13             our $VERSION;
14              
15 5     5   29 $VERSION = eval 'use version; 1' ? 'version'->new('0.37') : '0.37';
  5         41  
  5         20  
16             $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
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             sub new_from_file {
87 22     22 1 1279 my($proto, $file) = @_;
88 22   33     139 my($class) = ref $proto || $proto;
89              
90 22 100       556 return unless -r $file;
91              
92 21         50 my $self = {};
93 21         553 $self->{file} = File::Spec->rel2abs($file);
94 21         61 $self->{dir} = '';
95 21         58 $self->{name} = '';
96 21         51 $self->{safe} = 0;
97 21         45 $self->{use_version} = 0;
98              
99 21         77 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.
110              
111             If you give your own @INC, that will be used to search instead.
112              
113             =cut
114              
115             sub new_from_module {
116 13     13 1 2375 my($class, $module, @inc) = @_;
117 13         62 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             sub new_from_loaded {
130 2     2 1 757 my($class, $name) = @_;
131              
132 2         13 my $mod_file = join('/', split('::', $name)) . '.pm';
133 2   100     28 my $filepath = $INC{$mod_file} || '';
134              
135 2 100       8 my $module = Module::Info->new_from_file($filepath) or return;
136 1         5 $module->{name} = $name;
137 1         56 ($module->{dir} = $filepath) =~ s|/?\Q$mod_file\E$||;
138 1         21 $module->{dir} = File::Spec->rel2abs($module->{dir});
139 1         5 $module->{safe} = 0;
140 1         3 $module->{use_version} = 0;
141              
142 1         8 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 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.
153              
154             =cut
155              
156             sub all_installed {
157 2     2 1 10 my($class, $module, @inc) = @_;
158 2         9 return $class->_find_all_installed($module, 0, @inc);
159             }
160              
161             # Thieved from Module::InstalledVersion
162             sub _find_all_installed {
163 15     15   44 my($proto, $name, $find_first_one, @inc) = @_;
164 15   33     100 my($class) = ref $proto || $proto;
165              
166 15 100       156 @inc = @INC unless @inc;
167 15         258 my $file = File::Spec->catfile(split /::/, $name) . '.pm';
168              
169 15         47 my @modules = ();
170 15         43 DIR: foreach my $dir (@inc) {
171             # Skip the new code ref in @INC feature.
172 37 100       107 next if ref $dir;
173              
174 36         429 my $filename = File::Spec->catfile($dir, $file);
175 36 100       1034 if( -r $filename ) {
176 17         68 my $module = $class->new_from_file($filename);
177 17         231 $module->{dir} = File::Spec->rel2abs($dir);
178 17         39 $module->{name} = $name;
179 17         108 push @modules, $module;
180 17 100       74 last DIR if $find_first_one;
181             }
182             }
183              
184 15         89 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
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             sub name {
210 9     9 1 3590 my($self) = shift;
211              
212 9 100       41 $self->{name} = shift if @_;
213 9         45 return $self->{name};
214             }
215              
216             =item B
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             sub version {
227 14     14 1 43 my($self) = shift;
228 14         43 local($_, *MOD);
229              
230 14         47 my $parsefile = $self->file;
231 14         48 my $safe = $self->safe;
232              
233 14 50       834 open(MOD, $parsefile) or die $!;
234              
235 14         32 my $inpod = 0;
236 14         18 my $result;
237 14         279 while () {
238 108 50       382 $inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
    50          
239 108 100 66     568 next if $inpod || /^\s*#/;
240              
241 103         151 chomp;
242             # taken from ExtUtils::MM_Unix 6.63_02
243 103 50       315 next if /^\s*(if|unless|elsif)/;
244 103 100       320 if (m{^\s*package\s+\w[\w\:\']*\s+(v?[0-9._]+)\s*;}) {
245 1         3 local $^W = 0;
246 1         3 $result = $1;
247 1         3 last;
248             }
249 102 100       482 next unless /([\$*])(([\w\:\']*)\bVERSION)\b.*\=/;
250 13 100       151 my $eval = sprintf qq{
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 13         61 local $^W = 0;
260 13         104 $result = $self->_eval($eval);
261 13 50 33     1508 warn "Could not eval '$eval' in $parsefile: $@" if $@ && !$safe;
262 13 100       40 $result = "undef" unless defined $result;
263 13         38 last;
264             }
265 14         224 close MOD;
266 14 100 33     44 $result = 'version'->new($result) # quotes for 5.004
      66        
267             if $self->use_version
268             && (!ref($result) || !UNIVERSAL::isa($result, "version"));
269 14         113 return $result;
270             }
271              
272              
273             =item B
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             sub inc_dir {
283 8     8 1 21 my($self) = shift;
284              
285 8         147 return $self->{dir};
286             }
287              
288             =item B
289              
290             my $file = $module->file;
291              
292             The absolute path to this module.
293              
294             =cut
295              
296             sub file {
297 44     44 1 135 my($self) = shift;
298              
299 44         260 return $self->{file};
300             }
301              
302             =item B
303              
304             my $is_core = $module->is_core;
305              
306             Checks if this module is the one distributed with Perl.
307              
308             B 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             sub is_core {
315 5     5 1 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 5         362 $Config{privlib});
322             }
323              
324             =item B
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             sub has_pod {
335 0     0 1 0 my $self = shift;
336              
337 0         0 my $filename = $self->file;
338            
339 0 0       0 open my $file, $filename or return; # the file won't even open
340            
341 0         0 while( <$file> ) {
342 0 0       0 return $filename if /^=[a-z]/;
343             }
344              
345             # nothing found? Try a companion POD file
346              
347 0 0       0 $filename =~ s/\.[^.]+$/.pod/ or return;
348              
349 0 0       0 return unless -f $filename;
350              
351 0 0       0 open $file, $filename or return;
352            
353 0         0 while( <$file> ) {
354 0 0       0 return $filename if /^=[a-z]/;
355             }
356            
357 0         0 return;
358             }
359              
360             =back
361              
362             =head2 Information that requires loading.
363              
364             B 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
375              
376             my @packages = $module->packages_inside;
377              
378             Looks for any explicit C 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 Currently doesn't spot package changes inside subroutines.
383              
384             =cut
385              
386             sub packages_inside {
387 7     7 1 403 my $self = shift;
388              
389 7         41 my %packs = map {$_, 1} $self->_call_B('packages');
  15         141  
390 6         120 return keys %packs;
391             }
392              
393             =item B
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), and
399             whose values are the versions of those packages.
400              
401             =cut
402              
403             sub package_versions {
404 2     2 1 2547 my $self = shift;
405              
406 2         14 my @packs = $self->packages_inside;
407              
408             # To survive the print(), we translate undef into '~' and then back again.
409 2         27 (my $quoted_file = $self->file) =~ s/(['\\])/\\$1/g;
410 2         12 my $command = qq{-le "require '$quoted_file';};
411 2         12 foreach (@packs) {
412 5         22 $command .= " print defined $_->VERSION ? $_->VERSION : '~';"
413             }
414 2         7 $command .= qq{"};
415              
416 2         28 my ($status, @versions) = $self->_call_perl($command);
417 2         28 chomp @versions;
418 2         26 foreach (@versions) {
419 5 100       42 $_ = undef if $_ eq '~';
420             }
421              
422 2         11 my %map;
423 2         32 @map{@packs} = @versions;
424              
425 2         76 return %map;
426             }
427              
428              
429             =item B
430              
431             my @used = $module->modules_used;
432              
433             Returns a list of all modules and files which may be C'd or
434             C'd by this module.
435              
436             B These modules may be conditionally loaded, can't tell. Also
437             can't find modules which might be used inside an C.
438              
439             =cut
440              
441             sub modules_used {
442 3     3 1 4915 my($self) = shift;
443 3         30 my %used = $self->modules_required;
444              
445 3         109 return keys %used;
446             }
447              
448             =item B
449              
450             my %required = $module->modules_required;
451              
452             Returns a list of all modules and files which may be C'd or
453             C'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 These modules may be conditionally loaded, can't tell. Also
460             can't find modules which might be used inside an C.
461              
462             =cut
463              
464             sub modules_required {
465 4     4 1 25 my($self) = shift;
466              
467 4         30 my $mod_file = $self->file;
468 4         25 my @mods = $self->_call_B('modules_used');
469              
470 4         41 my @used_mods = ();
471 4         39 my %used_mods = ();
472 4   66     546 for (grep /^use \D/ && /at "\Q$mod_file\E" /, @mods) {
473 18         162 my($file, $version) = /^use (\S+) \(([^\)]*)\)/;
474 18   100     84 $used_mods{_file2mod($file)} ||= [];
475 18 100 66     199 next unless defined $version and length $version;
476              
477 6         33 push @{$used_mods{_file2mod($file)}}, $version;
  6         70  
478             }
479              
480 4         46 push @used_mods, map { my($file) = /^require bare (\S+)/; _file2mod($file) }
  8         56  
  8         34  
481             grep /^require bare \D/ , @mods;
482              
483 4         50 push @used_mods, map { /^require not bare (\S+)/; $1 }
  3         25  
  3         30  
484             grep /^require not bare \D/, @mods;
485              
486 4         26 foreach ( @used_mods ) { $used_mods{$_} = [] };
  11         98  
487 4         94 return %used_mods;
488             }
489              
490             sub _file2mod {
491 32     32   93 my($mod) = shift;
492 32         116 $mod =~ s/\.pm//;
493 32         83 $mod =~ s|/|::|g;
494 32         292 return $mod;
495             }
496              
497              
498             =item B
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 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 as line 1, Module::Info will report line 3 as the
522             start and line 4 as the end. C is a compile-time
523             statement. Again, this will change as perl changes.
524              
525             Note this only catches simple C subroutine
526             declarations. Anonymous, autoloaded or eval'd subroutines are not
527             listed.
528              
529             =cut
530              
531             sub subroutines {
532 2     2 1 3268 my($self) = shift;
533              
534 2         43 my $mod_file = $self->file;
535 2         18 my @subs = $self->_call_B('subroutines');
536 2         4417 return map { /^(\S+) at "[^"]+" from (\d+) to (\d+)/;
  35         215  
537 35         677 ($1 => { start => $2, end => $3 }) }
538             grep /at "\Q$mod_file\E" /, @subs;
539             }
540              
541 16     16   55 sub _get_extra_arguments { '' }
542              
543             sub _call_B {
544 15     15   71 my($self, $arg) = @_;
545              
546 15         75 my $mod_file = $self->file;
547 15         73 my $extra_args = $self->_get_extra_arguments;
548 15         74 my $command = qq{$extra_args "-MO=Module::Info,$arg" "$mod_file"};
549 15         194 my($status, @out) = $self->_call_perl($command);
550              
551 15 100       371 if( $status ) {
552 2         22 my $exit = $status >> 8;
553 2         30 my $msg = join "\n",
554             "B::Module::Info,$arg use failed with $exit saying:",
555             @out;
556              
557 2 100       37 if( $self->{die_on_compilation_error} ) {
558 1         81 die $msg;
559             }
560             else {
561 1         70 warn $msg;
562 1         48 return;
563             }
564             }
565              
566 13         1872 @out = grep !/syntax OK$/, @out;
567 13         544 chomp @out;
568 13         1150 return @out;
569             }
570              
571              
572             =item B
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 superclasses() is currently cheating. See L below.
580              
581             =cut
582              
583             sub superclasses {
584 1     1 1 15 my $self = shift;
585              
586 1         11 my $mod_file = $self->file;
587 1         10 my $mod_name = $self->name;
588 1 50       6 unless( $mod_name ) {
589 0         0 carp 'isa() requires $module->name to be set';
590 0         0 return;
591             }
592              
593 1         12 my $extra_args = $self->_get_extra_arguments;
594 1         12 my $command =
595             qq{-e "require q{$mod_file}; print join qq{\\n}, \@$mod_name\::ISA"};
596 1         22 my($status, @isa) = $self->_call_perl("$extra_args $command");
597 1         24 chomp @isa;
598 1         41 return @isa;
599             }
600              
601             =item B
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             sub subroutines_called {
623 2     2 1 1618 my($self) = shift;
624              
625 2         27 my @subs = $self->_call_B('subs_called');
626 2         104 my $mod_file = $self->file;
627              
628 2         2563 @subs = grep /at "\Q$mod_file\E" line/, @subs;
629 2         12 my @out = ();
630 2         16 foreach (@subs) {
631 26         73 my %info = ();
632 26         233 ($info{type}) = /^(.+) call/;
633 26 100       142 $info{type} = 'symbolic function' if /using symbolic ref/;
634 26         132 ($info{'name'}) = /to (\S+)/;
635 26         114 ($info{class})= /via (\S+)/;
636 26         160 ($info{line}) = /line (\d+)/;
637 26         112 push @out, \%info;
638             }
639 2         125 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
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             sub dynamic_method_calls {
664 1     1 1 2740 my($self) = shift;
665 1         6 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
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 Cs with the same error message it would use
687             in the warning.
688              
689             =cut
690              
691             sub die_on_compilation_error {
692 2     2 1 372 my($self) = shift;
693              
694 2 50       27 $self->{die_on_compilation_error} = $_[0] ? 1 : 0 if @_;
    50          
695 2         6 return $self->{die_on_compilation_error};
696             }
697              
698             =item B
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 method
706             executes its code in a C compartment.
707              
708             =cut
709              
710             sub safe {
711 50     50 1 122 my($self) = shift;
712              
713 50 100       179 if( @_ ) {
714 2 50       10 $self->{safe} = $_[0] ? 1 : 0;
715 2 50       1118 require Safe if $self->{safe};
716             }
717 50         50198 return $self->{safe};
718             }
719              
720             sub AUTOLOAD {
721 31 100   31   216 my($super) = $_[0]->safe ? 'Module::Info::Safe' : 'Module::Info::Unsafe';
722 31         104 my($method) = $AUTOLOAD;
723 31         296 $method =~ s/^.*::([^:]+)$/$1/;
724              
725 31 50       133 return if $method eq 'DESTROY';
726              
727 31         376 my($code) = $super->can($method);
728              
729 31 50       113 die "Can not find method '$method' in Module::Info" unless $code;
730              
731 31         220 goto &$code;
732             }
733              
734             =item B
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             sub use_version {
746 18     18 1 40 my($self) = shift;
747              
748 18 100       50 if( @_ ) {
749 2 50 33     12 die "Can not use 'version.pm' as requested"
750             if $_[0] && !$has_version_pm;
751              
752 2 50       13 $self->{use_version} = $_[0] ? 1 : 0;
753             }
754              
755 18         129 return $self->{use_version};
756             }
757              
758             =back
759              
760             =head1 REPOSITORY
761              
762             L
763              
764             =head1 AUTHOR
765              
766             Michael G Schwern with code from ExtUtils::MM_Unix,
767             Module::InstalledVersion and lots of cargo-culting from B::Deparse.
768              
769             Mattia Barbon maintained
770             the module from 2002 to 2013.
771              
772             Neil Bowers 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             my $root = 'Module::Info::Safe::_safe';
805              
806             sub _create_compartment {
807 2     2   14 my $safe = Safe->new( $root );
808              
809 2         2821 $safe->permit_only( qw(:base_orig :base_core) );
810              
811 2         26 return $safe;
812             }
813              
814             sub _eval {
815 2     2   7 my($self, $code) = @_;
816 2   33     15 $self->{compartment} ||= _create_compartment;
817              
818 2         10 return $self->{compartment}->reval( $code, 0 )
819             }
820              
821             sub _call_perl {
822 0     0   0 die "Module::Info attemped an unsafe operation while in 'safe' mode.";
823             }
824              
825             package Module::Info::Unsafe;
826              
827 3     3   28 sub _eval { eval($_[1]) }
  3     3   5  
  3     2   175  
  3     2   15  
  3     2   6  
  3     2   100  
  2     2   21  
  2     2   60  
  2     11   208  
  2         16  
  2         6  
  2         59  
  2         14  
  2         5  
  2         126  
  2         14  
  2         4  
  2         53  
  2         11  
  2         3  
  2         91  
  2         10  
  2         4  
  2         52  
  11         923  
828              
829             sub _is_win95() {
830 18   33 18   132 return $^O eq 'MSWin32' && (Win32::GetOSVersion())[4] == 1;
831             }
832              
833             sub _is_macos_classic() {
834 36     36   283 return $^O eq 'MacOS';
835             }
836              
837             sub _call_perl {
838 18     18   54 my($self, $args) = @_;
839              
840 18 50       69 my $perl = _is_macos_classic ? 'perl' : $^X;
841 18         74 my $command = "$perl $args";
842 18         44 my @out;
843              
844 18 50       61 if( _is_win95 ) {
    50          
845 0         0 require IPC::Open3;
846 0         0 local *OUTFH;
847 0         0 my($line, $in);
848 0         0 my $out = \*OUTFH;
849 0         0 my $pid = IPC::Open3::open3($in, $out, $out, $command);
850 0         0 close $in;
851 0         0 while( defined($line = ) ) {
852 0         0 $line =~ s/\r\n$/\n/; # strip CRs
853 0         0 push @out, $line;
854             }
855              
856 0         0 waitpid $pid, 0;
857             }
858             elsif( _is_macos_classic ) {
859 0         0 @out = `$command \xb7 Dev:Stdout`;
860             }
861             else {
862 18         4106038 @out = `$command 2>&1`;
863             }
864              
865 18         3142 @out = grep !/^Using.*blib$/, @out;
866 18         1744 return ($?, @out);
867             }
868              
869             return 'Stepping on toes is what Schwerns do best! *poing poing poing*';