home | O'Reilly's CD bookshelfs | FreeBSD | Linux | Cisco | Cisco Exam  


Programming Perl

Programming PerlSearch this book
Previous: 6.4 Cooperating with Other Languages Chapter 7 Next: 7.2 Library Modules
 

7. The Standard Perl Library

This chapter describes the collection of Perl code that comes along with the Perl distribution. If you use this library and then share your program with others, they will not have to take special steps to execute the program, because the same library is available to Perl programs everywhere.

You'll save some time if you make the effort to get familiar with the standard library. There's no point in reinventing the wheel. You should be aware, however, that the library contains a wide range of material. While some modules may be extremely helpful, others may be completely irrelevant to your needs. For example, some are useful only if you are creating extensions to Perl. We offer below a rough classification of the library modules to aid you in browsing.

First, however, let's untangle some terminology:

package

A package is a simple namespace management device, allowing two different parts of a Perl program to have a (different) variable named $fred . These namespaces are managed with the package declaration, described in Chapter 5, Packages, Modules, and Object Classes .

library

A library is a set of subroutines for a particular purpose. Often the library declares itself a separate package so that related variables and subroutines can be kept together, and so that they won't interfere with other variables in your program. Generally, a library is placed in a separate file, often ending in " .pl ", and then pulled into the main program via require . (This mechanism has largely been superseded by the module mechanism, so nowadays we often use the term "library" to talk about the whole system of modules that come with Perl. See the title of this chapter, for instance.)

module

A module is a library that conforms to specific conventions, allowing the file to be brought in with a use directive at compile time. Module filenames end in " .pm ", because the use directive insists on that. (It also translates the subpackage delimiter :: to whatever your subdirectory delimiter is; it is / on UNIX.) Chapter 5 describes Perl modules in greater detail.

pragma

A pragma is a module that affects the compilation phase of your program as well as the execution phase. Think of them as hints to the compiler. Unlike modules, pragmas often (but not always) limit the scope of their effects to the innermost enclosing block of your program. The names of pragmas are by convention all lowercase.

For easy reference, this chapter is arranged alphabetically. If you wish to look something up by functional grouping, Tables 7-1 through 7-11 display an (admittedly arbitrary) listing of the modules and pragmas described in this chapter.


Table 7.1: General Programming: Miscellaneous
Module Function
Benchmark Check and compare running times of code
Config Access Perl configuration information
Env Import environment variables
English Use English or awk names for punctuation variables
Getopt::Long Extended processing of command-line options
Getopt::Std Process single-character switches with switch clustering
lib Manipulate @INC at compile time
Shell Run shell commands transparently within Perl
strict Restrict unsafe constructs
Symbol Generate anonymous globs; qualify variable names
subs Predeclare subroutine names
vars Predeclare global variable names

Table 7.2: General Programming: Error Handling and Logging
Module Function
Carp Generate error messages
diagnostics Force verbose warning diagnostics
sigtrap Enable stack backtrace on unexpected signals
Sys::Syslog Perl interface to UNIX syslog (3) calls

Table 7.3: General Programming: File Access and Handling
Module Function
Cwd Get pathname of current working directory
DirHandle Supply object methods for directory handles
File::Basename Parse file specifications
File::CheckTree Run many tests on a collection of files
File::Copy Copy files or filehandles
File::Find Traverse a file tree
File::Path Create or remove a series of directories
FileCache Keep more files open than the system permits
FileHandle Supply object methods for filehandles
SelectSaver Save and restore selected filehandle

Table 7.4: General Programming: Text Processing and Screen Interfaces
Module Function
Pod::Text Convert POD data to formatted ASCII text
Search::Dict Search for key in dictionary file
Term::Cap Terminal capabilities interface
Term::Complete Word completion module
Text::Abbrev Create an abbreviation table from a list
Text::ParseWords Parse text into a list of tokens
Text::Soundex The Soundex Algorithm described by Knuth
Text::Tabs Expand and unexpand tabs
Text::Wrap Wrap text into a paragraph

Table 7.5: Database Interfaces
Module Function
AnyDBM_File Provide framework for multiple DBMs
DB_File Tied access to Berkeley DB
GDBM_File Tied access to GDBM library
NDBM_File Tied access to NDBM files
ODBM_File Tied access to ODBM files
SDBM_File Tied access to SDBM files

