Perl CPAN Made Easy

Posted by JD 03/15/2010 at 11:10

CPAN, the Comprehensive Perl Archive Network, contains 10,000+ different Perl modules. All of these work just about the same to get installed on a system. That’s because Perl programmers use a standard package for distributing software that includes the code, tests, and manual. Below I’ll show the general way to build and install most perl modules, including those with external, non-perl, dependencies. Then I’ll show how to automate this with CPAN.

Check the version of Perl you have installed. I’ve used the methods below on 5.6.x and later.

$ perl -v
This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi

CPAN Way to Install

First, be certain to update your CPAN module. This will also ask you to configure CPAN settings if this is your first time running cpan.

sudo cpan ‘CPAN

The general use of cpan is:

sudo cpan ‘Module::Submodule’ ‘Mod2’ …   ‘ModN’

So if you want to install Inline/MakeMaker.pm, run
sudo cpan ‘Inline::MakeMaker’

This command will install a perl module Inline::MakeMaker to the system for everyone to use. It will download the latest version of the module from CPAN (or a configured mirror), unpack it, check dependencies, warn of any missing dependencies, recursively download, build, test, install the perl dependencies, the build, test, and install the requested module. When it works, it works well and it is really very amazing.

There are other, older ways to accomplish the same things.

Issues with CPAN Installations

When there are issues, it is usually with an external dependency. For example, if you are trying to get access to MySQL, there will be an external C program that may be included in the Perl MySQL module. If you don’t have the expected C compiler on your system, then the build, test, install effort of cpan will fail. The error messages should be clear enough.

Sometimes there is a missing library. You’ll want to check your package manager for it, since we all know that installing programs or libraries from source is a good way to get into RPM/DEB package manager hell.

Tests Failing are sometimes the only error between you and an install. Sometimes that is because the test is broken. If there are 50 tests included for a perl module and only 1 or 2 are failing, it is likely you’ve found a test bug. Perhaps the test is missing a system check to determine that it shouldn’t run those specific tests. Anyway, you can edit the test script, find the failing version of the test then find where the error is. These tests are usually fairly simple pieces of code. Mine usually have either
: print “ok 7\n”;
or
: print “not ok 7\n”;
when there’s a non-good return $ret. Simple.

If you have any questions, here’s a really good and complete CPAN and Perl Module installation HowTo. I really like the part about using sudo, but only for installation.

Manual Perl Module Installs

Support you have a perl module in source format – JP-DBM-1.5.tar.gz or is could be named JP-DBM-1.5.tgz.

First, uncompress and untar it

tar zxvf JP-DBM-1.5.tar.gz

Then cd into the newly created directory
cd JP-DBM-1.5

Run the following commands to create a makefile for your system, build the module, test it, then install it system wide.

perl Makefile.PL
make
make test
sudo make install

Every Perl module works in exactly the same way. Every one of them. I suppose it is a little different on MS-Windows since the make program isn’t generally available there.

Each step will return a good/clean status or a failure. If there is a failure, the error message should be clear enough to tell whether it is a dependency issue or test failure or some other error. This method does not do all the things that the cpan module does, like automatic dependency downloads.

If you want to know how to use the module, the documentation is included inside it in pod format.

perldoc DBM.pm

will display the man-page-like documentation. It is customary to include an example use at the top in the SYNOPSIS section. Every method of the class should be clearly defined as should the usage. After a perl module is installed, the POD is placed in to the manpath too, so man JP::DBM will display the POD too.

Trackbacks

Use the following link to trackback from your own site:
https://blog.jdpfu.com/trackbacks?article_id=558