Listing 2.10. Augmenting base class methods.
package MyCust; # Requires Customer.pm in @INC
use Customer;
@ISA = qw(Customer);
sub search{
my $self = shift;
my $key = shift;
defined($self->{$key}) ? return $self->{$key} : undef;
}
sub dumpcust{
my $self = shift;
# Print out _all_ the values for the object
Customer::dumpcust($self);
foreach $key (keys %{$self}){
next if($key =~ /Vitals|Name/);
print $self->{$key},"\n";
}
}
Notice how in Listing 2.10 you first call the parent's method, Customer::dumpcust(), passing in the object, $self. Also notice that you invoke the parent's method using the Class::method() syntax. You couldn't use the $self->method syntax and rely on the object getting passed automatically, because $self is blessed into the MyCust class. You would have put yourself into an infinite loop! Always stay mindful of what class an object belongs to when using inheritance.
Note:
We could have also used the UNIVERSAL class, or the SUPER keyword to implement method-augmentation, but we're not going to discuss those techniques here. They became available after the original authorship of this chapter, and may best be discussed in a more complete coverage of Perl5 OO techniques.
Now, the output from running Listing 2.9, and invoking the dumpcust() method therein, is ordered first according to the preference of the parent class, Customer, followed by the variables you've added to the customer object:
Customer's phone number is 1-203-456-7890 Billy T. Kid Age : 10 Sex : M 1-203-456-7890 12345
That wraps up the discussion of object-oriented Perl programming. You've now seen the techniques used to implement a clean and fine-tuned level of functionality in your programs that use Perl5 and modules. I haven't covered all of the other object-oriented techniques and tricks, by any means, so I recommend that you investigate PERLBOT and PERLOBJ for more examples and in-depth explanations.