Table 7.6: Mathematics
Module Function
integer Do arithmetic in integer instead of double
Math::BigFloat Arbitrary-length floating-point math package
Math::BigInt Arbitrary-length integer math package
Math::Complex Complex numbers package

Table 7.7: Networking and Interprocess Communication
Module Function
IPC::Open2 Open a process for both reading and writing
IPC::Open3 Open a process for reading, writing, and error handling
Net::Ping Check whether a host is online
Socket Load the C socket.h defines and structure manipulators
Sys::Hostname Try every conceivable way to get hostname

Table 7.8: Time and Locale
Module Function
Time::Local Efficiently compute time from local and GMT time
I18N::Collate Compare 8-bit scalar data according to the current locale

Table 7.9: For Developers: Autoloading and Dynamic Loading
Module Function
AutoLoader Load functions only on demand
AutoSplit Split a module for autoloading
Devel::SelfStubber Generate stubs for a SelfLoading module
DynaLoader Automatic dynamic loading of Perl modules
SelfLoader Load functions only on demand

Table 7.10: For Developers: Language Extensions and Platform Development Support
Module Function
ExtUtils::Install Install files from here to there
ExtUtils::Liblist Determine libraries to use and how to use them
ExtUtils::MakeMaker Create a Makefile for a Perl extension
ExtUtils::Manifest Utilities to write and check a MANIFEST file
ExtUtils::Miniperl Write the C code for perlmain.c
ExtUtils::Mkbootstrap Make a bootstrap file for use by DynaLoader
ExtUtils::Mksymlists Write linker option files for dynamic extension
ExtUtils::MM_OS2 Methods to override UNIX behavior in ExtUtils::MakeMaker
ExtUtils::MM_Unix Methods used by ExtUtils::MakeMaker
ExtUtils::MM_VMS Methods to override UNIX behavior in ExtUtils::MakeMaker
Fcntl Load the C fcntl.h defines
POSIX Interface to IEEE Std 1003.1
Safe Create safe namespaces for evaluating Perl code
Test::Harness Run Perl standard test scripts with statistics

Table 7.11: For Developers: Object-Oriented Programming Support
Module Function
Exporter Default import method for modules
overload Overload Perl's mathematical operations
Tie::Hash Base class definitions for tied hashes
Tie::Scalar Base class definitions for tied scalars
Tie::StdHash Base class definitions for tied hashes
Tie::StdScalar Base class definitions for tied scalars
Tie::SubstrHash Fixed-table-size, fixed-key-length hashing

7.1 Beyond the Standard Library

If you don't find an entry in the standard library that fits your needs, it's still quite possible that someone has written code that will be useful to you. There are many superb library modules that are not included in the standard distribution, for various practical, political, and pathetic reasons. To find out what is available, you can look at the Comprehensive Perl Archive Network (CPAN). See the discussion of CPAN in the Preface.

Here are the major categories of modules available from CPAN:

  • Archiving and Compression

  • Authentication, Security and Encryption

  • Control Flow Utilities (callbacks, exceptions, and so on)

  • Data Types and Data Type Utilities

  • Database Interfaces

  • Development Support

  • Filehandle and Input/Output Stream Utilities

  • File Names, File Systems and File Locking

  • Images, Pixmap and Bitmap Manipulation, Drawing and Graphing

  • Interfaces to/Emulations of Other Programming Languages

  • Internationalization and Locale

  • Language Extensions and Documentation Tools

  • Mail and Usenet News

  • Miscellaneous Modules

  • Networking, Device Control (modems) and Inter-process Communication

  • Operating System Interfaces

  • Option, Argument, Parameter and Configuration File Processing

  • Server and Daemon Utilities

  • String Processing, Language Text Processing, Parsing and Searching

  • User Interfaces

  • World Wide Web, HTML, HTTP, CGI, MIME

Allow us again to reiterate once more that these things are in a state of flux, and you will certainly find more and better stuff on CPAN than we can possibly describe here. The Perl of Great Price has outgrown its oyster, so to speak, because Perl is truly a community effort these days - see John 14:12.