15
Module::Info - Information about Perl modules
21
my $mod = Module::Info->new_from_file('Some/Module.pm');
22
my $mod = Module::Info->new_from_module('Some::Module');
23
my $mod = Module::Info->new_from_loaded('Some::Module');
25
my @mods = Module::Info->all_installed('Some::Module');
27
my $name = $mod->name;
28
my $version = $mod->version;
29
my $dir = $mod->inc_dir;
30
my $file = $mod->file;
31
my $is_core = $mod->is_core;
33
# Only available in perl 5.6.1 and up.
34
# These do compile the module.
35
my @packages = $mod->packages_inside;
36
my @used = $mod->modules_used;
37
my @subs = $mod->subroutines;
38
my @isa = $mod->superclasses;
39
my @calls = $mod->subroutines_called;
41
# Check for constructs which make perl hard to predict.
42
my @methods = $mod->dynamic_method_calls;
43
my @lines = $mod->eval_string; *UNIMPLEMENTED*
44
my @lines = $mod->gotos; *UNIMPLEMENTED*
45
my @controls = $mod->exit_via_loop_control; *UNIMPLEMENTED*
46
my @unpredictables = $mod->has_unpredictables; *UNIMPLEMENTED*
48
# set/get Module::Info options
49
$self->die_on_compilation_error(1);
50
my $die_on_error = $mod->die_on_compilation_error;
54
Module::Info gives you information about Perl modules B<without
55
actually loading the module>. It actually isn't specific to modules
56
and should work on any perl code.
62
There are a few ways to specify which module you want information for.
63
They all return Module::Info objects.
69
my $module = Module::Info->new_from_file('path/to/Some/Module.pm');
71
Given a file, it will interpret this as the module you want
72
information about. You can also hand it a perl script.
74
If the file doesn't exist or isn't readable it will return false.
79
my($proto, $file) = @_;
80
my($class) = ref $proto || $proto;
82
return unless -r $file;
85
$self->{file} = File::Spec->rel2abs($file);
89
return bless $self, $class;
94
my $module = Module::Info->new_from_module('Some::Module');
95
my $module = Module::Info->new_from_module('Some::Module', @INC);
97
Given a module name, @INC will be searched and the first module found
98
used. This is the same module that would be loaded if you just say
101
If you give your own @INC, that will be used to search instead.
105
sub new_from_module {
106
my($class, $module, @inc) = @_;
107
return ($class->_find_all_installed($module, 1, @inc))[0];
110
=item new_from_loaded
112
my $module = Module::Info->new_from_loaded('Some::Module');
114
Gets information about the currently loaded version of Some::Module.
115
If it isn't loaded, returns false.
119
sub new_from_loaded {
120
my($class, $name) = @_;
122
my $mod_file = join('/', split('::', $name)) . '.pm';
123
my $filepath = $INC{$mod_file} || '';
125
my $module = Module::Info->new_from_file($filepath);
126
$module->{name} = $name;
127
($module->{dir} = $filepath) =~ s|/?\Q$mod_file\E$||;
128
$module->{dir} = File::Spec->rel2abs($module->{dir});
135
my @modules = Module::Info->all_installed('Some::Module');
136
my @modules = Module::Info->all_installed('Some::Module', @INC);
138
Like new_from_module(), except I<all> modules in @INC will be
139
returned, in the order they are found. Thus $modules[0] is the one
140
that would be loaded by C<use Some::Module>.
145
my($class, $module, @inc) = @_;
146
return $class->_find_all_installed($module, 0, @inc);
149
# Thieved from Module::InstalledVersion
150
sub _find_all_installed {
151
my($proto, $name, $find_first_one, @inc) = @_;
152
my($class) = ref $proto || $proto;
154
@inc = @INC unless @inc;
155
my $file = File::Spec->catfile(split /::/, $name) . '.pm';
158
DIR: foreach my $dir (@inc) {
159
# Skip the new code ref in @INC feature.
162
my $filename = File::Spec->catfile($dir, $file);
164
my $module = $class->new_from_file($filename);
165
$module->{dir} = File::Spec->rel2abs($dir);
166
$module->{name} = $name;
167
push @modules, $module;
168
last DIR if $find_first_one;
178
=head2 Information without loading
180
The following methods get their information without actually compiling
187
my $name = $module->name;
188
$module->name($name);
190
Name of the module (ie. Some::Module).
192
Module loaded using new_from_file() won't have this information in
193
which case you can set it yourself.
200
$self->{name} = shift if @_;
201
return $self->{name};
206
my $version = $module->version;
208
Divines the value of $VERSION. This uses the same method as
209
ExtUtils::MakeMaker and all caveats therein apply.
213
# Thieved from ExtUtils::MM_Unix 1.12603
217
my $parsefile = $self->file;
219
open(MOD, $parsefile) or die $!;
224
$inpod = /^=(?!cut)/ ? 1 : /^=cut/ ? 0 : $inpod;
225
next if $inpod || /^\s*#/;
228
next unless /([\$*])(([\w\:\']*)\bVERSION)\b.*\=/;
230
package Module::Info::_version;
239
$result = eval($eval);
240
warn "Could not eval '$eval' in $parsefile: $@" if $@;
241
$result = "undef" unless defined $result;
251
my $dir = $module->inc_dir;
253
Include directory in which this module was found. Module::Info
254
objects created with new_from_file() won't have this info.
266
my $file = $module->file;
268
The absolute path to this module.
275
return $self->{file};
280
my $is_core = $module->is_core;
282
Checks if this module is the one distributed with Perl.
284
B<NOTE> This goes by what directory it's in. It's possible that the
285
module has been altered or upgraded from CPAN since the original Perl
293
return scalar grep $self->{dir} eq File::Spec->canonpath($_),
294
($Config{installarchlib},
295
$Config{installprivlib});
300
=head2 Information that requires loading.
302
B<WARNING!> From here down reliability drops rapidly!
304
The following methods get their information by compiling the module
305
and examining the opcode tree. The module will be compiled in a
306
seperate process so as not to disturb the current program.
308
They will only work on 5.6.1 and up and requires the B::Utils module.
312
=item B<packages_inside>
314
my @packages = $module->packages_inside;
316
Looks for any explicit C<package> declarations inside the module and
317
returns a list. Useful for finding hidden classes and functionality
318
(like Tie::StdHandle inside Tie::Handle).
320
B<KNOWN BUG> Currently doesn't spot package changes inside subroutines.
324
sub packages_inside {
327
my %packs = map {$_, 1} $self->_call_B('packages');
331
=item B<package_versions>
333
my %versions = $module->package_versions;
335
Returns a hash whose keys are the packages contained in the module
336
(these are the same as what's returned by C<packages_inside()>), and
337
whose values are the versions of those packages.
341
sub package_versions {
344
my @packs = $self->packages_inside;
346
# To survive the print(), we translate undef into '~' and then back again.
347
(my $quoted_file = $self->file) =~ s/(['\\])/\\$1/g;
348
my $command = qq{-le "require '$quoted_file';};
350
$command .= " print defined $_->VERSION ? $_->VERSION : '~';"
354
my ($status, @versions) = $self->_call_perl($command);
356
foreach (@versions) {
357
$_ = undef if $_ eq '~';
361
@map{@packs} = @versions;
367
=item B<modules_used>
369
my @used = $module->modules_used;
371
Returns a list of all modules and files which may be C<use>'d or
372
C<require>'d by this module.
374
B<NOTE> These modules may be conditionally loaded, can't tell. Also
375
can't find modules which might be used inside an C<eval>.
381
my %used = $self->modules_required;
386
=item B<modules_required>
388
my %required = $module->modules_required;
390
Returns a list of all modules and files which may be C<use>'d or
391
C<require>'d by this module, together with the minimum required version.
393
The hash is keyed on the module/file name, the corrisponding value is
394
an array reference containing the requied versions, or an empty array
395
if no specific version was required.
397
B<NOTE> These modules may be conditionally loaded, can't tell. Also
398
can't find modules which might be used inside an C<eval>.
402
sub modules_required {
405
my $mod_file = $self->file;
406
my @mods = $self->_call_B('modules_used');
410
for (grep /^use \D/ && /at "\Q$mod_file\E" /, @mods) {
411
my($file, $version) = /^use (\S+) \(([^\)]*)\)/;
412
$used_mods{_file2mod($file)} ||= [];
413
next unless defined $version and length $version;
415
push @{$used_mods{_file2mod($file)}}, $version;
418
push @used_mods, map { my($file) = /^require bare (\S+)/; _file2mod($file) }
419
grep /^require bare \D/ , @mods;
421
push @used_mods, map { /^require not bare (\S+)/; $1 }
422
grep /^require not bare \D/, @mods;
424
$used_mods{$_} = [] for @used_mods;
438
my %subs = $module->subroutines;
440
Returns a hash of all subroutines defined inside this module and some
441
info about it. The key is the *full* name of the subroutine
442
(ie. $subs{'Some::Module::foo'} rather than just $subs{'foo'}), value
443
is a hash ref with information about the subroutine like so:
445
start => line number of the first statement in the subroutine
446
end => line number of the last statement in the subroutine
448
Note that the line numbers may not be entirely accurate and will
449
change as perl's backend compiler improves. They typically correspond
450
to the first and last I<run-time> statements in a subroutine. For
459
Taking C<sub foo {> as line 1, Module::Info will report line 3 as the
460
start and line 4 as the end. C<package Wibble;> is a compile-time
461
statement. Again, this will change as perl changes.
463
Note this only catches simple C<sub foo {...}> subroutine
464
declarations. Anonymous, autoloaded or eval'd subroutines are not
472
my $mod_file = $self->file;
473
my @subs = $self->_call_B('subroutines');
474
return map { /^(\S+) at "[^"]+" from (\d+) to (\d+)/;
475
($1 => { start => $2, end => $3 }) }
476
grep /at "\Q$mod_file\E" /, @subs;
480
return $^O eq 'MSWin32' && Win32::GetOSVersion() == 1;
483
sub _is_macos_classic() {
484
return $^O eq 'MacOS';
487
sub _get_extra_arguments { '' }
490
my($self, $args) = @_;
492
my $perl = _is_macos_classic ? 'perl' : $^X;
493
my $command = "$perl $args";
501
my $pid = IPC::Open3::open3($in, $out, $out, $command);
503
while( defined($line = <OUTFH>) ) {
504
$line =~ s/\r\n$/\n/; # strip CRs
510
elsif( _is_macos_classic ) {
511
@out = `$command \xb7 Dev:Stdout`;
514
@out = `$command 2>&1`;
517
@out = grep !/^Using.*blib$/, @out;
522
my($self, $arg) = @_;
524
my $mod_file = $self->file;
525
my $extra_args = $self->_get_extra_arguments;
526
my $command = qq{$extra_args "-MO=Module::Info,$arg" "$mod_file"};
527
my($status, @out) = $self->_call_perl($command);
530
my $exit = $status >> 8;
532
"B::Module::Info,$arg use failed with $exit saying:",
535
if( $self->{die_on_compilation_error} ) {
544
@out = grep !/syntax OK$/, @out;
550
=item B<superclasses>
552
my @isa = $module->superclasses;
554
Returns the value of @ISA for this $module. Requires that
555
$module->name be set to work.
557
B<NOTE> superclasses() is currently cheating. See L<CAVEATS> below.
564
my $mod_file = $self->file;
565
my $mod_name = $self->name;
566
unless( $mod_name ) {
567
carp 'isa() requires $module->name to be set';
571
my $extra_args = $self->_get_extra_arguments;
573
qq{-e "require q{$mod_file}; print join qq{\\n}, \@$mod_name\::ISA"};
574
my($status, @isa) = $self->_call_perl("$extra_args $command");
579
=item B<subroutines_called>
581
my @calls = $module->subroutines_called;
583
Finds all the methods and functions which are called inside the
586
Returns a list of hashes. Each hash represents a single function or
587
method call and has the keys:
589
line line number where this call originated
590
class class called on if its a class method
591
type function, symbolic function, object method,
592
class method, dynamic object method or
593
dynamic class method.
594
(NOTE This format will probably change)
595
name name of the function/method called if not dynamic
600
sub subroutines_called {
603
my @subs = $self->_call_B('subs_called');
604
my $mod_file = $self->file;
606
@subs = grep /at "\Q$mod_file\E" line/, @subs;
610
($info{type}) = /^(.+) call/;
611
$info{type} = 'symbolic function' if /using symbolic ref/;
612
($info{'name'}) = /to (\S+)/;
613
($info{class})= /via (\S+)/;
614
($info{line}) = /line (\d+)/;
622
=head2 Information about Unpredictable Constructs
624
Unpredictable constructs are things that make a Perl program hard to
625
predict what its going to do without actually running it. There's
626
nothing wrong with these constructs, but its nice to know where they
627
are when maintaining a piece of code.
631
=item B<dynamic_method_calls>
633
my @methods = $module->dynamic_method_calls;
635
Returns a list of dynamic method calls (ie. C<$obj->$method()>) used
636
by the $module. @methods has the same format as the return value of
637
subroutines_called().
641
sub dynamic_method_calls {
643
return grep $_->{type} =~ /dynamic/, $self->subroutines_called;
650
The following methods get/set specific option values for the
655
=item B<die_on_compilation_error>
657
$module->die_on_compilation_error(0); # default
658
$module->die_on_compilation_error(1);
659
my $flag = $module->die_on_compilation_error;
661
Sets/gets the "die on compilation error" flag. Whne the flag is off
662
(default), and a module fails to compile, Module::Info simply emits a
663
watning and continues. When the flag is on and a module fails to
664
compile, Module::Info die()s with the same error message it would use
669
sub die_on_compilation_error {
672
$self->{die_on_compilation_error} = $_[0] ? 1 : 0 if @_;
673
return $self->{die_on_compilation_error};
680
Michael G Schwern <schwern@pobox.com> with code from ExtUtils::MM_Unix,
681
Module::InstalledVersion and lots of cargo-culting from B::Deparse.
683
Mattia Barbon <MBARBON@cpan.org> is the current maintainer.
687
This program is free software; you can redistribute it and/or
688
modify it under the same terms as Perl itself.
692
Many thanks to Simon Cozens and Robin Houston for letting me chew
697
Code refs in @INC are currently ignored. If this bothers you submit a
700
superclasses() is cheating and just loading the module in a seperate
701
process and looking at @ISA. I don't think its worth the trouble to
702
go through and parse the opcode tree as it still requires loading the
703
module and running all the BEGIN blocks. Patches welcome.
705
I originally was going to call superclasses() isa() but then I
706
remembered that would be bad.
708
All the methods that require loading are really inefficient as they're
709
not caching anything. I'll worry about efficiency later.
713
return 'Stepping on toes is what Schwerns do best! *poing poing poing*';