I am going through book "Intermediate Perl" and started with Object Oriented section. I have created a distribution Animal and inside it:

bin/pasture

use Animal; use Cow; use Horse; use Sheep; Cow->speak; Horse->speak; Sheep->speak;

lib/Animal.pm

package Animal; use 5.006; use strict; use warnings; sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n"; } sub sound { die "'You have to define sound() in a subclass' found"; }

lib/Cow.pm

package Cow; use 5.006; use strict; use warnings; use Animal; our @ISA = qw(Animal); sub sound { "moooo" }

lib/Sheep.pm

package Sheep; use 5.006; use strict; use warnings; use Animal; our @ISA = qw(Animal); sub sound { "baaaah" }

lib/Horse.pm

package Horse; use 5.006; use strict; use warnings; use Animal; our @ISA = qw(Animal); sub sound { "neigh" }

When I run ~/localperl/bin/perl -Ilib bin/pasture I get following output:

a Cow goes moooo! a Horse goes neigh! a Sheep goes baaaah!

As per book, explanation of this behavior is:

What happens when we invoke Cow−>speak now?

First, Perl constructs the argument list. Here, it’s just Cow. Then Perl looks for Cow::speak. That’s not there, so Perl checks for the inheritance array @Cow::ISA. It finds @Cow::ISA contains the single name Animal.

Perl next looks for speak inside Animal instead, as in Animal::speak. That found, Perl invokes that method with the already frozen argument list, as if we had said:

Animal::speak('Cow'); #This is the part I am having trouble understanding. How did "Cow" become an argument when I didn't specify it anywhere?

As I mentioned in comment, I am unable to understand: How did "Cow" become an argument when I didn't specify it anywhere?

Can someone please help me understand this behavior? (Forgive me for the long post.)


In reply to [SOLVED]: Trying to understand method calling in OOP by Perl300

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.