I always like to say that 90% of Perl is its modules. Back in 2000 when I was working as a junior Perl programmer I was asked to write a web application that, among other things, could send contact messages through email. Unfortunately, I never had anyone to really teach me the Path of Perl – I only relied on Learning Perl by Randall Schwartz, and whatever I could find on the net. I had a really hard time with that application, mainly because I didn’t know about Perl modules, MySQL and SQL language. Had I been familiar with at least the Perl modules part, I wouldn’t have had to spend 8 days and nights in the office (including my birthday). I didn’t even know how to
use strict; at the time! Keep reading if
use strict; makes no sense to you.
Being the extensible and flexible language that it is, Perl provides us with some safeguards and helpers to assist in avoiding what happened to me (I wish I knew that back then). The first of which I’ll talk about is Pragmas.
Pragmas are special modules that come installed by default in every Perl distribution. They tell the interpreter of how it is supposed to act. To turn them on, all you have to do is call the special word
use with the appropriate Pragma. To turn them off, call
no and the Pragma in question. The most common and powerful Pragma is, in my opinion,
strict (hence the name of this blog:
Strict tells the Perl interpreter that all variables must be declared and tightens up security a notch. To
use strict; you have to have at least working knowledge of lexical variables. It takes a while to getting used to at first, but once you’re hooked, you won’t know how you could possibly have written Perl programs without it before (I know I don’t).
#!/usr/bin/perl $var = 1; # OK use strict; $var1 = 2; # compile time error
In our example above, Perl will refuse to run, raising a compile time error like such:
Global symbol “$var1” requires explicit package name at example.pl line 5.
Execution of example.pl aborted due to compilation errors.
strict in effect. Notice that
$var was not cited, since
strict was only enforced below it. In order to bypass that error, we should have declared our variable with either
local – depending on the need.
my is the most common. Look up “Packages, Namespaces, and Lexical scopes” for more on those 3 operators.
#!/usr/bin/perl $var = 1; # OK use strict; my $var1 = 2; # OK
use strict; is so important that it is usually the second line of code in any decent Perl program – the first line being the shebang.
If you need to turn off
strict for one reason or another, you can do so with the key word
#!/usr/bin/perl use strict; my $var = 1; # OK no strict; $var1 = 2; # also OK
When writing your Perl programs, it’s also good to turn on
warnings will complain about possible problems such as useless uses of certain functions.
diagnostics, on the other hand, will throw you a truckload of information regarding errors. It’s a good place to start when you’re stumped.
Perl modules are pieces of code or packages that can be imported into your script with the keyword
use in the same way as Pragmas. They can be Object Oriented, Procedural, or both. I will not discuss how to write a module in this post, but I will tell you where to find them for download and how to install them.
My all time favorite module is Data::Dumper. So I will use it in the following examples. The funny
:: between Data and Dumper is kind of like a directory separator. The module Dumper resides in the directory Data, found in one of the paths configured in the Perl config files or the PERL5LIB environmental variable (which set the @INC array).
Data::Dumper comes installed by default, along with hundreds other modules that the developers deemed worthy. To test that your Perl distro has it, run the following command in a command line:
$ perl -MData::Dumper -e 'print "OK\n"'
You’ll most definitely see the
OK being printed on your screen. The command passes 2 parameters to the Perl interpreter:
-M which tells it to load a module (in this case Data::Dumper – no spaces between -M and the module name, or you’ll get a “missing argument” error), and
-e which tells it to execute a piece of code (we told it to print OK, but any valid piece of code would do).
Look at what would have happened if we tried to load a module that wasn’t installed:
$ perl -Maaaa -e 'print "OK\n"'
Can’t locate aaaa.pm in @INC (@INC contains: /usr/lib/perl5/5.10/i686-cygwin /usr/lib/perl5/5.10 /usr/lib perl5/site_perl/5.10/i686-cygwin /usr/lib/perl5/site_perl/5.10 /usr/lib/perl5/vendor_perl/5.10/i686-cygwin /usr/lib/perl5/vendor_perl/5.10 /usr/lib/perl5/vendor_perl/5.10 /usr/lib/perl5/site_perl/5.8 /usr/lib/perl5/vendor_perl/5.8 .).
BEGIN failed–compilation aborted.
To install a module, you can do it the hard way or the easy way. Some modules are harder than others, so I’ll stick with the easier ones for now. Let’s start with the hard way.
First, you should know where to find your module. CPAN – The Comprehensive Perl Archive Network is the place to go to get your modules. It has a handy search engine which will search through almost 16000 modules at the time of this writing (5/2009). There you will find code ranging from the most trivial to the craziest needs. If there’s one thing that CPANs contributors don’t lack, it’s creativity (that’s a compliment).
Enter a keyword in the search engine and it will list all relative modules. Click on the link to the one that interests you most. You will be shown its documentation in POD (Plain Old Documentation) format. That’s the data you will most likely need to learn how to use your module. It should also be the most up-to-date information, since it’s kept by its authors.
At the top of the POD screen, you’ll see a breadcrumb set of links showing the Author’s name, the module’s distribution name and version, and the module name itself:
Click on the link for the module’s distribution name and version, to go to the distribution details screen. You will see lots of information about the module and its sub-modules, but what should concern you right now is the download link next to the release name. Click on it and download the module tarball.
Now this is where I halt and tell you that potential headaches lie ahead. There are basically 2 kinds of modules: PurePerl ones and C-based ones. PurePerl modules are just that – modules that are solely written in Perl. The vast majority of modules, however, are written in C with bindings special for Perl. Those usually have to be compiled and as such, need a C/C++ compiler and a
make program. The good news is that most *nix systems come with those tools already installed or readily available. Windows systems, however, require that you install nmake and preferably Microsoft Visual C++. Check out my post about installing MQSeries module on Windows for more information on how to get Microsoft Visual C++
Back to installing the module…
Unpack your module in some directory where you have full access. I like to keep an untouched copy of the tarball, by reading the gunzipped contents and throwing it to the screen with the
-c option, and piping it to tar with
xvf - parameters:
gunzip -c tarball.tar.gz | tar -xvf -
You’ll end up with a directory having the name of your distribution. Go in there and read all README files you can find. Read the INSTALL files if any.
One of the files you’ll see in the directory is
Makefile.PL. That’s the kickoff file for the installation. It takes the following optional parameters: PREFIX, LIB, and INC, and creates a
makefile tailor made for your system. It’s important to set the PREFIX parameter if you want the module installed in a place other than the default. LIB and INC point to the C lib and include directories, respectively.
Now that you have your
makefile, it’s just a matter of running
make test, and finally
make install. Depending on the module, you should have no issues whatsoever. Other more “sensitive” modules, however, often require hours of work and even some tweaking of the makefiles by hand. DBD::Oracle is by far the craziest module I’ve ever had to install. In some machines it’s a piece of cake, and others it manages to amuse with the amount of errors it pulls out of the hat. Anyway…
That was the hard way. Now for the easy way.
If you’re finding it strange to see the CPAN title here when I’ve already talked about it above, don’t worry – I’m talking about the application CPAN and not the website.
Perl comes with the CPAN module installed, and most of the time it also creates a script in the bin directory called
cpan. It’s an interactive shell that allows you to fetch information regarding authors and modules, and also allows you to install modules without having to go through the whole process of downloading the distribution, unpacking, etc.
Start up the CPAN application by calling
$ perl -MCPAN -e shell
If it’s the very first time you call it, you will be promped to answer a series of questions. Sticking to the default values is almost always OK. One of the first questions you will be asked is if you want CPAN to configure everything automatically. I recommend against it unless you know that the defaults are correct and will enable you to successfully install modules. And if you know that, then you probably already know how to use the CPAN application and this post has nothing new.
Once the questions have been answered, you will be presented with a cpan prompt. Type
? to know what options you have.
cpan> ? Display Information (ver 1.9205) command argument description a,b,d,m WORD or /REGEXP/ about authors, bundles, distributions, modules i WORD or /REGEXP/ about any of the above ls AUTHOR or GLOB about files in the author's directory (with WORD being a module, bundle or author name or a distribution name of the form AUTHOR/DISTRIBUTION) Download, Test, Make, Install... get download clean make clean make make (implies get) look open subshell in dist directory test make test (implies make) readme display these README files install make install (implies test) perldoc display POD documentation Upgrade r WORDs or /REGEXP/ or NONE report updates for some/matching/all modu les upgrade WORDs or /REGEXP/ or NONE upgrade some/matching/all modules Pragmas force CMD try hard to do command fforce CMD try harder notest CMD skip testing Other h,? display this menu ! perl-code eval a perl command o conf [opt] set and query options q quit the cpan shell reload cpan load CPAN.pm again reload index load newer indices autobundle Snapshot recent latest CPAN uploads
o conf to display the parameters to which you answered all those questions. You can change them with
o conf as well. If you didn’t enable auto-commit before, you will have to call
o conf commit to save your changes for use in future sessions.
o conf http_proxy "http://some_proxy.com:80"
m command allows you to fetch information regarding a certain module.
cpan> m Data::Dumper .... Possibly some data about fetching updated files from the internet here .... Module id = Data::Dumper DESCRIPTION Convert data structure into perl code CPAN_USERID GSAR (Gurusamy Sarathy <firstname.lastname@example.org>) CPAN_VERSION 2.121 CPAN_FILE I/IL/ILYAM/Data-Dumper-2.121.tar.gz DSLIP_STATUS SdpOp (standard,developer,perl,object-oriented,Standard-Perl) MANPAGE Data::Dumper - stringified perl data structures, suitable for b oth printing and C INST_FILE /usr/lib/perl5/5.10/i686-cygwin/Data/Dumper.pm INST_VERSION 2.121_14
In the output above, CPAN tells us that we alreay have Data::Dumper version 2.121_14 installed. If you are not sure what is the exact name of a module, use the
i command to fetch information using a regex:
cpan> i /klingon/ Distribution JALDHAR/DateTime-Event-Klingon-1.0.1.tar.gz Distribution PNE/Lingua-Klingon-Collate-1.03.tar.gz Distribution PNE/Lingua-Klingon-Recode-1.02.tar.gz Distribution PNE/Lingua-Klingon-Segment-1.03.tar.gz Module DateTime::Event::Klingon (JALDHAR/DateTime-Event-Klingon-1.0.1.tar.gz) Module Lingua::Klingon::Collate (PNE/Lingua-Klingon-Collate-1.03.tar.gz) Module Lingua::Klingon::Recode (PNE/Lingua-Klingon-Recode-1.02.tar.gz) Module Lingua::Klingon::Segment (PNE/Lingua-Klingon-Segment-1.03.tar.gz) 8 items found
Once you’re happy with the module name, you can check if it’s installed or not using
cpan> m Lingua::Klingon::Collate Module id = Lingua::Klingon::Collate CPAN_USERID PNE (Philip Newton <email@example.com>) CPAN_VERSION 1.03 CPAN_FILE P/PN/PNE/Lingua-Klingon-Collate-1.03.tar.gz INST_FILE (not installed)
Install it with
cpan install Lingua::Klingon::Collate
Now, unless you already have module Test::Differences installed, Lingua::Klingon::Collate will fail with a dependency error. Not all modules are like that. Some are coded in a way that CPAN actually asks you if you want to follow and install dependencies automagically. Those are a cinch to install.
If something goes wrong, look at the output of the installation. Best case scenario, you’re just missing another module and it didn’t warn you about it. For example, after Lingua::Klingon::Collate failed with the warning that I should have Test::Differences installed, I tried to install that dependency directly. It also failed. When looking at the output on the screen, I see a bunch of lines like this:
t/regression..........Can't locate Text/Diff.pm in @INC (@INC contains: /home/vi alves/.cpan/build/Test-Differences-0.4801-lI_xia/blib/lib /home/vialves/.cpan/bu ild/Test-Differences-0.4801-lI_xia/blib/arch /usr/lib/perl5/5.10/i686-cygwin /us r/lib/perl5/5.10 /usr/lib/perl5/site_perl/5.10/i686-cygwin /usr/lib/perl5/site_p erl/5.10 /usr/lib/perl5/vendor_perl/5.10/i686-cygwin /usr/lib/perl5/vendor_perl/ 5.10 /usr/lib/perl5/vendor_perl/5.10 /usr/lib/perl5/site_perl/5.8 /usr/lib/perl5 /vendor_perl/5.8 .) at /home/vialves/.cpan/build/Test-Differences-0.4801-lI_xia/ blib/lib/Test/Differences.pm line 213.
You’ll notice that the error is the same as when we did the
$ perl -Maaaa -e 'print "OK"'. Module Text/Diff.pm (or namely Text::Diff) is not installed. So we now go on that quest of following dependencies by hand.
It so happens that Test::Differences tried to install Text::Diff, but Text::Diff failed during the test phase. Suppose I know that those failed tests are not important and won’t hinder the results of the rest (I don’t, but bare with me), I can force CPAN to disregard the test failures:
cpan> force install Text::Diff Running install for module 'Text::Diff' Running make for R/RB/RBS/Text-Diff-0.35.tar.gz Has already been unwrapped into directory /home/vialves/.cpan/build/Text-Diff- 0.35-B8Qsuo Has already been made Running make test /usr/bin/perl.exe "-MExtUtils::Command::MM" "-e" "test_harness(0, 'blib/lib', 'b lib/arch')" t/*.t t/ext_format......ok t/general.........Use of /g modifier is meaningless in split at t/general.t line 129. Use of /g modifier is meaningless in split at t/general.t line 130. t/general.........ok t/inputs..........ok t/keygen..........ok t/outputs.........1/8 No such file or directory at t/outputs.t line 12, line 6. t/outputs......... Dubious, test returned 2 (wstat 512, 0x200) Failed 4/8 subtests t/table...........ok Test Summary Report ------------------- t/outputs.t (Wstat: 512 Tests: 4 Failed: 0) Non-zero exit status: 2 Parse errors: Bad plan. You planned 8 tests but ran 4. Files=6, Tests=29, 1 wallclock secs ( 0.04 usr 0.04 sys + 0.59 cusr 0.26 csy s = 0.93 CPU) Result: FAIL Failed 1/6 test programs. 0/29 subtests failed. make: *** [test_dynamic] Error 255 RBS/Text-Diff-0.35.tar.gz /usr/bin/make test -- NOT OK //hint// to see the cpan-testers results for installing this module, try: reports RBS/Text-Diff-0.35.tar.gz Running make install Installing /usr/lib/perl5/site_perl/5.10/Text/Diff.pm Installing /usr/lib/perl5/site_perl/5.10/Text/Diff/Table.pm Writing /usr/lib/perl5/site_perl/5.10/i686-cygwin/auto/Text/Diff/.packlist Appending installation info to /usr/lib/perl5/5.10/i686-cygwin/perllocal.pod RBS/Text-Diff-0.35.tar.gz /usr/bin/make install -- OK Failed during this command: RBS/Text-Diff-0.35.tar.gz : make_test FAILED but failure ign ored because 'force' in effect
Now I can go on to installing Text::Differences and finally Lingua::Klingon::Collate.
cpan install Test::Differences .... some output here .... Appending installation info to /usr/lib/perl5/5.10/i686-cygwin/perllocal.pod OVID/Test-Differences-0.4801.tar.gz /usr/bin/make install -- OK cpan install Lingua::Klingon::Collate Running install for module 'Lingua::Klingon::Collate' Running Build for P/PN/PNE/Lingua-Klingon-Collate-1.03.tar.gz Has already been unwrapped into directory /home/vialves/.cpan/build/Lingua-Kli ngon-Collate-1.03-MX7FZn -- No Build created, won't make Running Build test Make had some problems, won't test Running Build install Make had some problems, won't install
Ok, that happens. It’s because of the previous bad attempt. We can bypass that by telling CPAN to restart the build from scratch. To do so, it must first clean the build environment for that module.
cpan clean Lingua::Klingon::Collate
That usually does the trick and enables you to run the
install again, but if it doesn’t, you can
force get Lingua::Klingon::Collate to get a fresh package.
cpan> install Lingua::Klingon::Collate Running install for module 'Lingua::Klingon::Collate' Running Build for P/PN/PNE/Lingua-Klingon-Collate-1.03.tar.gz Has already been unwrapped into directory /home/vialves/.cpan/build/Lingua-Kli ngon-Collate-1.03-ZA4a1s CPAN.pm: Going to build P/PN/PNE/Lingua-Klingon-Collate-1.03.tar.gz Checking whether your kit is complete... Looks good Checking prerequisites... Looks good Creating new 'Build' script for 'Lingua-Klingon-Collate' version '1.03' Copying lib/Lingua/Klingon/Collate.pm -> blib/lib/Lingua/Klingon/Collate.pm Manifying blib/lib/Lingua/Klingon/Collate.pm -> blib/libdoc/Lingua.Klingon.Colla te.3pm HTMLifying blib/lib/Lingua/Klingon/Collate.pm -> blib/libhtml/site/lib/Lingua/Kl ingon/Collate.html ./Build: blib/lib/Lingua/Klingon/Collate.pm: cannot resolve L<strcoll(3)> in par agraph 60. ./Build: blib/lib/Lingua/Klingon/Collate.pm: cannot resolve L<strxfrm(3)> in par agraph 60. PNE/Lingua-Klingon-Collate-1.03.tar.gz ./Build -- OK Running Build test t/01_base...........ok t/02_strcoll........ok t/03_strxfrm........ok t/04_strunxfrm......ok t/05_list...........ok All tests successful. Files=5, Tests=87, 1 wallclock secs ( 0.05 usr 0.03 sys + 0.52 cusr 0.25 csy s = 0.85 CPU) Result: PASS PNE/Lingua-Klingon-Collate-1.03.tar.gz ./Build test -- OK Running Build install Prepending /home/vialves/.cpan/build/Lingua-Klingon-Collate-1.03-ZA4a1s/blib/arc h /home/vialves/.cpan/build/Lingua-Klingon-Collate-1.03-ZA4a1s/blib/lib to PERL5 LIB for 'install' Installing /usr/lib/perl5/site_perl/5.10/Lingua/Klingon/Collate.pm Installing /usr/share/man/man3/Lingua.Klingon.Collate.3pm Installing /usr/share/doc/perl-5.10.0/html/html3/site/lib/Lingua/Klingon/Collate .html Writing /usr/lib/perl5/site_perl/5.10/i686-cygwin/auto/Lingua/Klingon/Collate/.p acklist PNE/Lingua-Klingon-Collate-1.03.tar.gz ./Build install -- OK
q to exit the shell and that’s it! There are many other options when using CPAN, but what you’ve seen so far in this post should be enough to get you started. Just remember to use
? every now and then to see what powers are offered to you.