On Feb 7, Jan Eden said:

>I just work my way through "Learning Perl Objects, References & Modules".
>Now at one point, I am stuck: Randal introduces classes and methods in
>Chapter 8.

Hopefully I'll answer before Randal. ;)

>{   package Mouse
>    @ISA = qw{Animal};
>    ...
>    sub speak {
>    my $class = shift;
>     ...
>     Animal::speak($class);
>     ...
>     }
>}

I was about to ask why it's written this way, but upon checking the
source, I see that this is the way the first example on overriding a
method looks.

>Since there is a method speak in Mouse, it would override the parent's
>classes method Animal::speak if the latter were not called explicitly.
>
>But, as Randal points out, this forces Perl to look for speak in Animal
>and nowhere else - without the method invocation arrow, it cannot check
>@ISA for ancestor classes.

Right.  I was about to say "why is it written this way?" but then I saw
the rest of the examples (and read the rest of your email).

>So far, I get the point. But then he introduces the following solution:
>
>$class->Animal::speak(@_);
>
>Apart from the fact that @_ should be unnecessary here (or did I get
>something wrong), this should expand to:
>
>Mouse::Animal::speak("Mouse");

No, it does not.  If $class is 'Mouse', then

  $class->method(@_);

will try looking for Mouse::method(), and if not, it will look through
Mouse's @ISA for a class that DOES supply method().  BUT HERE, we're using

  $class->OtherClass::method(@_);

which says explicitly to start looking for method() in OtherClass (and if
it fails there, look in OtherClass's @ISA).  So

  $class->Animal::speak();

in your case becomes

  Animal::speak($class);

except that it becomes that DYNAMICALLY.

>And when Perl does not find Animal::speak in Mouse, to:

Ah, here's the confusion.  This isn't looking for a method named
'Animal::speak' in 'Mouse'; it's looking for a method named 'speak' in
'Animal'.

Only the right-most part of a Thing::Like::this denotes the name of a
method.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to