Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I find all modules used in a Perl script and install them?

I have been given a few Perl scripts to deploy.

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

EDIT:

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

like image 511
Nifle Avatar asked Aug 20 '09 08:08

Nifle


People also ask

How do I see what Perl modules are installed?

You need to use instmodsh (interactive inventory for installed Perl modules) command to find out what modules already installed on my system. instmodsh command provides an interactive shell type interface to query details of locally installed Perl modules.

Where are Perl modules stored on Windows?

My Perl modules are in ~/perl/install, for example. Well, in my situation, user files all in NFS path, and locate just exclude NFS. Since the PERL5LIB is a bit long on that system, find is not a good way. For Windows, might need to use double-quotes on the outside, single quotes on the inside.

How do I install missing Perl modules?

Install missing Perl modules using distribution's package manager. Many Perl modules are available as packages, so you can install it using your distribution's package manager. And install the missing module using 'pacman' command.

Where are CPAN modules installed?

Standard modules are installed in a directory like /usr/lib/perl5, whereas third-party modules are installed in /usr/lib/perl5/site_ perl. If your system manager isn't around or can't be prevailed upon to run the installation, don't worry.


2 Answers

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

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

You can pipe that list to cpan.

$ examples/extract_modules -l some_program | xargs cpan

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

If there's something that doesn't work for you, modify the program to handle that. If you think it would be useful to other people, send a pull request. :)

like image 54
brian d foy Avatar answered Sep 24 '22 17:09

brian d foy


I was hoping Module::ScanDeps which provides the command line utility scandeps.pl would be useful here but, to my dismay, Module::ScanDeps is apparently not intended for this particular purpose as scandeps.pl either ignores missing modules or (with -c or -x) croaks when the script uses a module that is not installed.

Here is a quick'n'dirty Perl script that tries to execute the script using 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 be run, you can run perl -c $script, capture stderr output of that and parse for missing module messages and call cpan for each such module found until perl -c $script outputs "Syntax OK". That gives you a cleaner loop too. I'll look at this later.

You might miss dependencies loaded at run time using this technique.

like image 32
Sinan Ünür Avatar answered Sep 22 '22 17:09

Sinan Ünür