Perl Programming/CPAN< Perl Programming
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.
- 1 Installing modules
- 2 Using a module in your program
- 3 Finding documentation
- 4 Contributing your own modules to CPAN
With ActivePerl (Windows systems)Edit
From a command-line, type the command
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:
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:
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.
With Perl (cpanm)Edit
The Perl module cpanm (CPAN Minus) is another alternative for installing modules from the CPAN library http://search.cpan.org/~miyagawa/App-cpanminus-1.1001/lib/App/cpanminus.pm.
cpanm can be installed and used like this on a UNIX-like system:
curl -L "http://cpanmin.us" >cpanm chmod +x cpanm ./cpanm LWP::Bundle
One must have root privileges in order to install module in the system-wide directories, however alternatives exist such as local::lib, which allows regular users to install and use Perl modules in their home folder http://search.cpan.org/~getty/local-lib-1.006007/lib/local/lib.pm.
With Strawberry Perl (Windows systems)Edit
Strawberry Perl also includes the CPAN module, so you can use the command above to activate the package manager.
The start menu, however, also includes a shortcut (with the name of "CPAN Client") so that you don't have to go to a command line to do so.
A number of modules are already included in Strawberry Perl, beyond what comes with a normal version of Perl, or what comes with ActivePerl, so you may wish to check if the module you want is already installed before you start the CPAN client.
Using a module in your programEdit
To incorporate a module into your program, use the Use keyword:
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.
On Unix systems, the documentation is usually installed as man pages in section 3p, so that the command below will work:
man 3p Module::Name
perldoc Module::Name will also work.
If you want documentation that is browseable in a web browser, you can install Perldoc::Server as noted below.
Windows systems running ActivePerlEdit
Module documentation is installed as HTML files in ActivePerl. To find those files, try looking in some of the following directories:
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.
Windows systems running Strawberry PerlEdit
Strawberry Perl does not install module documentation as either manpages or html files. Instead, you can run the perldoc command to display module documentation.
You can also use Perldoc::Server to display module documentation, as illustrated below.
The Perldoc::Server module (which can be installed via CPAN) will provide a local server that will display html files "on the fly" from Perl's documentation and the documentation for installed modules. Install it, and the command
will be in your path. Run it, and then browse to http://localhost:7375/ in your web browser to see the documentation.
Note that the perldoc-server command must be running to provide the documentation using this method.
Contributing your own modules to CPANEdit
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.