Hack #32. Manage Module Installations

Bundle up required modules to make installations easier.

Embracing the Perl way means taking advantage of the CPAN when possible. There are thousands of reusable, easily installable modules that do almost anything you can imagine—including making your coding life much easier and simpler.

Some day you'll have to distribute your software, upgrade Perl, or do something else that means that you can't rely on having all of your existing modules available. Never fear; just create a bundle that the CPAN module can use to install all of the necessary modules for you!

The Hack

The CPAN module doesn't only download and install modules. It can also give you a catalog of what you have installed on your system. The autobundle command takes this list and writes it to a bundle file—a very simple, mostly POD module that CPAN can use later (or elsewhere) to install necessary modules.

Tip

If you only support one application, you can use a technique such as in "Trace All Used Modules" [Hack #74] to figure out everything you need to install.

All you have to do is launch the shell, issue the autobundle command, and note where it creates the bundle file:

$ cpan

cpan shell -- CPAN exploration and modules installation (v1.7601)
ReadLine support enabled

cpan> autobundle

# time passes...

Wrote bundle file
  /usr/src/.cpan/Bundle/Snapshot_2005_11_13_00.pm

Running the Hack

Copy or move the bundle file from its current location. Then when you upgrade or reinstall Perl, or when you move to another box, move the bundle file to the Bundle/ directory beneath the CPAN module's working directory. Then, from the CPAN shell in the new machine or installation:

$ cpan

cpan shell -- CPAN exploration and modules installation (v1.7601)
ReadLine support enabled

cpan> install Bundle::Snapshot_2005_11_13_00

# time really passes...

It will go through the bundle list in order, intstalling all modules as necessary. At least, it will try.

Hacking the Hack

If you look at the bundle file, you might notice that it includes lots and lots of modules—maybe more than you need and certainly plenty of core modules. Worse yet, depending on how you've configured your CPAN and how well the modules you want to install mark their dependencies, you may need to babysit the installation to get it to succeed. You may even have to restart it a few times.

If possible, set CPAN to follow all prerequisites without asking when configuring it for the first run. (You can always change it back later.) That will help. The next best thing to do is to prune the module list. When possible, try to arrange dependencies appropriately. (Modules change enough that it's unlikely you'll be able to do this perfectly.)

Finally, you can prune out all of the core modules by running the bundle file through Module::CoreList [Hack #73]. That way, you have a somewhat smaller list of modules to install.

use Module::CoreList;

my ($bundle, $version) = @ARGV;
$version             ||= $];
@ARGV                  = $bundle;
my $core_list          = $Module::CoreList::version{ $version };
die "Unknown version $version\\n" unless $core_list;

# find module list
while (<>)
{
    print;
    last if $_ eq "=head1 CONTENTS\\n";
}

print "\\n";

# process only module/version lines
while (<>)
{
    if ( $_ eq "=head1 CONFIGURATION\\n" )
    {
        print;
        last;
    }

    chomp;
    next unless $_;

    my ($module, $version) = split( /\\s+/, $_ );
    $version = 0 if $version eq 'undef';

    next if exists $core_list->{ $module }
               and $core_list->{ $module } >= $version;

    print "$module $version\\n\\n";
}

# print everything else
print while <>;

Run this program, passing the name of the bundle file and, optionally, the version of Perl against which to check. Redirect the output to a new bundle file:

$ perl prune_bundle.pl Snapshot_2005_11_03_00.pm > PrunedSnapshot.pm
$

Now you have an easier time deciding which modules you really need to install.

Get Perl Hacks now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.