How09 has asked for the wisdom of the Perl Monks concerning the following question:

Hi, just starting in perl. In this perlboot example I have a question:
{ package Animal; sub sound { "WOOOAAARRRR" } sub speak { my $class = shift; print "a $class goes ", $class->sound, "!\n"; } } { package Horse; @Horse::ISA = qw(Animal); sub sound { "neigh" } sub p1 { print "p1: ". shift() ."\n"; } } Horse->speak(); # ok, works as expected Horse::p1("Horse"); # ok, prints "Horse" Horse::speak("Horse"); # Message: # "Undefined subroutine &Horse::speak called.. +." # So, the inheritance mechanism does not work +for this?

Replies are listed 'Best First'.
Re: Inheritance via class::derived_method Question
by ikegami (Patriarch) on Nov 18, 2010 at 18:33 UTC
    Inheritance only applies to classes and objects. If you're not going to use either, how could inheritance possibly "work".
    Horse->speak(); # Class method $ed->speak(); # Object method
Re: Inheritance via class::derived_method Question
by AnomalousMonk (Archbishop) on Nov 18, 2010 at 22:03 UTC
    So, the inheritance mechanism does not work for this?

    The 'inheritance mechanism' is the  -> operator, which is used in
        Horse->speak();
    but entirely absent (and so cannot work) in
        Horse::speak("Horse");
    which is an invocation of the  Horse package's  speak subroutine, which is undefined in the OPed example.

      The 'inheritance mechanism' is the -> operator,

      No. The inheritance mechanism applies to method calls. Few of the -> operators have anything to do with methods. For example,

      $x->[0]

      Furthermore, not all method calls use the -> operator. For example,

      new Foo

      Forms of method calls:

      $class->method(LIST) # Static method call. $object->method(LIST) # Instance method call. $x->Class::method(LIST) # Pretend the LHS is class "Class". $x->SUPER::method(LIST) # Pretend the LHS is its super class. method $x LIST; # Indirect method syntax

      I think that's all of them.

      Horse::speak("Horse") is a sub call. Subroutines aren't inherited. Subroutines don't have classes, so subroutines don't have parent classes, so subroutines have nothing from which they could inherit.

        Thanks for the help. I think I'm getting a feel for it now.

        class::method();

        Is more like a name which is either defined or not.
        There is no @class::ISA array search for "method".
        There is no "automatic" first argument=class passed to the method.

        class->method();

        dereferences class for "method" and if not found will then use the "special method lookup mechanism" to search the classes listed in the @ISA array for "method"
        The class name is automatically passed to "method" as the 1st argument.

        -----
        Fairly close?
        Thanks again.