Slides

advertisement
Painless Perl Modules
Fernando J. Pineda
Biostat computing club
2013/2/21
Perl

Use the right tool for the right job
Use perl for tasks that are 90% text
manipulation and if someone has alread
written modules to do what you want
 User R for stats and plotting and if someone
has already written libraries that do what you
want
 We’re not going to teach perl but

A trivial perl script
#!/usr/bin/perl
$length= 1;
$string = “hello world”
print “The length is $length\n”;
Print “The string is $string\n”;
Modules



The the fundamental unit of code reuse
The same idea as libraries in R
Example: reading commandline options
compute-0-9> ./template_ez_script.pl --help
USAGE: template_ez_script.pl
OPTIONS:
--length = <value>
--file = <filename>
--verbose
--help
<options>
#
#
#
#
some integer value
some string
set a flag
print this help message
template_ez_script.pl
#!/usr/bin/perl
use Getopt::Long;
# module that handles commandline options
my $the_options = GetOptions (
"verbose" => \$verbose,
"length=i" => \$length,
"file=s"
=> \$string,
"help"
=> \$help
);
# set a flag for verbose output
# numeric
# example string
# set to print help message
$how_many = efined($help)+defined($length)+defined($string)+defined($verbose);
if(defined($verbose)) { print "yak yak yak...\n" }
if(defined($string)) { print "the string is $string\n" }
if(defined($length)) { print "the length is $length\n" }
# handle help message
$help_msg = "\nUSAGE: template_ez_script.pl <options>\n"
."OPTIONS:\n"
."\t --length = <value>
# some integer value\n"
."\t --file = <filename> # some string\n"
."\t --verbose
# set a flag\n"
."\t --help
# print this help message\n";
if(defined($help)) { print $help_msg }
if($how_many == 0){ print $help_msg }
www.CPAN.org
Source for all things perl. In particular more modules than you
could possibly imagine
.bashrc configuration

On the cluster add the following 2 lines to your .bashrc
export USER_INSTALL_BASE=/home/mmi/fernando/myperl
source /hpscc/usr/local/perl/perlrc

Of course change fernando to your own userid !!!

Source your .bashrc
Source ~/.bashrc

What this does:



Sets up your directory for installing your own perl modules
Gives you access to community perl modules
Sets up path to pmtools commands
pmtools
unix commands for finding out about perl modules
pmall
pman
pmcat
Pmcheck
pmdesc
pmdirs
pmeth
pmexp
pmfunc
pminst
Pmload
pmls
Pmpath
pmvers
show all installed versions and descs
show a module's man page
page through a module file
check that Perl is set up correctly for Perl modules
print out version and whatis description of perl modules
print out module directories
show a Perl class's methods
show a module's exports
cat out a function from a module
find modules whose names match this pattern
show what files a given module loads at compile time
long list the module path
show full path to a perl module
print out a module's version
pmtools
Perldoc (documentation) commands
basepods
faqpods
modpods
Podgrep
podpath
pods
podtoc
Sitepods
Stdpods
print out pod paths for the standard perl manpages
print out pod paths for the standard perl faqs
print out paths for the standard modules
grep in pod sections only
print the path to the pod
print out all pod paths
show outline of pods
print out the paths to the modules that this site added
print out the paths to the modules that came with Perl
Pmtools
Miscellaneous commands
pfgrep
plxload
grep out function definitions from perlfunc
show what files a perl program loads at compile time
Modules



A module is a fundamental unit of code reuse.
Code for use by colleagues and others is distributed
as modules.
It contains subroutines, variables and executable
statements.
The quick and dirty way to install
modules
1) In the same directory as the perl script that uses them
2) In a subdirectory of your home
and tell perl where to find it in your script
#!/usr/bin/perl
use strict;
use lib qw(/home/fernando/perl/lib);
Installing modules 1:
from distribution

Download the module, e.g. from www. cpan.org

Install the module
 perl Makefile.PL
or perl Makefile.PL LIB=/put/module/here
 make
 make test
 make install

This generally always works
Installing modules with CPAN
1. If you are not the administrator you first configure cpan
>perl -e shell -MCPAN
cpan>o conf makepl_arg PREFIX=path_to_your_perl_modules_directory
cpan>o conf mbuildpl_arg "--prefix path_to_your_perl_modules_directory"
cpan>o conf commit
2. Or my favorite : blow away your cpan configuration and start over
>rm –rf ~/.cpan
>perl -e shell -MCPAN
Answer questions with defaults except for PREFIX
[] PREFIX =
path_to_your_perl_modules_directory
quit cpan
Now you can use cpan to install your modules with CPAN
> cpan module_that_you_want_to_install
But it never works!


Actually the installation always works flawlessly,
but perl can’t find it!
Example script:
#!/usr/bin/perl
use foobar;

The error message:
compute-0-9> ./template_ez_script.pl
Can't locate foobar.pm in @INC (@INC contains:
/home/mmi/project/markhamlab/fernando/hiv2gen/lib
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86_6
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86…
Where is the module?
I was expecting it in: /home/mmi/fernando/myperl
But the reality is it could be any number of subdirectories…
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8
/home/mmi/fernando/myperl/lib64/perl5/site_perl
/home/mmi/fernando/myperl/lib64/perl5/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/5.8.8
/home/mmi/fernando/myperl/lib64/perl5/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib64/perl5/5.8.8
/home/mmi/fernando/myperl/lib64/perl5
/home/mmi/fernando/myperl/lib/perl5/site_perl/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib/perl5/site_perl/5.8.8
/home/mmi/fernando/myperl/lib/perl5/site_perl/5.8.8
/home/mmi/fernando/myperl/lib/perl5/site_perl
/home/mmi/fernando/myperl/lib/perl5/5.8.8/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib/perl5/5.8.8
/home/mmi/fernando/myperl/lib/perl5/x86_64-linux-thread-multi
/home/mmi/fernando/myperl/lib/perl5/hpscc/usr/local/perl/lib64/perl5/site_perl/5.8.8/…
Download