8.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 the subroutine can't be found, inheritance kicks in, but you'll learn about that later.) This means that you get the class name as the first parameter or the only parameter, if no arguments are given. You can rewrite the Sheep speaking subroutine 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 subroutine. But once again, you have a lot of similar structure. Can you factor out that commonality even further? Yes ”by calling another method in the same class.



Learning Perl Objects, References & Modules
Learning Perl Objects, References, and Modules
ISBN: 0596004788
EAN: 2147483647
Year: 2003
Pages: 199

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