[ Team LiB ] Previous Section Next Section

Hack 8 Installing Perl Modules

figs/moderate.giffigs/hack08.gif

A fair number of our hacks require modules not included with the standard Perl distribution. Here, we'll show you how to install these modules on Windows, Mac OS X, and Unix-based systems.

As you go through this book, you'll notice that we're constantly mentioning a variety of different Perl modules. Some of them aren't standard installation fare, so it's unlikely that you'll already have them available to your coding.

Why do we mention nonstandard modules? Quite simply, we didn't want to reinvent the wheel. We do some pretty odd toting and lifting in this book, and without the help of many of these modules we'd have to do a lot of extra coding (which means just that many more breakable bits in our scripts).

If you're new to Perl, however, you may be feeling intimidated by the idea of installing modules. Don't worry; it's a snap! If you're running ActiveState Perl for Windows, you'll want to use the Programmer's Package Manager (PPM). Otherwise, you'll use CPAN.

Example: Installing LWP

LWP is used throughout this book, because it's the workhorse for any Perl script with designs on interacting with the Internet. Some Perl installations already have it installed; others don't. We'll use it here as an example of how to install a typical Perl module; the steps apply to almost any available noncore module that we use in our other hacks—and, indeed, that you may encounter in your ongoing programming.

Usually, the easiest way to install a Perl module is via another Perl module. The CPAN module, included with just about every modern Perl distribution, automates the installation of Perl modules, fetching components and any prerequisites and building the whole kit and kaboodle for you on the fly.

CPAN installs modules into standard system-wide locations and, therefore, assumes you're running as the root user. If you have no more than regular user access, you'll have to install your module by hand (see Unix and Mac OS X installation by hand later in this hack).


Unix and Mac OS X installation via CPAN

Assuming you have the CPAN module, have root access, and are connected to the Internet, installation should be no more complicated than this:

% su
Password:
# perl -MCPAN -e shell
cpan shell -- CPAN exploration and modules installation (v1.52)
ReadLine support available (try ``install Bundle::CPAN'')
cpan> install libwww-perl

Or, if you prefer one-liners:

% sudo perl -MCPAN -e 'install libwww-perl'

What's with this libwww-perl? I thought we were installing LWP! They're actually one and the same, LWP being an abbreviation of libwww-perl. You'll find more details, should you be interested, at http://www.linpro.no/lwp/lwpng-paper/.


In either case, go grab yourself a cup of coffee, meander through the garden, read the paper, and check back once in a while. Your terminal's sure to be riddled with incomprehensible gobbledygook that you can, for the most part, summarily ignore. You may be asked a question or three; in most cases, simply pressing Return to accept the default answer will do the trick.

Unix and Mac OS X installation by hand

If CPAN installation didn't quite work as expected, you can of course fall back to installing the module by hand. Download the latest version manually from CPAN (http://search.cpan.org/author/GAAS/libwww-perl/, in this case), unpack it, then build it, like so:

% tar xvzf libwww-perl-5.69.tar.gz
libwww-perl-5.69/
libwww-perl-5.69/lib/
libwww-perl-5.69/lib/HTTP/
libwww-perl-5.69/lib/HTTP/Cookies.pm
libwww-perl-5.69/lib/HTTP/Status.pm
...
libwww-perl-5.69/Changes
libwww-perl-5.69/README
libwww-perl-5.69/lwpcook.pod
% cd libwww-perl-5.69
% perl Makefile.PL
This package comes with some sample programs that I can try
to install in /usr/local/bin.

   Note that you can avoid these questions by passing
   the '-n' option to 'Makefile.PL'.
...
% make
mkdir blib
mkdir blib/lib
...
% make test
/usr/bin/perl t/TEST 0 
base/common-req.......ok 
base/cookies..........ok 
base/date.............ok 
base/headers-auth.....ok
...
% su
Password:
# make install
Installing /Library/Perl/LWP.pm
Installing /Library/Perl/lwptut.pod
...

Of course, your output will vary from module to module, version to version, and operating system to operating system.


If, during the perl Makefile.PL phase, you run into any warnings about installing prerequisites, you'll have to install each in turn before attempting to install LWP again. A typical prerequisite warning looks something like this:

Checking if your kit is complete...
Looks good
Warning: prerequisite HTTP::Daemon failed to load: Can't locate
HTTP/Daemon.pm in @INC (@INC contains: /System/Library/Perl/darwin
/System/Library/Perl /Library/Perl/darwin /Library/Perl /Library/Perl
/Network/Library/Perl/darwin /Network/Library/Perl
/Network/Library/Perl .) at (eval 8) line 3.

If you have little more than user access to the system and still insist on installing LWP yourself, you'll have to install it and all its prerequisites somewhere in your home directory; ~/lib, a lib directory in your home directory, is as good a place as any. Inform Perl of your preference like so:

% perl Makefile.PL LIB=/home/login/lib

Replace /home/login/lib with an appropriate path.

Windows installation via PPM

If you're running Perl under Windows, chances are it's ActiveState's ActivePerl (http://www.activestate.com/Products/ActivePerl/). Thankfully, ActivePerl's outfitted with a CPAN-like module-installation utility. PPM (http://aspn.activestate.com/ASPN/Downloads/ActivePerl/PPM/) grabs nicely packaged module bundles from the ActiveState archive and drops them into place on your Windows system with little need of help from you.

Simply launch PPM from a DOS window (Start Menu Run . . . command OK) and tell it to install the LWP bundle:

C:\>ppm
PPM interactive shell (2.1.6) - type 'help' for available commands.
PPM> install libwww-perl

If you're running a reasonably recent build, you're in for a pleasant surprise; libwww-perl was installed along with ActivePerl itself:

Version 5.64 of 'libwww-perl' is already installed.
    [ Team LiB ] Previous Section Next Section