start page | rating of books | rating of authors | reviews | copyrights

Book HomeLearning Perl, 3rd EditionSearch this book

B.4. Extending Perl's Functionality

One of the most common pieces of good advice heard in the Perl discussion forums is that you shouldn't reinvent the wheel. Other folks have written code that you can put to use. The most frequent way to add to what Perl can do is by using a library or module. Many of these come with Perl, while others are available from CPAN. Of course, you can even write your own libraries and modules.

B.4.1. Libraries

Many programming languages offer support for libraries much as Perl does. Libraries are collections of (mostly) subroutines for a given purpose. In modern Perl, though, it's more common to use modules than libraries.

B.4.2. Modules

A module is a "smart library". A module will typically offer a collection of subroutines that act as if they were built in functions, for the most part. Modules are smart in that they keep their details in a separate package, only importing what you request. This keeps a module from stomping on your code's symbols.

Although many useful modules are written in pure Perl, others are written using a language like C. For example, the MD5 algorithm is sort of like a high-powered checksum.[401] It uses a lot of low-level bit-twiddling that could be done in Perl, but hundreds of times more slowly;[402] it's an algorithm that was designed to be efficiently implemented in C. So, the Digest::MD5 module is made to use the compiled C code. When you use that module, it's as if your Perl had a built in function to calculate MD5 digests.

[401]It's not really a checksum, but that's good enough for this explanation.

[402]The module Digest::Perl::MD5 is a pure Perl implementation of the MD5 algorithm. Although your mileage may vary, we found it to be about 280 times slower than the Digest::MD5 module on one sample dataset. Remember that many of the bit-twiddling operations in the C algorithm compile down to a single machine instruction; thus, entire lines of code can take a mere handful of clock cycles to run. Perl is fast, but let's not be unrealistic.

B.4.3. Finding and Installing Modules

Maybe your system already has the module you need. But how can you find out which modules are installed? You can use the program inside, which should be available for download from CPAN in the directory http://www.cpan.org/authors/id/P/PH/PHOENIX/.

If none of the modules already available on your system suits your needs, you can search for Perl modules on CPAN at http://search.cpan.org/. To install a module on your system, see the perlmodinstall manpage.

When using a module, you'll generally put the required use directives at the top of your program. That makes it easy for someone who is installing your program on a new system to see at a glance which modules it needs.

B.4.4. Writing Your Own Modules

In the rare case that there's no module to do what you need, an advanced programmer can write a new one, either in Perl or in another language (often C). See the perlmod and perlmodlib manpages for more information.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.