| 1 |
#!/usr/bin/perl -w |
| 2 |
|
| 3 |
=head1 NAME |
| 4 |
|
| 5 |
module_info - find information about modules |
| 6 |
|
| 7 |
=head1 SYNOPSIS |
| 8 |
|
| 9 |
module_info [B<-a>] [B<-s>] [B<-p>] [B<-m>] MODULE|FILE... |
| 10 |
|
| 11 |
=head1 DESCRIPTION |
| 12 |
|
| 13 |
List information about the arguments (either module names in the form |
| 14 |
C<Module::Name> or paths in the form C<Foo/Bar.pm> or C<foo/bar.pl>). |
| 15 |
|
| 16 |
By default only shows module name, version, directory, absolute path |
| 17 |
and a flag indicating if it is a core module. Additional information |
| 18 |
can be requested through command line switches. |
| 19 |
|
| 20 |
=over 4 |
| 21 |
|
| 22 |
=item B<-s> |
| 23 |
|
| 24 |
Show subroutines created by the module. |
| 25 |
|
| 26 |
=item B<-p> |
| 27 |
|
| 28 |
Show packages created by the module. |
| 29 |
|
| 30 |
=item B<-m> |
| 31 |
|
| 32 |
Show modules C<use()>d by the module. |
| 33 |
|
| 34 |
=item B<-a> |
| 35 |
|
| 36 |
Equivalent to C<-s -p -m>. |
| 37 |
|
| 38 |
=back |
| 39 |
|
| 40 |
=head1 AUTHOR |
| 41 |
|
| 42 |
Mattia Barbon <[email protected]> |
| 43 |
|
| 44 |
=head1 SEE ALSO |
| 45 |
|
| 46 |
L<Module::Info> |
| 47 |
|
| 48 |
=cut |
| 49 |
|
| 50 |
use strict; |
| 51 |
use Module::Info; |
| 52 |
use Getopt::Long; |
| 53 |
|
| 54 |
my( $show_subroutines, $show_modules, $show_packages, $show_all, $help ); |
| 55 |
|
| 56 |
GetOptions( 's' => \$show_subroutines, |
| 57 |
'p' => \$show_packages, |
| 58 |
'm' => \$show_modules, |
| 59 |
'a' => \$show_all, |
| 60 |
'h' => \$help, |
| 61 |
); |
| 62 |
|
| 63 |
if( $help || !@ARGV ) { |
| 64 |
print <<EOT; |
| 65 |
Usage: module_info [-a] [-s] [-p] [-m] MODULE|FILE... |
| 66 |
|
| 67 |
-a Equivalent to -s -p -m |
| 68 |
-h Help message |
| 69 |
-m Show modules use()d by the module |
| 70 |
-p Show packages created by the module |
| 71 |
-s Show subroutines created by the module |
| 72 |
EOT |
| 73 |
exit 0; |
| 74 |
} |
| 75 |
|
| 76 |
$show_subroutines ||= $show_all; |
| 77 |
$show_modules ||= $show_all; |
| 78 |
$show_packages ||= $show_all; |
| 79 |
|
| 80 |
my $some_error = 0; |
| 81 |
|
| 82 |
foreach my $module (@ARGV) { |
| 83 |
my $info; |
| 84 |
|
| 85 |
if( -f $module ) { |
| 86 |
$info = Module::Info->new_from_file($module); |
| 87 |
} |
| 88 |
else { |
| 89 |
$info = Module::Info->new_from_module($module); |
| 90 |
} |
| 91 |
|
| 92 |
unless( $info ) { |
| 93 |
warn "Can't create Module::Info object for module '$module'"; |
| 94 |
$some_error = 1; |
| 95 |
next; |
| 96 |
} |
| 97 |
|
| 98 |
$info->die_on_compilation_error(1); |
| 99 |
|
| 100 |
my $name = $info->name || $module; |
| 101 |
my $version = $info->version || '(unknown)'; |
| 102 |
my $dir = $info->inc_dir; |
| 103 |
my $file = $info->file; |
| 104 |
my $is_core = $info->is_core ? "yes" : "no"; |
| 105 |
|
| 106 |
print <<EOT; |
| 107 |
|
| 108 |
Name: $name |
| 109 |
Version: $version |
| 110 |
Directory: $dir |
| 111 |
File: $file |
| 112 |
Core module: $is_core |
| 113 |
EOT |
| 114 |
|
| 115 |
my %packages_created; |
| 116 |
my @modules_used; |
| 117 |
my %subroutines; |
| 118 |
|
| 119 |
eval { |
| 120 |
@modules_used = $info->modules_used if $show_modules; |
| 121 |
%packages_created = $info->package_versions if $show_packages; |
| 122 |
%subroutines = $info->subroutines if $show_subroutines; |
| 123 |
}; |
| 124 |
if( $@ ) { |
| 125 |
warn "Compilation failed for module '$module'"; |
| 126 |
$some_error = 1; |
| 127 |
next; |
| 128 |
} |
| 129 |
|
| 130 |
########################################################################### |
| 131 |
# Modules used |
| 132 |
########################################################################### |
| 133 |
if( $show_modules ) { |
| 134 |
print "\nModules used:\n"; |
| 135 |
foreach my $m (sort @modules_used) { |
| 136 |
print " $m\n"; |
| 137 |
} |
| 138 |
print " (none)\n" unless @modules_used; |
| 139 |
} |
| 140 |
|
| 141 |
########################################################################### |
| 142 |
# Packages defined |
| 143 |
########################################################################### |
| 144 |
if( $show_packages ) { |
| 145 |
print "\nPackages created:\n"; |
| 146 |
foreach my $p (sort keys %packages_created) { |
| 147 |
print " $p\t"; |
| 148 |
print +( defined( $packages_created{$p} ) ? |
| 149 |
$packages_created{$p} : |
| 150 |
'(no version)' ); |
| 151 |
print "\n"; |
| 152 |
} |
| 153 |
print " (none)\n" unless keys %packages_created; |
| 154 |
} |
| 155 |
|
| 156 |
########################################################################### |
| 157 |
# Subroutines |
| 158 |
########################################################################### |
| 159 |
if( $show_subroutines ) { |
| 160 |
print "\nSubroutines defined:\n"; |
| 161 |
{ |
| 162 |
my @subroutines = |
| 163 |
sort { ( $a->[0] cmp $b->[0] ) || ( $a->[1] cmp $b->[1] ) } |
| 164 |
map { |
| 165 |
my($package, $subname) = ($_ =~ m/^(.*)?::(\w+)$/); |
| 166 |
warn "Strange subroutine '$_'" |
| 167 |
unless $package || $subname; |
| 168 |
$package ||= '(unknown)'; |
| 169 |
$subname ||= '(unknown)'; |
| 170 |
[ $package, $subname ]; |
| 171 |
} keys %subroutines; |
| 172 |
my $prev_package = ':'; # impossible |
| 173 |
foreach my $s (@subroutines) { |
| 174 |
my($package, $subname) = @$s; |
| 175 |
if($prev_package ne $package) { |
| 176 |
$prev_package = $package; |
| 177 |
print " $package\n"; |
| 178 |
} |
| 179 |
|
| 180 |
print " $subname\n"; |
| 181 |
} |
| 182 |
} |
| 183 |
print " (none)\n" unless %subroutines; |
| 184 |
} |
| 185 |
} |
| 186 |
|
| 187 |
exit $some_error; |