Документация
HTML CSS PHP PERL другое

Section 11.3.  The Extra Parameter of Method Invocation

 
Previous
Table of Contents
Next

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.


Previous
Table of Contents
Next
© 2000- NIV