| 1 |
#!/usr/bin/perl |
| 2 |
use v5.10; |
| 3 |
use utf8; |
| 4 |
|
| 5 |
use open qw(:std :utf8); |
| 6 |
use strict; |
| 7 |
use warnings; |
| 8 |
|
| 9 |
use File::Spec::Functions qw(catfile); |
| 10 |
use Getopt::Std qw(getopts); |
| 11 |
|
| 12 |
=encoding utf8 |
| 13 |
|
| 14 |
=head1 NAME |
| 15 |
|
| 16 |
extract_modules - determine which Perl modules a given file uses |
| 17 |
|
| 18 |
=cut |
| 19 |
|
| 20 |
our $VERSION = '1.104'; |
| 21 |
|
| 22 |
=head1 SYNOPSIS |
| 23 |
|
| 24 |
Given Perl files, extract and report the Perl modules included |
| 25 |
with C<use> or C<require>. |
| 26 |
|
| 27 |
# print a verbose text listing |
| 28 |
$ extract_modules filename [...] |
| 29 |
Modules required by examples/extract_modules: |
| 30 |
- Getopt::Std (first released with Perl 5) |
| 31 |
- Module::CoreList (first released with Perl 5.008009) |
| 32 |
- Pod::Usage (first released with Perl 5.006) |
| 33 |
- strict (first released with Perl 5) |
| 34 |
- warnings (first released with Perl 5.006) |
| 35 |
5 module(s) in core, 0 external module(s) |
| 36 |
|
| 37 |
# print a succint list, one module per line |
| 38 |
$ extract_modules -l filename [...] |
| 39 |
Getopt::Std |
| 40 |
Module::CoreList |
| 41 |
Pod::Usage |
| 42 |
open |
| 43 |
strict |
| 44 |
warnings |
| 45 |
|
| 46 |
# print a succinct list, modules separated by null bytes |
| 47 |
# you might like this with xargs -0 |
| 48 |
$ extract_modules -0 filename [...] |
| 49 |
Getopt::StdModule::CoreListPod::Usageopenstrictwarnings |
| 50 |
|
| 51 |
# print the modules list as JSON |
| 52 |
$ extract_modules -j filename [...] |
| 53 |
[ |
| 54 |
"Getopt::Std", |
| 55 |
"Module::CoreList", |
| 56 |
"Pod::Usage", |
| 57 |
"open", |
| 58 |
"strict", |
| 59 |
"warnings" |
| 60 |
] |
| 61 |
|
| 62 |
# print the modules list as a basic cpanfile |
| 63 |
# https://metacpan.org/pod/cpanfile |
| 64 |
$ extract_modules -c filename [...] |
| 65 |
requires 'Getopt::Std', '1.23'; |
| 66 |
requires 'Module::CoreList'; |
| 67 |
requires 'Pod::Usage'; |
| 68 |
requires 'open'; |
| 69 |
requires 'strict'; |
| 70 |
requires 'warnings'; |
| 71 |
|
| 72 |
# look for the file in PATH |
| 73 |
$ extract_modules -b some_program |
| 74 |
|
| 75 |
=head1 DESCRIPTION |
| 76 |
|
| 77 |
This script does not execute the code in the files it examines. It |
| 78 |
uses the C<Module::Extract::Use> or C<Module::ExtractUse> modules |
| 79 |
which statically analyzes the source without compiling or running it. |
| 80 |
These modules cannot discover modules loaded dynamically through a |
| 81 |
string eval. |
| 82 |
|
| 83 |
=head2 Command-line options |
| 84 |
|
| 85 |
=over 4 |
| 86 |
|
| 87 |
=item * -b look for filename in current directory or in PATH |
| 88 |
|
| 89 |
=item * -c cpanfile output |
| 90 |
|
| 91 |
=item * -e exclude core modules |
| 92 |
|
| 93 |
=item * -j JSON output |
| 94 |
|
| 95 |
=item * -l succinct list, one module per line |
| 96 |
|
| 97 |
=item * -0 succinct list, modules null separated (for xargs -0) |
| 98 |
|
| 99 |
=cut |
| 100 |
|
| 101 |
run(@ARGV) unless caller; |
| 102 |
|
| 103 |
sub run { |
| 104 |
my @args = @_; |
| 105 |
|
| 106 |
my %opts; |
| 107 |
{ |
| 108 |
local @ARGV = @args; |
| 109 |
getopts('bcejl0', \%opts); |
| 110 |
@args = @ARGV; |
| 111 |
} |
| 112 |
|
| 113 |
# if no parameters are passed, give usage information |
| 114 |
unless( @args ) { |
| 115 |
require Pod::Usage; |
| 116 |
Pod::Usage::pod2usage( msg => 'Please supply at least one filename to analyze' ); |
| 117 |
exit; |
| 118 |
} |
| 119 |
|
| 120 |
use Data::Dumper; |
| 121 |
my( $object, $method, $sub ); |
| 122 |
my @classes = qw( Module::Extract::Use Module::ExtractUse ); |
| 123 |
my %methods = ( |
| 124 |
'Module::Extract::Use' => [ 'get_modules_with_details', sub { |
| 125 |
[ $_[0]->module, $_[0]->version ]; |
| 126 |
} ], |
| 127 |
'Module::ExtractUse' => [ 'extract_use', sub { |
| 128 |
say Dumper( \@_ ); |
| 129 |
[ $_[0], undef ]; |
| 130 |
} ], |
| 131 |
); |
| 132 |
|
| 133 |
foreach my $module ( @classes ) { |
| 134 |
eval "require $module"; |
| 135 |
next if $@; |
| 136 |
( $object, $method, $sub ) = ( $module->new, @{ $methods{$module} } ); |
| 137 |
} |
| 138 |
|
| 139 |
die join( |
| 140 |
"\n\t", |
| 141 |
"Install one of these modules to make this program work:", |
| 142 |
sort keys %methods |
| 143 |
) . "\n" |
| 144 |
unless defined $object; |
| 145 |
|
| 146 |
if( $opts{'b'} ) { |
| 147 |
require Config; |
| 148 |
my @dirs = split /\Q$Config::Config{'path_sep'}/, $ENV{'PATH'}; |
| 149 |
|
| 150 |
my @found_args; |
| 151 |
FILE: foreach my $file ( @args ) { |
| 152 |
if( -e $file ) { |
| 153 |
push @found_args, $file; |
| 154 |
next FILE; |
| 155 |
} |
| 156 |
|
| 157 |
foreach my $dir ( @dirs ) { |
| 158 |
print STDERR "Looking in $dir\n"; |
| 159 |
my $path = catfile $dir, $file; |
| 160 |
if( -e $path ) { |
| 161 |
push @found_args, $path; |
| 162 |
next FILE; |
| 163 |
} |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
@args = @found_args; |
| 168 |
} |
| 169 |
|
| 170 |
my @Grand_modules; |
| 171 |
foreach my $file ( @args ) { |
| 172 |
unless ( -r $file ) { |
| 173 |
printf STDERR "Could not read $file\n"; |
| 174 |
next; |
| 175 |
} |
| 176 |
|
| 177 |
my @modules = $object->$method( $file ); |
| 178 |
my $ref = ref $modules[0] ? $modules[0] : \@modules; |
| 179 |
push @Grand_modules, map { $sub->( $_ ) } @$ref; |
| 180 |
|
| 181 |
# remove core modules |
| 182 |
@Grand_modules = |
| 183 |
grep { ! defined Module::CoreList->first_release( $_->[0] ) } |
| 184 |
@Grand_modules |
| 185 |
if $opts{e}; |
| 186 |
|
| 187 |
next if $opts{j} || $opts{l} || $opts{0} || $opts{c}; # do these after |
| 188 |
|
| 189 |
# Handle this here because we want the filename |
| 190 |
long_list( $file, @Grand_modules ) |
| 191 |
} |
| 192 |
|
| 193 |
# Handle these options after going through all the files |
| 194 |
if( $opts{l} or $opts{0} ) { short_list( \%opts, @Grand_modules ) } |
| 195 |
elsif( $opts{j} ) { json_list( \%opts, @Grand_modules ) } |
| 196 |
elsif( $opts{c} ) { cpan_file( \%opts, @Grand_modules ) } |
| 197 |
} |
| 198 |
|
| 199 |
sub short_list { |
| 200 |
state $Seen = {}; |
| 201 |
my $opts = shift; |
| 202 |
|
| 203 |
my $glue = $opts->{0} ? "\000" : "\n"; |
| 204 |
print join( $glue, |
| 205 |
grep( { ! $Seen->{$_}++ } sort map { $_->[0] } @_ ), |
| 206 |
'' |
| 207 |
); |
| 208 |
} |
| 209 |
|
| 210 |
sub json_list { |
| 211 |
state $Seen = {}; |
| 212 |
my $opts = shift; |
| 213 |
|
| 214 |
my $glue = $opts->{0} ? "\000" : "\n"; |
| 215 |
print |
| 216 |
"[\n\t", |
| 217 |
join( ",\n\t", |
| 218 |
map { qq("$_") } |
| 219 |
grep { ! $Seen->{$_}++ } |
| 220 |
sort |
| 221 |
map { $_->[0] } |
| 222 |
@_ |
| 223 |
), |
| 224 |
"\n]\n"; |
| 225 |
} |
| 226 |
|
| 227 |
sub cpan_file { |
| 228 |
state $Seen = {}; |
| 229 |
my $opts = shift; |
| 230 |
|
| 231 |
foreach my $module ( @_ ) { |
| 232 |
printf "requires '%s'", $module->[0]; |
| 233 |
printf ", '%s'", $module->[1] if defined $module->[1]; |
| 234 |
print ";\n"; |
| 235 |
} |
| 236 |
} |
| 237 |
|
| 238 |
BEGIN { |
| 239 |
my $corelist = eval { require Module::CoreList }; |
| 240 |
|
| 241 |
sub long_list { |
| 242 |
my( $file, @modules ) = @_; |
| 243 |
|
| 244 |
printf "Modules required by %s:\n", $file; |
| 245 |
|
| 246 |
my( $core, $extern ) = ( 0, 0 ); |
| 247 |
|
| 248 |
foreach my $tuple ( @modules ) { |
| 249 |
my( $module, $version ) = @$tuple; |
| 250 |
printf " - $module%s\n", |
| 251 |
$corelist |
| 252 |
? |
| 253 |
do { |
| 254 |
my $v = Module::CoreList->first_release( $module ); |
| 255 |
$core++ if $v; |
| 256 |
$v ? " (first released with Perl $v)" : ''; |
| 257 |
} |
| 258 |
: |
| 259 |
do { $extern++; '' } |
| 260 |
} |
| 261 |
|
| 262 |
printf "%d module(s) in core, %d external module(s)\n\n", $core, $extern; |
| 263 |
} |
| 264 |
} |
| 265 |
|
| 266 |
=back |
| 267 |
|
| 268 |
=head1 AUTHORS |
| 269 |
|
| 270 |
Jonathan Yu C<< <[email protected]> >> |
| 271 |
|
| 272 |
brian d foy C<< <[email protected]> >> |
| 273 |
|
| 274 |
=head1 COPYRIGHT & LICENSE |
| 275 |
|
| 276 |
Copyright © 2009-2026, brian d foy <[email protected]>. All rights reserved. |
| 277 |
|
| 278 |
You can use this script under the same terms as Perl itself. |
| 279 |
|
| 280 |
=head1 SEE ALSO |
| 281 |
|
| 282 |
L<Module::Extract::Use>, |
| 283 |
L<Module::ExtractUse>, |
| 284 |
L<Module::ScanDeps>, |
| 285 |
|
| 286 |
=cut |