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 you can put to use. The most frequent way to add to what Perl can do is by using a library or module. Many come with Perl and others are available from CPAN. You can even write your own libraries and modules.

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.

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.

Though many useful modules are written in pure Perl, others are written using a language like C. For example, the MD5 algorithm is like a high-powered checksum.[379] It uses a lot of low-level bit twiddling that could be done in Perl but hundreds of times more slowly;[380] 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.

Finding and Installing Modules

Maybe your system already has ...

Get Learning Perl, Fourth Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.