Friday, September 20, 2013

Installing Perl Modules

One of the great advantages of Perl is the substantial amount of code developed by the Perl community.  However, installing and using external code can be a minor annoyance for experienced Perl programmers and a major headache for Perl beginners.  For personal future reference and to help ease the pain of this learning curve, I am writing this post to outline the main installation strategies for Perl modules and give some tips for getting things working smoothly.

There are two main approaches to installing Perl modules--CPAN and manual installation via ExtUtils::MakeMaker or Module::Build.  CPAN is a fantastic resource.  My preference for installing Perl modules is to use the cpanm script (which automates the CPAN download, build, and install process) coupled with local::lib.  I also recently learned that cpanm can take a URL argument to a git or SourceForge developers release.  It will also automatically install any dependencies declared in the build script.  The command would look something like this:


local::lib is an important part of the installation process.  Many instructional websites illustrate the installation process with sudo commands.  If you are a system administrator or you know that several users will need the Perl module you are installing then it is best to use the sudo command to install into the public set of Perl modules.  However, for personal use it is best to install in a local directory.  The Perl local::lib module can take care of this very nicely for you.  Note that there are other ways to trick CPAN into installing a Perl module in a local directory, but these are not nearly as clean as using local::lib.  local::lib is surprising easy to set up by simply following the instructions on the CPAN documentation page here.  Once local::lib is setup cpanm will recognize these setting and automatically install into your local Perl module set.

The second option for installing Perl modules is a manual installation via Module::Build or ExtUtils::MakeMaker.  If you are a serious Perl programmer I would highly recommend becoming very familiar with one of these two installation modules.  I personally like Module::Build, but I admit that I have had limited experience with ExtUtils::MakeMaker.  To simply use these for installing Perl modules only a basic understanding of a handful of commands is required.

First, the module package must be downloaded to the machine where you plan to install.  I recommend having a directory where you keep all the module you download.  The directory I have designated for this is $HOME/build.  Once you have the module downloaded to your build directory you will have to unpackage it with the unzip command (or a comparable command).  This will create a directory where you unzipped the module.  Navigate into the directory.  If you see a file called Build.PL you know the module was built using Module::Build.  Use the following commands to build, test and install the module:

perl Build.PL
./Build test
./Build install

If the module fails to install because required dependencies are missing you can try the command:

./Build installdeps

If you do not see the Build.PL file then you should see a file named Makefile.PL.  This indicates that the module was built using ExtUtils::MakeMaker.  To install such a module use the following commands:

perl Makefile.PL
make test
make install

For details on the wide range of capabilities for both these modules carefully read the documentation pages (Module::Build, ExtUtils::MakeMaker).

For more information on installing Perl modules see the following:

As a side note the following command is useful for determining if a module is already installed and where it is located:

perldoc -l <module name>

No comments:

Post a Comment