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