160 likes | 284 Views
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
E N D
Painless Perl ModulesFernando J. PinedaBiostat computing club2013/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> OPTIONS: --length = <value> # some integer value --file = <filename> # some string --verbose # set a flag --help # 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, # set a flag for verbose output "length=i" => \$length, # numeric "file=s" => \$string, # example string "help" => \$help # 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 • Of course change fernandoto your own userid!!! • Source your .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 export USER_INSTALL_BASE=/home/mmi/fernando/myperl source /hpscc/usr/local/perl/perlrc Source ~/.bashrc
pmtoolsunix commands for finding out about perl modules pmall show all installed versions and descs pman show a module's man page pmcat page through a module file Pmcheck check that Perl is set up correctly for Perl modules pmdesc print out version and whatis description of perl modules pmdirs print out module directories pmeth show a Perl class's methods pmexp show a module's exports pmfunc cat out a function from a module pminst find modules whose names match this pattern Pmload show what files a given module loads at compile time pmls long list the module path Pmpath show full path to a perl module pmvers print out a module's version
pmtoolsPerldoc (documentation) commands basepods print out pod paths for the standard perl manpages faqpods print out pod paths for the standard perl faqs modpods print out paths for the standard modules Podgrep grep in pod sections only podpath print the path to the pod pods print out all pod paths podtoc show outline of pods Sitepods print out the paths to the modules that this site added Stdpods print out the paths to the modules that came with Perl
PmtoolsMiscellaneous commands pfgrep grep out function definitions from perlfunc plxload 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.PLor 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 Answer questions with defaults except for PREFIX [] PREFIX = path_to_your_perl_modules_directory quit cpan > cpan module_that_you_want_to_install 2. Or my favorite : blow away your cpan configuration and start over >rm –rf ~/.cpan >perl -e shell -MCPAN Now you can use cpan to install your modules with CPAN
But it never works! • Actually the installation always works flawlessly, but perl can’t find it! • Example script: • The error message: #!/usr/bin/perl use foobar; 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_64-linux-thread-multi /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/…