Programming Perl

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

7. The Standard Perl Library

Contents:
Beyond the Standard Library
Library Modules

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
ModuleFunction
BenchmarkCheck and compare running times of code
ConfigAccess Perl configuration information
EnvImport environment variables
EnglishUse English or awk names for punctuation variables
Getopt::LongExtended processing of command-line options
Getopt::StdProcess single-character switches with switch clustering
libManipulate @INC at compile time
ShellRun shell commands transparently within Perl
strictRestrict unsafe constructs
SymbolGenerate anonymous globs; qualify variable names
subsPredeclare subroutine names
varsPredeclare global variable names

Table 7.2: General Programming: Error Handling and Logging
ModuleFunction
CarpGenerate error messages
diagnosticsForce verbose warning diagnostics
sigtrapEnable stack backtrace on unexpected signals
Sys::SyslogPerl interface to UNIX syslog(3) calls

Table 7.3: General Programming: File Access and Handling
ModuleFunction
CwdGet pathname of current working directory
DirHandleSupply object methods for directory handles
File::BasenameParse file specifications
File::CheckTreeRun many tests on a collection of files
File::CopyCopy files or filehandles
File::FindTraverse a file tree
File::PathCreate or remove a series of directories
FileCacheKeep more files open than the system permits
FileHandleSupply object methods for filehandles
SelectSaverSave and restore selected filehandle

Table 7.4: General Programming: Text Processing and Screen Interfaces
ModuleFunction
Pod::TextConvert POD data to formatted ASCII text
Search::DictSearch for key in dictionary file
Term::CapTerminal capabilities interface
Term::CompleteWord completion module
Text::AbbrevCreate an abbreviation table from a list
Text::ParseWordsParse text into a list of tokens
Text::SoundexThe Soundex Algorithm described by Knuth
Text::TabsExpand and unexpand tabs
Text::WrapWrap text into a paragraph

Table 7.5: Database Interfaces
ModuleFunction
AnyDBM_FileProvide framework for multiple DBMs
DB_FileTied access to Berkeley DB
GDBM_FileTied access to GDBM library
NDBM_FileTied access to NDBM files
ODBM_FileTied access to ODBM files
SDBM_FileTied access to SDBM files

Table 7.6: Mathematics
ModuleFunction
integerDo arithmetic in integer instead of double
Math::BigFloatArbitrary-length floating-point math package
Math::BigIntArbitrary-length integer math package
Math::ComplexComplex numbers package

Table 7.7: Networking and Interprocess Communication
ModuleFunction
IPC::Open2Open a process for both reading and writing
IPC::Open3Open a process for reading, writing, and error handling
Net::PingCheck whether a host is online
SocketLoad the C socket.h defines and structure manipulators
Sys::HostnameTry every conceivable way to get hostname

Table 7.8: Time and Locale
ModuleFunction
Time::LocalEfficiently compute time from local and GMT time
I18N::CollateCompare 8-bit scalar data according to the current locale

Table 7.9: For Developers: Autoloading and Dynamic Loading
ModuleFunction
AutoLoaderLoad functions only on demand
AutoSplitSplit a module for autoloading
Devel::SelfStubberGenerate stubs for a SelfLoading module
DynaLoaderAutomatic dynamic loading of Perl modules
SelfLoaderLoad functions only on demand

Table 7.10: For Developers: Language Extensions and Platform Development Support
ModuleFunction
ExtUtils::InstallInstall files from here to there
ExtUtils::LiblistDetermine libraries to use and how to use them
ExtUtils::MakeMakerCreate a Makefile for a Perl extension
ExtUtils::ManifestUtilities to write and check a MANIFEST file
ExtUtils::MiniperlWrite the C code for perlmain.c
ExtUtils::MkbootstrapMake a bootstrap file for use by DynaLoader
ExtUtils::MksymlistsWrite linker option files for dynamic extension
ExtUtils::MM_OS2Methods to override UNIX behavior in ExtUtils::MakeMaker
ExtUtils::MM_UnixMethods used by ExtUtils::MakeMaker
ExtUtils::MM_VMSMethods to override UNIX behavior in ExtUtils::MakeMaker
FcntlLoad the C fcntl.h defines
POSIXInterface to IEEE Std 1003.1
SafeCreate safe namespaces for evaluating Perl code
Test::HarnessRun Perl standard test scripts with statistics

Table 7.11: For Developers: Object-Oriented Programming Support
ModuleFunction
ExporterDefault import method for modules
overloadOverload Perl's mathematical operations
Tie::HashBase class definitions for tied hashes
Tie::ScalarBase class definitions for tied scalars
Tie::StdHashBase class definitions for tied hashes
Tie::StdScalarBase class definitions for tied scalars
Tie::SubstrHashFixed-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:

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.


Previous: 6.4 Cooperating with Other LanguagesProgramming PerlNext: 7.2 Library Modules
6.4 Cooperating with Other LanguagesBook Index7.2 Library Modules