[Previous] [Contents] [Next]


Listing 2.6. Subclassing the Customer module.


package MyCust;

 require Customer; # Customer.pm must be in @INC

 @ISA = qw(Customer);



 sub search{

 my $self = shift;

 my $key = shift;

 defined($self->{$key}) ? return $self->{$key} : undef;

 }

 1;


Note how you use the @ISA array to imply that this class is a subclass of the Customer class. The @ISA array tells Perl that this package is an instance of the elements it contains, which usually correspond to other packages. In this case, MyCust is an instance of Customer. The only code you have to write is the search() method, and when the next release of Customer comes out, you are (theoretically) unaffected by any internal changes that have been made by the author.

Note:

Ordinarily, changes to the base class should not affect a subclass, unless the author changes the names of the methods or the structure of the object. Such changes are generally not a wise practice, once the class is considered stable and "out of alpha."


Now you can code your program to use the MyCust class instead of the base Customer class and have the additional functionality of the search() method, as shown in Listing 2.7 (this listing requires both Listing 2.4 and Listing 2.6):

[Previous] [Contents] [Next]