From charlesreid1

Modules

More info here: http://www.cpan.org/modules/index.html

Finding

Use the Comprehensive Perl Archive Network (CPAN) to find useful Perl modules. Most Perl modules are on CPAN.

Installing

Before you install perl modules: cpanm

The cpanm utility can be used to easily install perl modules. To install this utility, run the command:

$ cpan App::cpanminus

This will ask several questions and has several prerequisites, including the following:

  • gzip
  • tar
  • unzip
  • make
  • lynx (available via Fink for Mac)
  • wget (available via Fink for Mac)
  • ncftpget (available for most operating systems here: http://www.ncftp.com/download/)
  • ftp
  • gpg
  • less/more (pager program)

Note that if you are running Mac OS X, you will also need to install developer tools (via XCode on the installation CD, or by downloading and installing XCode 3 from Apple).

Once you've run the above command, cpanm will be a script located in:

~/.cpan/build/App-cpanminus-1.4004/blib/script/cpanm

Installing perl modules with cpanm

You can install a module found on CPAN by running the command

$ cpanm Module::Name

If you run this as a regular user, it will put everything in

~/perl5

If you run this as the superuser, it will put everything in

/usr/local/bin

and/or, if you're on a Mac, in

/Library/Perl/5.8.8

Alternatively, use the runtime option --local-lib=/path/to/perl/install/location, or set the environmental variable PERL_CPANM_OPT="--local-lib=/path/to/perl/install/location".


Updating

You can update your cpanm by running

$ cpanm App::cpanminus

or

$ cpanm --self-upgrade


Perl Module Example

To give an example of how one would go about installing a Perl module, I will give an example. This example will walk you through the installation and usage of the GraphViz::Makefile Perl module.

(This module creates a Dot file from a Makefile)

1. Visit this page for more information about the module: http://search.cpan.org/~srezic/GraphViz-Makefile-1.16/Makefile.pm

2. Install the module by fetching it using cpanm:

$ ~/.cpan/build/App-cpanminus-1.4004/blib/script/cpanm GraphViz::Makefile

This will download appropriate files to ~/.cpanm/latest-build.

3. Create a sample Makefile:

all: foo
all: bar
    echo hallo

any: foo hiya
    echo larry
    echo howdy

any: blah blow

foo: blah boo
    echo Hi

foo: howdy buz
    echo Hey

4. Create a Perl script to run this Makefile through the GraphViz::Makefile module:

#!/usr/bin/perl

use GraphViz::Makefile;
my $gm = GraphViz::Makefile->new(undef, "Makefile");
$gm->generate("all");
open(O, ">plot.dot") or die $!;
binmode O;
print $gm->GraphViz->as_text;
close O;

NOTE: this is different from the version on the author's page, because my goal is to output a plain text Dot file. This can then be processed by Dot in whatever way I want. Alternatively, you can consult the GraphViz documentation (http://search.cpan.org/~lbrocard/GraphViz-2.04/lib/GraphViz.pm) and figure out a better method than as_text to output the Dot figure in whatever format you wish.

5. Run the perl script. Here's something cool: all the Perl-related output will go to stderr, and all the Dot-related output will go to stdout, so you can create the Dot file really easily by doing this:

$ ./graphviz.pl  > plot.dot
Reading /Library/Perl/5.8.8/Make.pm
Reading /temp/graphviz/Makefile
Command for foo redefined at /Library/Perl/5.8.8/Make.pm line 94, <Makefile> line 16.
Was:echo Hi
Now:echo He

This file can be run through Dot, which results in the following image:

DotMakefile.png

References