Section 11.8. The SUPER Way of Doing Things


11.8. The SUPER Way of Doing Things

By changing the Animal class to the SUPER class in that invocation, we get a search of all our superclasses (classes listed in @ISA) automatically:

 { package Animal;   sub speak {     my $class = shift;     print "a $class goes ", $class->sound, "!\n";   } } { package Mouse;   @ISA = qw(Animal);   sub sound { 'squeak' }   sub speak {     my $class = shift;     $class->SUPER::speak;     print "[but you can barely hear it!]\n";   } } 

Thus, SUPER::speak means to look in the current package's @ISA for speak, invoking the first one found if there's more than one. In this case, we look in the one and only base class, Animal, find Animal::speak, and pass it Mouse as its only parameter.




Intermediate Perl
Intermediate Perl
ISBN: 0596102062
EAN: 2147483647
Year: N/A
Pages: 238

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net