Dave Tang <[email protected]> asked:
> I've been going through perldoc perlboot and I have a question about
> using the SUPER class. Here's the code in the documentation:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> {
> package Animal;
> sub speak {
> my $class = shift;
> print "a $class goes ", $class->sound, "!\n";
> }
> }
>
> {
> package Mouse;
> @Mouse::ISA = qw (Animal);
> sub sound { "squeak" };
> sub speak {
> my $class = shift;
> $class->SUPER::speak;
> print "but you can barely hear it!\n";
> }
> }
>
> Mouse->speak; #outputs a Mouse goes squeak!
> # but you can barely hear it!
>
> It reads: "So, SUPER::speak means look in the current package's @ISA for
> speak, invoking the first one found. Note that it does not look in the
> @ISA of $class."
>
> We are calling the method speak using the Mouse class, so $class = Mouse.
> So when the documentation says SUPER::speak looks in the current
> package's @ISA for speak, isn't the current package Mouse (which is also
> $class)? I just don't understand what the note means when it says it does
> not look in the @ISA of $class. Could someone explain that for me?
I agree, the phrasing might be a bit confusing.
Let's assume that you have created a derived class from Mouse, like
{
package Dormouse;
@Dormouse::ISA = qw (Mouse);
# note no speak() here
}
which doesn't override speak.
Now, if you call
Dormouse->speak()
Mouse::speak is invoked, since there is no speak() method in the Dormouse
package.
Now $class is 'Dormouse', but the current package is 'Mouse' and the
explanation from perlboot makes sense: $class->SUPER::speak() invokes
Animal::speak() and not Mouse::speak().
HTH,
Thomas
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/