Download presentation
Presentation is loading. Please wait.
Published byAniyah Burgoyne Modified over 10 years ago
1
Painless Perl Modules Fernando J. Pineda Biostat computing club 2013/2/21
2
Perl Use the right tool for the right job 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 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 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 We’re not going to teach perl but
3
A trivial perl script #!/usr/bin/perl $length= 1; $string = “hello world” print “The length is $length\n”; Print “The string is $string\n”;
4
Modules The the fundamental unit of code reuse The the fundamental unit of code reuse The same idea as libraries in R The same idea as libraries in R Example: reading commandline options Example: reading commandline options compute-0-9>./template_ez_script.pl --help USAGE: template_ez_script.pl OPTIONS: --length = # some integer value --file = # some string --verbose # set a flag --help # print this help message
5
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 \n"."OPTIONS:\n"."\t --length = # some integer value\n"."\t --file = # 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 }
6
www.CPAN.org Source for all things perl. In particular more modules than you could possibly imagine
7
.bashrc configuration On the cluster add the following 2 lines to your.bashrc On the cluster add the following 2 lines to your.bashrc Of course change fernando to your own userid !!! Of course change fernando to your own userid !!! Source your.bashrc Source your.bashrc What this does: What this does: Sets up your directory for installing your own perl modules Sets up your directory for installing your own perl modules Gives you access to community perl modules Gives you access to community perl modules Sets up path to pmtools commands Sets up path to pmtools commands export USER_INSTALL_BASE=/home/mmi/fernando/myperl source /hpscc/usr/local/perl/perlrc Source ~/.bashrc
8
pmtools unix commands for finding out about perl modules pmall show all installed versions and descs pman show a module's man page pmcatpage through a module file Pmcheckcheck that Perl is set up correctly for Perl modules pmdescprint out version and whatis description of perl modules pmdirsprint out module directories pmethshow a Perl class's methods pmexpshow a module's exports pmfunccat out a function from a module pminstfind modules whose names match this pattern Pmloadshow what files a given module loads at compile time pmlslong list the module path Pmpathshow full path to a perl module pmversprint out a module's version
9
pmtools Perldoc (documentation) commands basepodsprint out pod paths for the standard perl manpages faqpodsprint out pod paths for the standard perl faqs modpodsprint out paths for the standard modules Podgrepgrep in pod sections only podpathprint the path to the pod podsprint out all pod paths podtocshow outline of pods Sitepodsprint out the paths to the modules that this site added Stdpodsprint out the paths to the modules that came with Perl
10
Pmtools Miscellaneous commands pfgrepgrep out function definitions from perlfunc plxloadshow what files a perl program loads at compile time
11
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.
12
The quick and dirty way to install modules #!/usr/bin/perl use strict; use lib qw(/home/fernando/perl/lib); 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
13
Installing modules 1: from distribution Download the module, e.g. from www. cpan.org Download the module, e.g. from www. cpan.org Install the module Install the module perl Makefile.PL or perl Makefile.PL LIB=/put/module/here perl Makefile.PL or perl Makefile.PL LIB=/put/module/here make make make test make test make install make install This generally always works This generally always works
14
Installing modules with 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 1. If you are not the administrator you first configure cpan Now you can use cpan to install your modules with CPAN 2. Or my favorite : blow away your cpan configuration and start over >rm –rf ~/.cpan >perl -e shell -MCPAN
15
But it never works! Actually the installation always works flawlessly, but perl can’t find it! Actually the installation always works flawlessly, but perl can’t find it! Example script: Example script: The error message: 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_64-linux-thread-multi /home/mmi/fernando/myperl/lib64/perl5/site_perl/5.8.8/x86… #!/usr/bin/perl use foobar;
16
Where is the module? I was expecting it in: /home/mmi/fernando/myperl /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 /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 /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/… But the reality is it could be any number of subdirectories…
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.