Saturday, August 23, 2008

CPAN - Installing Perl Modules

When you have been developing Perl CGI scripts for while you will invariable need to install one of the many useful modules located in the Comprehensive Perl Archive Network.
CPAN is a group of servers around the world which provide access to Perl source code, and hundreds of Modules that have been contributed by volunteers.

Installing Modules

Part of what makes CPAN powerful, is that Perl supports it directly with the CPAN.pm module which is distributed with Perl. Many of the database projects we have developed could not of been possible without the templating system HTML::Template.

On most operating systems, you can install a CPAN module by typing the following from the command line :-

perl -MCPAN -e install HTML::Template

where HTML::Template is the name of the module you wish to install.

This command will automatically find, download, compile, and install the module onto your system.

If this is the first time you have run the command on your system, you may be prompted with multi-choice questions, so that the CPAN module can find the nearest FTP server.

Activestate Perl and Windows 32 Systems

If you are using ActiveState Perl on a windows based server, then you can use the PPM command-line utility.

from the /bin sub folder of perl type:- ppm
then type:- install HTML::Template

Using CPAN modules without Root access

If you are using a web hosting account which doesn't give you access to the operating system, so that you can install perl modules as described above, then you can still use modules by setting up your own lib folder.

Typically the best place to locate the lib folder is under you cgi-bin directory. The tree structure below shows where you would need to upload the Template.pm file to use Html::Template.

The module name will determine where the .pm files need to be located under your lib directory. The text prior to the :: will become the directory name.






For example the Calc.pm module that comes with Date::Calc will need to go into a sub-directory of lib called Date.



To use the module within a Perl script, you need to call the Lib module, passing it the full system path to your lib folder. You can then use the modules in the normal way,









see the example code below. Please note some CPAN modules rely on other modules being present, and if thats the case then you will need to upload them as well.

#/path/to/perl -w
use strict;

use lib(/full/path/to/cgi-bin/lib);
use Html::Template;

.. rest of code

No comments: