EGTDC Perl Course 2004
Total Page:16
File Type:pdf, Size:1020Kb
EGTDC Perl Course 2004 Making use of CPAN modules Perl on other platforms Tim Booth : [email protected] Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk CPAN Recap • "Comprehensive Perl Archive Network". • Holds virtually every Perl module, among other things. • Access via the web (http) and by the CPAN shell. – search.cpan.org is probably your first point of call. • Most modules are standardised to make them easy to use right away: – Documentation in ©perldoc© format. – Standard installation procedure – Self-testing Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk CPAN and modules • CPAN contains around 2860 modules - bits of code that can be plugged into your scripts. Some are small, some complex. • Need to interface with a database? Use the DBI/DBD modules. • Need to make webpages with Perl? Use the CGI module. • Need to make a graphical interface? Use the Tk module. • Need to do bioinformatics? Use the BioPerl modules! • To use an installed module you "use module_name;" in the same way as you "use strict;" • Modules can be installed in 2 ways – Manually, from source – From CPAN, using the CPAN shell. Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Installing modules from source • On Bio-Linux, log in as manager to install modules system-wide • Modules are distributed as .tar.gz files. • Download the module to the manager©s home directory. • if you are missing components required for the module to work you will be told at the 2nd step below. • Always check README or INSTALL files before installing. tar -zxvf test_module.tar.gz cd test_module #or whatever directory was created perl Makefile.PL #note the capitalisation make make test sudo make install Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Installing modules with CPAN • sudo perl -MCPAN -e shell on the command line launches the CPAN interface. • CPAN shell is very clever as it automtically downloads, builds, tests, installs and resolves dependencies (ie if one perl module requires another). If you specify a module you already have then it will be updated to the latest version. • Bio-Linux has CPAN set up, for other systems you may need to configure it and install dependencies first. • Full CPAN documentation is here: http://theoryx5.uwinnipeg.ca/CPAN/data/CPAN/README.html • Great for getting BioPerl up and running fast: – install Bundle::BioPerl Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Installing modules as a regular user • You do not have to be the system administrator to install modules for your own use. • You may also want to test new or updated modules in a temporary directory. • CPAN can still help, but there is some fiddly setting up to do. You will probably find it easier to install the modules manually: tar -xvzf downloaded_module.tar.gz ; cd downloaded_module_dir perl Makefile.PL PREFIX=~/myperl make make test && make install • This puts the module in the ©myperl© subdirectory of your home dir. Don©t forget to include this in your @INC path to use the module! Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Setting the @INC path ● The global array @INC lists all the places where Perl will look for modules, but you should not modify it directly. For example, to use modules in your ~/perl/lib/5.8.0 directory, do one of the following: ● Set the PERL5LIB environment variable at the shell prompt: export PERL5LIB=$HOME/perl/lib/5.8.0 perl myscript.perl ● Run perl with the -I option perl -I$HOME/perl/lib/5.8.0 myscript.perl ● Put a ©use lib© statement in your script. #!/usr/bin/perl use lib “/home/user1/perl/lib/5.8.0”; use My::Module; ● Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Checking installed modules ● All good modules will self-test before they install, so you just need to verify that Perl can find the new module. Simply run: ● perl -e 'use Config::Simple' ● Specifying the name of the module to test. ● If this succeeds you will get no output, otherwise you will get an error. ● The error message lists the directories where Perl has looked for modules, and you will need to add the correct directory to this list. This will apply especially if you are installing modules in a user directory or some other temporary location. Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk CPAN as a regular user You can still use CPAN to install modules in your home directory, but it is fiddly! On Bio- Linux you would have to set up CPAN as follows: Make a local directory for CPAN: mkdir -p ~/.cpan/CPAN And generate a config file: sed -e "s/egdcmanager/$USER/" /usr/lib/perl5/5.8.0/CPAN/Config.pm >HOME/.cpan/CPAN/MyConfig.pm Start CPAN and set some settings: perl -MCPAN -e shell (now in the CPAN shell) o conf makepl_arg "PREFIX=~/perl/lib" o conf commit Now install your module: install Some::Module Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk Perl on Windows • If you want to use Perl - but have no UNIX machine - don©t panic! • Perl runs on Windows too (in fact, Perl runs on just about anything). • The most popular version comes from www.activestate.com • Download "ActivePerl" a free binary distribution of Perl for Windows. • Uses "ppm" the "perl package manager" to control module installation - it©s like the CPAN shell only 10x easier. • Use "notepad" or a similar text editor to write your scripts. • If you want to mimic a Unix environment in Windows for your programming needs, you might want to look at www.cygwin.com which gives a Linux like environment and development software you would expect on a Linux system. Environmental Genomics Thematic Programme Data Centre http://envgen.nox.ac.uk.