12. Packages, Libraries, and Modules
Contents:
Like all those possessing a library, Aurelian was aware that he was guilty of not knowing his in its entirety. - Jorge Luis Borges The Theologians 12.0. Introduction
Imagine that you have two separate programs, both of which work fine by themselves, and you decide to make a third program that combines the best features from the first two. You copy both programs into a new file or cut and paste selected pieces. You find that the two programs had variables and functions with the same names that should remain separate. For example, both might have an
The solution to this problem is
packages
. Perl uses packages to partition the global namespace. The package is the basis for both traditional modules and object-oriented classes. Just as directories contain files, packages contain identifiers. Every global identifier (variables, functions, file and directory handles, and formats) has two parts: its package name and the identifier proper. These two pieces are separated from one another with a double colon. For example, the variable
Where the filesystem uses slashes to separate the directory from the filename, Perl uses a double colon (prior to release 5.000, you could only use a single quote mark, as in
package Alpha; $name = "first"; package Omega; $name = "last"; package main; print "Alpha is $Alpha::name, Omega is $Omega::name.\n"; Alpha is first, Omega is last.
Unlike user-defined identifiers, built-in variables with punctuation names (like Modules
The unit of software reuse in Perl is the
module
, a file that has a collection of related functions designed to be used by other programs and library modules. Every module has a public interface, a set of variables and functions that outsiders are encouraged to use. From inside the module, the interface is defined by initializing certain package variables that the standard Exporter module looks at. From outside the module, the interface is accessed by importing symbols as a side effect of the
The
Modules included with
The other difference between
The required file extension for a Perl module is If the module name itself contains one or more double colons, these are translated into your system's directory separator. That means that the File::Find module resides in the file File/Find.pm under most filesystems. For example: require "FileHandle.pm"; # run-time load require FileHandle; # ".pm" assumed; same as previous use FileHandle; # compile-time load require "Cards/Poker.pm"; # run-time load require Cards::Poker; # ".pm" assumed; same as previous use Cards::Poker; # compile-time load Import/Export RegulationsThe following is a typical setup for a hypothetical module named Cards::Poker that demonstrates how to manage its exports. The code goes in the file named Poker.pm within the directory Cards : that is, Cards/Poker.pm . (See Recipe 12.7 for where the Cards directory should reside.) Here's that file, with line numbers included for reference:
1 package Cards::Poker;
2 use Exporter;
3 @ISA = ('Exporter');
4 @EXPORT = qw(&shuffle @card_deck);
5 @card_deck = (); # initialize package global
6 sub shuffle { } # fill-in definition later
7 1; # don't forget this
Line 1 declares the package that the module will put its global variables and functions in. Typically, a module first switches to a particular package so that it has its own place for global variables and functions, one that won't conflict with that of another program. This
must
be written exactly as the corresponding
Don't say
Line 2 loads in the
Exporter module, which manages your module's public interface as described below. Line 3 initializes the special, per-package array
Line 4 assigns the list
Lines 5 and 6 set up the package global variables and functions to be exported. (We presume you'll actually flesh out their initializations and definitions more than in these examples.) You're free to add other variables and functions to your module as well, including ones you don't put in the public interface via
Finally, line 7 is a simple
Packages group and organize global identifiers. They have nothing to do with privacy. Code compiled in package Other Kinds of Library Files
A library is a collection of loosely related functions designed to be used by other programs. It lacks the rigorous semantics of a Perl module. The file extension
Perl libraries - or in fact, any arbitrary file with Perl code in it - can be loaded in using
Libraries work well when used by a program, but problems can arise when libraries use one another. Consequently, simple Perl libraries have been rendered mostly obsolete, replaced by the more modern modules. But some programs still use libraries, usually loading them in with
Other file extensions are occasionally seen in Perl. A So far we've only talked about traditional modules, which export their interface by allowing the caller direct access to particular subroutines and variables. Most modules fall into this category. But some problems - and some programmers - lend themselves to more intricately designed modules, those involving objects. An object-oriented module seldom uses the import-export mechanism at all. Instead, it provides an object-oriented interface full of constructors, destructors, methods, inheritance, and operator overloading. This is the subject of Chapter 13 . Not Reinventing the WheelCPAN, the Comprehensive Perl Archive Network, is a gigantic repository of nearly everything about Perl you could imagine, including source, documentation, alternate ports, and above all, modules. Before you write a new module, check with CPAN to see whether one already exists that does what you need. Even if one doesn't, something close enough might give you ideas. You can access CPAN at http://www.perl.com/CPAN/CPAN.html (or ftp://www.perl.com/pub/perl/CPAN/CPAN.html ). This file briefly describes each of CPAN's modules, but because it's manually edited, it may not always have the very latest modules' descriptions. You can find out about those in the CPAN/RECENT or CPAN/RECENT.html file. The module directory itself is in CPAN/modules . It contains indices of all registered modules plus three convenient subdirectories: by-module , by-author , and by-category . All modules are available through each of these, but the by-category directory is probably the most useful. There you will find directories covering specific applications areas including operating system interfaces; networking, modems, and interprocess communication; database interfaces; user interfaces; interfaces to other programming languages; authentication, security, and encryption; World Wide Web, HTML, HTTP, CGI, and MIME; images, pixmap and bitmap manipulation, drawing, and graphing - just to name a few. See AlsoThe sections on "Packages" and on "Modules" in Chapter 5 of Programming Perl and in perlmod (1) ![]() Copyright © 2001 O'Reilly & Associates. All rights reserved. |
|
|