Using Modules from CPAN (The Comprehensive Perl Archive Network)

If the modules in the standard Perl library don't have enough capabilities—and often, they don't—there is also CPAN. The Comprehensive Perl Archive Network, as I mentioned earlier, is a massive collection of publicly available modules for Perl; covering just about every topic you can imagine. Want a module to handle encrypting data? CPAN's got it. Need to send e-mail? CPAN's got a module to do that. Want to read in and process an entire HTML file? Not a problem. No matter what it is you want to do, it's a good idea to check CPAN first to see if someone has already done it for you. There's no point in reinventing the wheel when you can use someone else's code. That's the advantage of having a Perl community to rely on.

A Note of Caution

There are two drawbacks to the CPAN modules. The first is that you have to download, build, and install modules before you can use them, which means a bit more work involved than just inserting a use module line in your script. Some modules might require you to compile them, which means you'll need a working C compiler installed on your computer.

The second problem with modules on CPAN is that most of them are developed for use with Unix Perl. If you're using Windows or MacPerl, the module you want to use might not be available for that platform. This is changing as time goes on, however, and more and more modules are being developed cross-platform. Windows support, in particular, is becoming more and more widespread. There's even a special tool, PPM, for installing and managing Windows-specific CPAN modules for the ActiveState version of Perl for Windows (we'll look at PPM later in this lesson in the section entitled “Installing CPAN Modules on Windows Using PPM.”) If you're not sure if a particular module is available for your platform, you'll need to check the documentation for that module—or be prepared to port it yourself.

Acquiring Modules from CPAN

The CPAN modules are stored on the CPAN Web site or one of its mirrors. If you start at http://www.cpan.org/ you'll find out what CPAN contains, how to get the files, and how to find out if a particular module is available on your platform. There's also a module search engine available so you can figure out quickly if there is a module to do what you want.

Some modules come in bundles to reduce dependencies between different modules (there's nothing more irritating than trying to run a script, which needs one module, only to find after downloading that module that it requires another module, which then requires a third module, and so on down the line). Module bundles usually start with the word “lib”—for example, the libwww group of modules includes a whole set of modules for handling things relating to the World Wide Web. If the module you want is contained in a bundle available on CPAN, it's usually a good idea to download the whole bundle instead of the individual module, just to be safe.

Say you've found a module that you want to use. Usually, you'll download that module, uncompress or unarchive it using the tools for your platform (usually gzip and tar for Unix, WinZip for Windows, Stuffit for Mac). On Unix many modules include a makefile to install everything in the right spots (type perl Makefile.PL to get the process started). On Windows and the Mac, if there are not specific directions for installing the files on your platform you can sometimes just copy the .pm module files into the appropriate spots in your Perl hierarchy. The perlmodinstall man page part of the Perl installation offers many specific suggestions for getting modules decompressed and installed. The process, however, can be different for each module, so follow what the README files say to make sure everything is installed correctly.

Some modules have parts written in C that might require you to have a C compiler installed on your machine. If you don't have a C compiler, you might be able to use the module without these extra parts, depending on the module. Again, check the documentation that comes with the module.

If you're on Unix, and you're installing modules that have compiled parts, make sure that the C compiler you use to compile the modules is the same compiler you used to compile Perl in the first place (with network-mounted file systems this isn't as unusual a case as it sounds). You can run into difficult-to-solve incompatibilities with modules compiled in different environments than those with which Perl was compiled.

After you're done dearchiving, building, compiling, and installing, you should have a number of files installed into the right locations in your Perl hierarchy. You might want to explore the various directories in your @INC array to make sure they're there.

Installing CPAN Modules on Windows Using PPM

The Perl Package Manager, or PPM, is a tool that comes with the ActiveState version of Perl for Windows that makes installing and managing CPAN modules on Windows much, much easier. With PPM, you don't have to worry whether a particular module is supported on Windows, or figure out how to compile or install it using arcane Unix-based tools. PPM enables you to install, update, and remove individual already-built modules from inside a single program.

Note

A package, per the Perl Package Manager, is a collection of one or more modules and supporting files. It's different from a Perl namespace package.


To use PPM, you must be connected to the Internet. The PPM script gets its packages from a repository on the ActiveState Web site. To start PPM, simply type ppm from inside a command shell:

c:> ppm
PPM interactive shell (0.9.5) - type 'help' for available commands
PPM>

At the PPM prompt, you have several choices, including

  • help, to print a list of choices

  • search, to show which packages you have available to install

  • query, to show the packages you already have installed

  • install, to install a specific package

  • verify, to make sure all your packages are up to date

  • remove, to remove a specific package

You can find out more about PPM from the PPM Web page (part of ActiveState's Perl for Windows installation), or at http://www.activestate.com/activeperl/docs/ppm.html.

Using Modules from CPAN

After you have a module from CPAN installed—either by downloading and installing it yourself, using PPM, or even by copying files manually to your Perl installation—that module is available to your scripts. You can then import it with use and use its capabilities as if it were any other module. You'll need to check its documentation to see if it's a regular or object-oriented module, or to see if it uses import tags, but other than the fact that they need to be installed first, the CPAN modules typically behave the same as those in the standard library.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.33.207