Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

[Resolved]Perl inheritance

by ivanovic3001 (Initiate)
on Apr 18, 2014 at 18:27 UTC ( #1082802=perlquestion: print w/replies, xml ) Need Help??
ivanovic3001 has asked for the wisdom of the Perl Monks concerning the following question:

Dear anyone,

I need some serious help. Been trying to figure out inheritance. Was given an assignment to practice inheritance. the code given was.

use v5.10.0; use warnings; use strict; package Animal; sub new { my $class = $_[0]; my $self = {text => "I am an abstract animal.\n"}; return bless($self, $class); } sub speak { my $self = shift; print $self->{"text"} } package Duck; our @ISA = qw(Animal); # qw(Animal) je ekvavilentno ("Animal") sub new { return bless({text => "I am a a duck.\n"}, shift); }

So this is the given code and I'm just supposed to make another package named Cow, Horse, etc. However reading online this is different from the stuff I had and the code doesn't print out anything when running and testing it. From what I gathered it's supposed to print "I am a duck" but it does nothing at all.

I apologize in advance if this is a newbie question but i really can't figure it out tried asking my peers but no one new anything. Thanks in advance for any help.

Replies are listed 'Best First'.
Re: Perl inheritance
by 2teez (Priest) on Apr 18, 2014 at 18:45 UTC

    From what I gathered it's supposed to print "I am a duck" but it does nothing at all.

    And if you write this:

    my $an = Duck->new; $an->speak;
    with the code you presented, does it print out what you want?

    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me

      Wow i feel so ridiculous now. Yeah it's printing it out correctly. thanks alot this makes so much more sense now thanks to everyone for explaining it.

        Think nothing of it.   We all feel ridiculous ... in fact, we all do so, very frequently!   :-D   The digital computer is like that.   Guess that’s why we’re all Meditating here.

        Yeah, as usual, Monty Python illustrated it best ...

Re: Perl inheritance
by vinoth.ree (Monsignor) on Apr 18, 2014 at 18:44 UTC

    Hi,

    I am not sure I understand your problem correctly. whatever I get from your query is that, you are trying to implement the inheritance feature.

    In the above code you have given, Duck Package inherits the Animal package. In Duck package you are creating object for Duck, you need to call any function in Duck, for ex: speak() function, which is defined in Animal package, It will be get called, and your string in Duck package will be printed.

    So Create object for Duck and call speak function like  Duck->new()->speak()


    All is well
Re: Perl inheritance
by bigj (Monk) on Apr 18, 2014 at 18:43 UTC
    My first guess would be, that you give bless the parameters in wrong order: So in package Duck,
    sub new { # shift means shift @_ and will have $_[0] inside # what means $self like in package Animal return bless(shift, {text => "I am a a duck.\n"}); # and the class name needs to be first parameter }

    So, you accidently blessed the class 'I am a duck.\n' instead of the class Duck :-) But as 'I am a duck.\n'-class also @ISA Animal, it still could speak and speaks there $self->{text} that had never been defined anywhere, so it prints nothing at all absolutely correctly :D

    Greetings,
    Janek Schleicher

      The order of arguments to bless in the OP is correct.

      BTW: My favorite "off-label" use of the  => (fat comma) operator (see perlop) is in a statement like
          return bless $object_ref => $class;
      which can be read "return the result of blessing object reference into class".

Re: Perl inheritance
by Anonymous Monk on Apr 18, 2014 at 18:44 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1082802]
Approved by vinoth.ree
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (7)
As of 2017-12-11 14:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What programming language do you hate the most?




















    Results (297 votes). Check out past polls.

    Notices?