A.19 Modules and Packages
Chapter 1 summarizes the basic concepts of namespaces, packages, and modules.
A namespace is a table containing the
By default, a Perl program uses the namespace called main .
declaration enables you to declare and use different namespaces for different
Package declarations usually occur at or near the top of a file and are in effect throughout the file, but they can appear several times within a file, causing the active namespace to switch each time they are called.
When a file has one package declaration at the top of the file and it's named with the package name followed by the .pm suffix (e.g, Outer.pm ), the file is called a Perl module. (The module also needs to end with the statement " 1; " to load correctly when called.)
The code for a Perl module can be used in a Perl program by referencing the file defining the module with a use statement, as in the following example:
The Perl interpreter will then try to find a file called Outer.pm .
Chapter 1 gives the basic details on how to manage modules so that the Perl interpreter can find them.
A.20 Object-Oriented Programming
Object-oriented Perl programming is introduced in Chapter 3 and Chapter 4, and used in all the remaining chapters of the book.
There are three main concepts:
Arrow notation -> is used in a special way in object-oriented Perl code and has an effect on what arguments are passed to the class
You use a class in your code by saying, for example:
You create an object by calling a constructor method in the class, which is usually (but not necessarily) called new , for example:
$goodobject = Goodclass->new( parameter1 => 1, parameter2 => 2);
Note that arguments are usually (but not necessarily) specified using the hash notation key => value , and therefore can be given in any order.
You call other methods in the class by invoking them from a class object. For example you call the method
, with argument type
@goodstuff = $goodobject->stuff(type => 'good');
The arrow notation causes the called method to insert an additional argument. (In the examples just shown, the methods are the subroutines new and stuff ).
The additional argument is the class information that appears to the left of the arrow. So, in these examples, the method new has the argument list:
('Goodclass', parameter1=1, parameter2=2)
The method stuff has the argument list:
The details of how arguments are passed to methods are important to know only if you are writing a class, not if you are using one. If you simply use a class, you only have to know how to call the class methods using the arrow notation as just shown.
Inheritance is a technique with which you can write a new class that uses definitions from an already existing class.
AUTOLOAD is a subroutine that handles calls to undefined methods, and DESTROY is a subroutine that handles cleanup when a class object goes out of scope.
In addition to object methods that
A great deal of the utility of existing Perl code is available only in class modules. In this book I use some of the important and well-known modules including DBI , CGI , GD and GD::Graph , and the Bioperl collection of modules. Almost all modules are available at http://www.CPAN.org.