How to find all the modules used in a Perl script and install them?

I have been provided with several Perl scripts for deployment.

What is the easiest way to find and install all the modules used by these scripts?

EDIT:

From what I can find, there are no conditional inclusions or inclusions in evals.

+6
perl perl-module
source share
4 answers

Does my Module :: Extract :: Use help help? There extract_modules program in the examples directory:

$ examples/extract_modules -l some_program File::Spec File::Spec::Functions strict warning 

You can pass this list to cpan .

 $ examples/extract_modules -l some_program | xargs cpan 

Once you have this list, which is only the first level of dependencies, you can make a script distribution that allows people to use the regular CPAN toolchain to set everything up.

If something does not work for you, modify the program to process it. If you think this would be helpful to other people, send a pull request. :)

+10
source share

I was hoping for Module :: ScanDeps , which provides the scandeps.pl command-line utility , but, unfortunately, Module::ScanDeps does not seem to be intended for this specific purpose, because scandeps.pl ignores missing modules or (with -c or -x ) screams when the script uses a module that is not installed.

Here is a quick'n'dirty Perl script that tries to execute the script with do until it succeeds

 #!/usr/bin/perl use strict; use warnings; use Term::Prompt; my ($script) = @ARGV; die "Provide script file name on the command line\n" unless defined $script; until ( do $script ) { my $ex = $@ ; if ( my ($file) = $ex =~ /^Can't locate (.+?) in/ ) { my $module = $file; $module =~ s/\.(\w+)$//; $module = join('::', split '/', $module); print "Attempting to install '$module' via cpan\n"; system(cpan => $module); last unless prompt(y => 'Try Again?', '', 'n'); } else { die $ex; } } 

If you do not want the script to run, you can run perl -c $script , write stderr output of this, and analyze the missing module messages and call cpan for each such module found before the perl -c $script outputs "Syntax OK". This gives you a cleaner cycle. I will look at this later.

You can skip dependencies loaded at runtime using this technique.

+3
source share

Well, this is a very simplified way that I solved this.

In the bash shell:

 cat *.pl | grep "^use " | tr ';' ' ' | while read abc; do echo $b; done | sort -iu > modules.txt 

This gave me a file with module names, one on each line.

Then i used this

 cat modules.txt | while read a; do cpan $a; done 

to invoke cpan for each module name in the file. And then he sat, answering CPAN questions yes, to establish dependencies accordingly.

Not beautiful, but this time he did his job.

+1
source share

Or let PAR pp do the work for you, collecting everything you need into a single executable.

0
source share

All Articles