Flylib.com

Books Software

 
 
 

A.19 Modules and Packages


A.19 Modules and Packages

Chapter 1 summarizes the basic concepts of namespaces, packages, and modules.

A namespace is a table containing the names and values of variables and subroutines. Namespaces are excellent ways to protect one part of your code from unintentionally using the same variable or subroutine name as appears in another part of your code, causing a namespace collision and often leading to incorrect (but hard to identify) program behavior.

By default, a Perl program uses the namespace called main .

The package declaration enables you to declare and use different namespaces for different parts of your program. For instance, to declare a new namespace called Outer , use the following statement:

package Outer;

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:

use Outer;

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:

  • A class is a Perl module with a package declaration. The name of the class is the same as the name of the package; the module file also has the same name but with " .pm " (for Perl module) appended.

  • An object is a reference to a collection of data used by a Perl class, usually but not necessarily , implemented as a hash. An object is marked with the name of its class using bless .

  • A method is a subroutine in the class.

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 methods .

You use a class in your code by saying, for example:

use Goodclass;

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 stuff in class Goodclass on a Goodclass object $goodobject , with argument type initialized to the value ' good ', and save its output in the array @goodstuff , like so:

@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:

($goodobject, type='good')

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 operate on individual class objects, you can also define class methods that have access to information about multiple class objects ”for example, a count of how many objects have been created.

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.