From charlesreid1

(Created page with "=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 ...")
 
 
(26 intermediate revisions by the same user not shown)
Line 1: Line 1:
=Modules=
=Managing Modules Automatically=


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


==Finding==
==Finding==


Use the Comprehensive Perl Archive Network (CPAN) to find useful Perl modules.  Most Perl modules are on CPAN.
Use the Comprehensive Perl Archive Network (CPAN) to find useful Perl modules.  Most Perl modules are on CPAN.
http://www.cpan.org/


==Installing==
==Installing==
===Before you install perl modules: cpanm===


The <code>cpanm</code> utility can be used to easily install perl modules.  To install this utility, run the command:
The <code>cpanm</code> utility can be used to easily install perl modules.  To install this utility, run the command:
Line 27: Line 30:
* gpg
* gpg
* less/more (pager program)
* 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:
<pre>
~/.cpan/build/App-cpanminus-1.4004/blib/script/cpanm
</pre>
===Installing perl modules with cpanm===
You can install a module found on CPAN by running the command
<pre>
$ cpanm Module::Name
</pre>
If you run this as a regular user, it will put everything in
<pre>
~/perl5
</pre>
If you run this as the superuser, it will put everything in
<pre>
/usr/local/bin
</pre>
and/or, if you're on a Mac, in
<pre>
/Library/Perl/5.8.8
</pre>
Alternatively, use the runtime option <code>--local-lib=/path/to/perl/install/location</code>, or set the environmental variable <code>PERL_CPANM_OPT="--local-lib=/path/to/perl/install/location"</code>.
===Updating===
You can update your cpanm by running
<pre>
$ cpanm App::cpanminus
</pre>
or
<pre>
$ cpanm --self-upgrade
</pre>




==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 <code>GraphViz::Makefile</code> 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 <code>cpanm</code>:
<source lang="bash">
$ ~/.cpan/build/App-cpanminus-1.4004/blib/script/cpanm GraphViz::Makefile
</source>
This will download appropriate files to <code>~/.cpanm/latest-build</code>.
'''3.''' Create a sample Makefile:
<source lang="make">
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
</source>
'''4.''' Create a Perl script to run this Makefile through the <code>GraphViz::Makefile</code> module:
<source lang="perl">
#!/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;
</source>
'''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 <code>as_text</code> 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:
<source lang="bash">
$ ./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
</source>
<pre>
$ 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"];
}
</pre>
This file can be run through [[Dot]], e.g. by running the command
<source lang="bash">
$ dot plot.dot -Tpng -O
</source>
(see [[Dot#Usage]] for more info on using Dot), which results in the following image:
[[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:
<pre>
$ perl Makefile.PL
(some output, checking for prerequisites, etc.; the end result is a Makefile)
$ make
(some more output)
</pre>
Once you're done, you should be able to use the Perl module from a script by saying,
<source lang="perl">
use lang '/path/to/perl/module'
</source>
{{mbox|text='''NOTE''': Perl is notorious for dependency-hell, so it is very highly recommended that you do NOT try and install most modules by hand, as they will depend on two packages that each depend on four packages that each depend on eight packages, and so on...}}
Perl module for web scraping:
* [[Mechanize (Perl)|WWW::Mechanize]]
=Homebrew Perl=
<pre>
$ brew install perl
</pre>
After doing this, here were the instructions I saw:
<pre>
==> 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
</pre>
On running the first command,
<pre>
PERL_MM_OPT="INSTALL_BASE=$HOME/perl5" cpan local::lib
</pre>
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:
<pre>
echo 'eval "$(perl -I$HOME/perl5/lib/perl5 -Mlocal::lib)"' >> ~/.bash_profile
</pre>
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/
* youtube talk on perl moose: https://www.youtube.com/watch?v=wGGDPATfsfo
* perl moose mirror on github: https://github.com/nothingmuch/moose/blob/master/benchmarks/caf_vs_moose.pl
* moose tag on stack overflow: https://stackoverflow.com/questions/tagged/moose


=References=
=References=


* Comprehensive Perl Archive Network (CPAN): http://www.cpan.org/
==Basic Perl==
 
Perl.com introduction:
* http://www.perl.com/pub/2000/10/begperl1.html
 
MacTech intro:
* http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html
 
Perl tutorial: operators
* http://tizag.com/perlT/perloperators.php
 
Perldocs:
* http://perldoc.perl.org/
 
Erik Bern says Perl is dead, Long Live Go!
* https://erikbern.com/2017/03/15/the-eigenvector-of-why-we-moved-from-language-x-to-language-y.html
 
==Perl Resources/Packages==
 
Paws: accessing AWS services like S3 using Perl:
* background and example: http://perltricks.com/article/deploy-a-static-website-with-aws-s3-and-paws/
* same author, but on github: https://github.com/dnmfarrell/Paws-tools
 
Penetration testing with perl:
* scripts collection on github: https://github.com/dnmfarrell/Penetration-Testing-With-Perl
 
Perl one-liners: book in progress:
* https://github.com/dnmfarrell/Perl6-One-Liners
 
==CPAN==
 
Perl string matching:
* http://www.comp.leeds.ac.uk/Perl/matching.html
 
Comprehensive Perl Archive Network (CPAN):  
* http://www.cpan.org/


CPAN page on Perl modules:
* http://www.cpan.org/modules/index.html


PerlMonks page on Perl modules:
* http://www.perlmonks.org/?node_id=128077


StackOverflow page on installing specific versions of modules:
* http://stackoverflow.com/questions/260593/how-can-i-install-a-specific-version-of-a-perl-module


=See Also=


* [[Mechanize (Perl)]]




Line 43: Line 319:




{{Programs}}
{{Unix Programs}}
{{Languages}}


[[Category:Computers]]
[[Category:Perl]]
[[Category:Languages]]
[[Category:Programs]]

Latest revision as of 23:37, 11 March 2018

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