Listing 2.9. Using the overridden method in MyCust.
use MyCust; # Requires the code from Customer.pm to be in @INC
# Create a new customer object
$cust = MyCust->new( `Name' => "Billy T. Kid",
`Vitals' => ["Age : 10", "Sex : M"] );
# add a single statistic
$cust->addstat(`Phone' => `1-203-456-7890');
# add a single statistic
$cust->addstat(`Zip' => `12345');
# search for and return the phone number
print "Customer's phone number is ",$cust->search(`Phone'),"\n";
# dump all values for customer, including the new Zip field
$cust->dumpcust();
# prints:
Customer's phone number is 1-203-456-7890
Billy T. Kid
1-203-456-7890
12345
Age : 10 Sex : M
Now, instead of a completely different behavior in a method, you may merely wish to augment its existing behavior. You accomplish this technique in a like manner, with the exception that you need to write only the code that provides your enhancement, and then invoke the parent method.
When you call your augmenting method, the first thing to do is call the original method from the parent class, passing in the object. Then you perform the additional operations you need.
Here we augment the dumpcust() method in MyCust to examine this technique. (Note again that Listing 2.10 requires Customer.pm, from Listing 2.4, to be in @INC, to work.)