Perl Programming/CPAN
From Wikibooks, the open-content textbooks collection
A huge collection of freely usable perl modules, ranging from advanced mathematics to database connectivity, networking and more, can be downloaded from a network of sites called CPAN. Most or all of the software on CPAN is also available under either the Artistic License, the GPL, or both. CPAN.pm is also the name of the perl module that downloads and installs other perl modules from one of the CPAN mirror sites; such installations can be done with interactive prompts, or can be fully automated.
Contents |
[edit] Installing modules
[edit] With ActivePerl (Windows systems)
From a command-line, type the command
ppm
This will give you a "Perl Package Manager" prompt, which allows you to download and install modules from the internet. For example, to install the Time::HiRes module, type:
search time::hires
That will give you a list of modules which match your search query. Once you know the module is available and what its exact name is, you can install the module with:
install Time::HiRes
[edit] With Perl
If you're using a normal version of Perl, the way to activate the package manager is this:
perl -MCPAN -e shell;
This will load the CPAN module, and let you search for, download, install, and manage the modules on your computer the same as PPM.
[edit] Using a module in your program
To incorporate a module into your program, use the Use keyword:
use Time::HiRes;
You can supply an optional list of the functions you want to use from this module, if you're worried that some of the function names in the module are too similar to functions you're already using:
use Time::Hires qw(time gmtime);
With that done, you can simply use the supplied functions as normal. Most modules have example programs within their documentation, and the best way to start using a module is to copy and adapt one of the example programs.
[edit] Finding documentation
The documentation for each module is installed in your documentation directory when you get a new module, or you can browse documentation on CPAN. To find module documentation on your computer, try looking in some of the following directories:
- c:\perl\html\lib
- c:\perl\html\site\lib
If you're having real trouble finding the HTML documentation for a module, you may be able to read the *.pm perl file yourself for POD comments, or use the POD2HTML tool yourself to generate the HTML file.
[edit] Contributing your own modules to CPAN
In the event that a module you need isn't available on CPAN, the usual answer is to write the module yourself and add it to CPAN. That way, nobody else needs to waste time creating the same functionality that you're already written.

