Developer

Tap into the power of CPAN

CPAN is a tremendous resource that any Perl developer will find useful. Learn how to install CPAN modules and put them to work in your scripts.


Comprehensive Perl Archive Network (CPAN) is the centralized source for Perl modules and extensions. Hundreds of useful resources are available in this repository, free for the taking. Besides being community driven, these resources are easily accessible, thanks to the CPAN search tools available for Perl. Read below to learn about some of these tools and see how to use CPAN modules in your Perl scripts.

CPAN.pm script for UNIX and Windows
The Perl binary distribution comes bundled with the CPAN.pm script. This utility connects to the nearest CPAN mirror site and facilitates downloading, compiling, and installing CPAN modules. At the command prompt in either UNIX or Windows, type:
perl –MCPAN –e shell

This will enter the CPAN utility in interactive mode, where you can automatically install and update modules by issuing commands. You can get a list of available commands by typing h at the cpan> prompt. If this is the first time you’ve used CPAN.pm, you may be presented with the choice to walk through a configuration wizard or have the script autodetect values. Either method should work.

CPAN.pm on Windows
In order for CPAN.pm to download distribution files properly, you must make sure that your Perl distribution is installed in a path that does not contain spaces in the directory names. For easiest configuration all around, stick to the default install directory, C:\Perl.

Once you’re configured, you can begin looking for modules. To get a list of available distribution files, you need to have some idea of what you’re looking for. Browse the CPAN search Web page for quick access to module information. Alternatively, you can search within the CPAN.pm interactive shell for information about authors, bundles, distributions, and modules by entering the appropriate letter: a, b, d, m, or i to search all information, followed by a text string or regular expression. To get a list of all available bundles having to do with database interfaces, for example, enter the following at the cpan> prompt:
b /db/

The regular expression, contained within backslashes, refines your search of bundle records at the selected CPAN site. Once you’ve found the distribution you’re looking for, you can use the readme command to see associated documentation:
readme DBI

CPAN.pm will search mirror sites for the information and then download and display it. Type q to exit the readme file and return to the cpan> prompt. On Linux, you can enhance your searching capabilities by installing the CPAN::WAIT module.

Look before you leap
Before installing a Perl module from CPAN, check its listing to make sure the functionality is supported for your platform.

Once you’ve found the distribution you require, you can use the install command to see whether you already have the requested module version and if not, to download, make, test, and install the component. For example, to get the latest updates for the CPAN.pm script and all the modules it depends on, enter the following command at the cpan> prompt:
install Bundle::CPAN

This will step through the automated install process for all recommended modules associated with the bundle. CPAN.pm maintains records for only the current session. To see all modules installed on your system, exit the CPAN interface by typing q, and type the following at the command line:
perldoc perllocal

If your installation went smoothly, you should see CPAN listed along with any other modules you’ve installed. If an expected module doesn’t appear, try installing it again. Occasionally, server lag and other circumstances can cause mirror sites to be inaccessible.

To learn more about the CPAN.pm script and its functions, see the CPAN page in the Perl manualor CPAN’s CPAN.html page.

ActiveState's PPM tool for Windows
For Windows users, ActiveState is a popular source for the Perl distribution, called ActivePerl. This installation includes the Programmer’s Package Manager (PPM) tool used to search the ActiveState repository, which contains modules safe for use on Windows. PPM functions similarly to CPAN.pm, and you can use the repository add command to point to a different source, even a CPAN mirror.

To run PPM, enter ppm or ppm3 at your DOS prompt. Once you’re in the interactive shell, available commands include search, query, install, and remove, providing a somewhat friendlier environment than CPAN.pm.

For detailed information on using the PPM tool, visit the ActivePerl User Guide.

Webmin
If you think you must have a visual tool, try installing Webmin. This utility is used for UNIX system administration and requires a Web server and root user access.

Besides being able to manage DNS, Apache, SSL, logs, and many other services, it comes bundled with a Perl modules manager. This interface shows which modules are installed, has links to their readme files, and allows you to search CPAN and to download and install new modules.

The interface is self-explanatory and convenient, assuming, of course, that you are responsible for maintaining your server. You can download Webmin for free, and you'll find limited but useful documentation at the Webmin site.

Long division
There are, of course, those who have to do everything the hard way. You can bypass the utilities described above and download and install CPAN modules manually. This might be useful if, for example, you one day need to install CPAN.pm because it happened to disappear from your server.

To start, find the module you’re interested in from the CPAN search page. Click on the Latest Release link at the top of the module’s page to download it. From there, follow these instructions, linked from the CPAN FAQ, for unpacking, building, and installing the module.

If you decide to install your modules manually, remember that each module was written by a different author, and may have different conventions within the makefile, or may not even require building. Be sure to read the readme file, and any other documentation provided with a module before attempting to install it.

Using a CPAN module
Once you have installed a CPAN module, it is immediately available for use within your scripts. Simply include a line like the following at the top of your file:
use module_name;

The module_name is the actual name, such as XML::Parser or DBI. You can find the module’s name by looking at the CPAN search results page.

Once you’ve called the module in this manner, all methods and classes that are a part of the module are available for use in your code. Follow the distribution’s documentation to learn what functions are available.

CPAN at your fingertips
You can use any of the methods described here to easily download and install CPAN modules. With a large portion of the Perl community behind your development efforts, you can create powerful, robust Perl applications.

Got a hot tip on a great CPAN module?
If you know of a CPAN module that made your life easier, post a comment about it in the discussion below or send our editors an e-mail.

 

Editor's Picks

Free Newsletters, In your Inbox