Section 11.3. The Extra Parameter of Method Invocation


11.3. The Extra Parameter of Method Invocation

The invocation of:

 Class->method(@args) 

attempts to invoke the subroutine Class::method as:

 Class::method('Class', @args); 

(If it can't find the method, inheritance kicks in, but we'll show that later.) This means that we get the class name as the first parameter, or the only parameter, if no arguments are given. We can rewrite the Sheep speaking method as:

 sub Sheep::speak {   my $class = shift;   print "a $class goes baaaah!\n"; } 

The other two animals come out similarly:

 sub Cow::speak {   my $class = shift;   print "a $class goes moooo!\n"; } sub Horse::speak {   my $class = shift;   print "a $class goes neigh!\n"; } 

In each case, $class gets the value appropriate for that method. But once again, we have a lot of similar structure. Can we factor out that commonality even further? Yesby calling another method in the same class.




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