DekGenius.com
[ Team LiB ] Previous Section Next Section

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.

    [ Team LiB ] Previous Section Next Section