From charlesreid1

Managing Modules Automatically

Finding

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

http://www.cpan.org/

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
$ cat plot.dot

digraph test {
	graph [ratio=fill];
	node [label="\N"];
	graph [bb="0,0,288,180"];
	all [label=all, pos="175,162", width="0.75", height="0.5"];
	foo [label=foo, pos="139,90", width="0.75", height="0.5"];
	bar [label=bar, pos="211,90", width="0.75", height="0.5"];
	blah [label=blah, pos="27,18", width="0.75", height="0.5"];
	boo [label=boo, pos="99,18", width="0.75", height="0.5"];
	howdy [label=howdy, pos="180,18", width=1, height="0.5"];
	buz [label=buz, pos="261,18", width="0.75", height="0.5"];
	all -> bar [pos="e,202.37,107.27 183.71,144.57 187.96,136.08 193.15,125.69 197.87,116.27"];
	all -> foo [pos="e,147.63,107.27 166.29,144.57 162.04,136.08 156.85,125.69 152.13,116.27"];
	foo -> blah [pos="e,46.661,30.639 119.49,77.459 101.69,66.017 75.186,48.976 55.105,36.067"];
	foo -> boo [pos="e,108.41,34.941 129.52,72.937 124.69,64.239 118.71,53.48 113.33,43.795"];
	foo -> buz [pos="e,240.51,30.09 159.42,77.949 179.21,66.267 209.44,48.429 231.78,35.246"];
	foo -> howdy [pos="e,170.07,35.441 148.72,72.937 153.6,64.365 159.62,53.791 165.07,44.217"];
}

This file can be run through Dot, e.g. by running the command

$ dot plot.dot -Tpng -O

(see Dot#Usage for more info on using Dot), which results in the following image:

DotMakefile.png


Building/Managing Perl Modules Manually

When you download a Perl module, you'll usually see a Makefile.PL file. This is a Perl script that will create a Makefile. So the process looks like this:

$ perl Makefile.PL

(some output, checking for prerequisites, etc.; the end result is a Makefile)

$ make

(some more output)

Once you're done, you should be able to use the Perl module from a script by saying,

use lang '/path/to/perl/module'


Perl module for web scraping:

Homebrew Perl

$ brew install perl

After doing this, here were the instructions I saw:

==> Downloading https://homebrew.bintray.com/bottles/perl-5.24.0_1.el_capitan.bottle.tar.gz
######################################################################## 100.0%
==> Pouring perl-5.24.0_1.el_capitan.bottle.tar.gz
==> Caveats
By default non-brewed cpan modules are installed to the Cellar. If you wish
for your modules to persist across updates we recommend using `local::lib`.

You can set that up like this:
  PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib
  echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"' >> ~/.bash_profile
==> Summary
/usr/local/Cellar/perl/5.24.0_1: 2,291 files, 55.1M

On running the first command,

PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib

I was presented with a series of prompts, then some things were installed and set up and &c. Something about "installing internal null logger." (???) Pod installation info.

Next:

echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"' >> ~/.bash_profile

Okay. Now it adds that line to bash profile.

Profiling Perl Code

See Perl/Profiling

Perl Object Oriented Programming (POOP)

POOP in general:

Design patterns for POOP: http://www.perl.com/pub/2003/06/13/design1.html

MOOSE POOP: http://moose.iinteractive.com/en/

References

Basic Perl

Perl.com introduction:

MacTech intro:

Perl tutorial: operators

Perldocs:

Erik Bern says Perl is dead, Long Live Go!

Perl Resources/Packages

Paws: accessing AWS services like S3 using Perl:

Penetration testing with perl:

Perl one-liners: book in progress:

CPAN

Perl string matching:

Comprehensive Perl Archive Network (CPAN):

CPAN page on Perl modules:

PerlMonks page on Perl modules:

StackOverflow page on installing specific versions of modules:

See Also