ReLearning OOP

Discussion in 'CIW Certifications' started by stuPeas, Dec 17, 2006.

  1. stuPeas

    stuPeas Megabyte Poster

    774
    12
    76
    Im trying to learn Perl OO, and my course manual isnt up to it.
    Ive got a couple of realy newbie questions, hope someone can help.

    Ive been using Perlboot from cpan, and a couple of things in it have me confused.

    This is the code for this Qustion (Exactly as it appears in Perlboot).
    Code:
    { package Animal;
       sub speak {
          my $class = shift;
          print "a $class goes ",  $class->sound, "!\n"
         }
    }
    { package Horse;
       @ISA = qw(Animal);
       sub sound { "neigh"}
    }
    
    Talking about this code, the next sentance is
    "This lets us invoke Horse->speak to ripple upward to Animal::speak, calling back to Horse::sound to get the specific sound"

    Do you think that the writer is calling Horse->speak from within the Horse package or from Main::, and if from Main::, do you have to add "use Horse;" before you make the call.

    That was this simple question.
    Next...

    Earlier in Perlboot the author talks about "duplicating code".
    This is the code used for this bit.
    Code:
    #Animal package from before
    { package Mouse;
       @ISA = qw(Animal);
       sub sound { "squeak" }
       sub speak {
         my $class = shift;
         Animal::speak ($class);
         print "[but you can barely hear it !]\n";
         }
    }
    
    
    The text that comes with it says:
    "Note that we have to include the $class parameter (almost surely that of "Mouse") as the first parameter to Animal::speak, since we've stopped using the method arrow. Why did we stop? well, if we invoke Animal->speak there, the first parameter to the method will be "Animal" not "Mouse", and when the time comes for it to call for the sound, it won't have the right class to come back to this package."

    My question is: Why would'nt you just call Mouse->speak instead of Animal::speak($class) or Animal->speak???

    It then goes on to say:
    "Invoking Animal::speak directly is a mess, however. What if Animal::speak didnt exist before, and was being inherited from a class mentioned in @Animal::ISA? Because we are no longer using the method arrow, we get one and only one chance to hit the right subroutine."

    to cut a long story short (whoops, to late), the aurthor says the fix for this is
    $class->Animal::speak;

    My question is: I dont know where to start. What the hell is he talking about.


    Im presuming its ok to quote from stuff on CPAN.

    Thanks in advance
     
    Certifications: C&G Electronic, CIW Associate (v5).
    WIP: CIW (Website Design Manager)
  2. hbroomhall

    hbroomhall Petabyte Poster Gold Member

    6,624
    117
    224
    I supose as I've been banging on about Perl recently I had better chip in my 2d-worth.

    I don't regard OOP in Perl as anything other than a nasty hack - if I wanted OOP I'd write in C++ or the like. And I have no pretensions that I'm competent in it.

    Have a look here to see if it helps.

    Harry.
     
    Certifications: ECDL A+ Network+ i-Net+
    WIP: Server+

Share This Page

Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.