An In-Depth Look at Perl5 Modules

You're now ready to take an extended look at Perl5 modules. In the following sections, I'll give you a short history of the extensibility of Perl to demonstrate the usefulness of modules. Then we'll look at the general style and process for implementing a module.

The Short History of Perl Extensions

In earlier versions of Perl, a number of external APIs, or extensions, were added to Perl to give it additional features or functionality. This was accomplished by compiling the Perl source code with source code that "glued in" the desired functions from the API. Linking with the external libraries for the desired API then created a new Perl executable. The following are a few examples:

cursePerl: UNIX terminal control routines

tkPerl: Tk routines for screen graphics and widgets

sybPerl: Sybase database manipulation routines

Each of these executables had to be separately maintained by the person at the site who compiled Perl, and each time one of the APIs was revised, that person had to rebuild the complete API-specific Perl executable, relinking it with the new library and working out any problems that might show up with the newer version. Perl5 has outdated this tedious process. Nowadays, when you want to add features to Perl by linking with some external library, you use a completely different techniques.

Modules and Extensions: Purpose and Design

Version 5 of Perl implements the module as the standardized means for extending its functionality. A Perl module is very much like a Perl library in that it provides the user with a set of functions or variables, within a separate namespace known as a Perl package. The purpose of these functions or variables is to simplify and standardize the implementation of a given task or tasks.

Modules can be much more powerful and useful than the old-style Perl libraries, however. First, they can now be implemented as a shared library and thus, on many platforms, eliminate the need for a separate, statically linked Perl executable for each external API that is desired as a Perl extension. Second, they can be implemented to provide an interface for use in the new, object-oriented way. Extension Modules: Modules That Interface to External APIs A Perl5 module provides the implementor with the means, through the use of XSUBs, to compile C code that "wraps" or "glues" the functionality of an API into a shared library that can be loaded by Perl at runtime. After Perl loads the shared library, through a use or require statement, the glue code enables the end user to call the API's functions from within the Perl program, passing Perl data types in and out. This capability is one of the most powerful and useful new features in Perl5.

Note:

Not all architectures support shared libraries. Many do, but on the ones that don't, you must still link the API libraries with Perl to produce a Perl executable that provides the interface to the API.

Modules: The Object-Oriented Way Another purpose of modules, briefly demonstrated and discussed previously, is to provide the ability to use them combined with Perl references to create object-oriented programs. You'll see and use the object-oriented features of modules extensively in this tutorial to implement the examples and demonstrate the techniques that we consider to be the most up-to-date and cutting-edge for Web programming.

In the next section, you'll take a brief look at the general form of a module.