{ package Cow;
sub sound { "moooo" }
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "!\n";
}
}
Now, when you call Cow->speak, you get a
$class of Cow in
speak. This, in turn, selects the
Cow->sound method, which returns
moooo. How different would this be for the
Horse?
{ package Horse;
sub sound { "neigh" }
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "!\n";
}
}
Only the name of the package and the specific sound change. So can
you share the definition for speak between the cow
and the horse? Yes, with inheritance!
Now let's define a common subroutine package called
Animal with the definition for
speak:
{ package Animal;
sub speak {
my $class = shift;
print "a $class goes ", $class->sound, "!\n";
}
}
Then, for each animal, you can say it inherits from
Animal, along with the animal-specific sound:
{ package Cow;
@ISA = qw(Animal);
sub sound { "moooo" }
}
Note the added @ISA array. We'll
get to that in a minute.
What happens when you invoke Cow->speak now?
First, Perl constructs the argument list. In this case,
it's just Cow. Then Perl looks
for Cow::speak. That's not there,
so Perl checks for the inheritance array
@Cow::ISA. It's there, and
contains the single name Animal.
Perl next checks for speak inside
Animal instead, as in
Animal::speak. That found, Perl invokes that
subroutine with the already frozen argument list, as if you had said:
Animal::speak("Cow");