Chapter 12. Packages, Libraries, and Modules
Introduction
Like all those possessing a library, Aurelian was aware that he was guilty of not knowing his in its entirety.
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
init function or a global
$count variable. When merged into one program,
these separate parts would interfere with each other.
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
$CGI::needs_binmode is a global variable named
$needs_binmode, which resides in package
CGI.
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
$CGI'needs_bin_mode).
$Names::startup ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access