Downloading and using libraries

From BITS wiki
Jump to: navigation, search
Go back to Perl introductionary training#Exercises

Writing your own code in Perl is fine, but it is not rare that you do not have to do the effort yourself, because someone else did it already. There exist a lot of programs and libraries with subroutines that are available for free download all over the world. The first place to search is the Comprehensive Perl Archive Network (CPAN), a distribution site maintained by a network of enthusiast Perl developers. Note that the Perl packages at CPAN are in a format that is suited for being installed under UNIX/Linux/MacOS X, but the Strawberry Perl site and ActiveState maintain a collection of ports for Windows.

There is a lot to be found at CPAN. We propose you just one example, which might amuse the chemists amongst you : a program that interconverts name, symbol and atomic number of chemical elements.

First take a look at http://www.cpan.org. Note that there exists a series of "mirror" sites (see "Mirrors" in the blue menu bar) ; using a nearby mirror could be more efficient. There are several ways to search for the program you want. You can follow the link "Modules" in the blue menu bar and scroll down to the "How to find modules" section. You can also use the "Search:" box in the upper right. Type elements in the search box and "Search". You will obtain a list with probably as second or third item "Chemistry::Elements - Perl extension for working with Chemical Elements". You can follow the link and take a look to convince yourself that this is indeed what we are looking for. Do not download the program because we need the version for Windows.

Actually, obtaining CPAN Perl modules for Strawberry Perl is easy. The distribution of Strawberry Perl (and its derivative DWIMP) comes with an already installed Perl module called cpan that automates the installation. Simply type in the "DOS box" :

cpan Chemistry::Elements

cpan will connect through the Internet to the Strawberry Perl home site and try to find and retrieve the package you asked for. Do note by the way that some programs and libraries are dependant (via use commands) on other libraries, which could on their turn be dependent on still other libraries. It can be quite tedious to make sure that you did install them all. The nice thing is that cpan does take care of these so-called dependencies.

Write the following program element.pl :

#!/usr/bin/perl

use strict;
use Chemistry::Elements;

my $input = @ARGV[0];
my $element = Chemistry::Elements->new($input);
my ($symbol, $name, $Z);
if ($element) {
  $symbol = $element->symbol;
  $name = $element->name;
  $Z = $element->Z;
  print "  $name  $symbol  Z=$Z\n";
} else {
  print "$input unknown\n";
}

You can try it out by running elements.pl with arguments Fe, fe (case insensitive), sulfur, sulphur (typo error !) or 118 (the heaviest element).

Some explanation :

  • The syntax Chemistry::Elements means that "Elements" is located inside "Chemistry" and that the Perl interpreter should search in one of the library directories (here most likely C:\Perl64\site\lib) for a subdirectory Chemistry and in Chemistry for a file Elements.pm.
  • The library (as most libraries on CPAN) contains objects. Writing objects is beyond the scope of this introductory course, but you do not need to know how to write them in order to be able to use them. The statement $element = Chemistry::Elements->new(...) uses the method "new" of the class "Chemistry::Elements" to create a new object called $element (note that in Perl "creator" methods are not obligatorily called "new"). In this case the method "new" will try to match the input you provide to one of the element names, symbols or numbers present in the code of the library. We then proceed by using the method "symbol" of the class "Chemistry::Elements" to retrieve the symbol from the object called $element, etc.

You can find out how to use the library by doing :

perldoc Chemistry::Elements

If you are curious to see how such a library looks like you can take a look at the file C:\Dwimperl\perl\site\lib\Chemistry\Elements.pm.

If you are working under MacOS X or Linux instead of Windows

You can there too use cpan, which will download from the CPAN site.

Alternatively you can download Chemistry-Elements-1.07.tar.gz from the CPAN site and do :

gunzip Chemistry-Elements-1.071.tar.gz
tar xf Chemistry-Elements-1.071.tar
cd Chemistry-Elements-1.071
perl Makefile.PL	(ignore the warning about "LICENCE")
make	(ignore the eventual warning about not being able to write .../man3/...)
make install		(you need to do this as "superuser")

Instead of "installing" the library you can also let it where it is and in your program put a "shebang" line :

#!/usr/bin/perl -IChemistry-Elements-1.071/blib/lib

The -I parameter tells the Perl interpreter to look in this folder/directory for library files (on top of the current directory and the list of directories defined in the Perl installation). You can put several -I parameters in one command line.

Go back to Perl introductionary training#Exercises