Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl inheritance

by bigj (Monk)
on Apr 18, 2014 at 18:43 UTC ( [id://1082803]=note: print w/replies, xml ) Need Help??


in reply to [Resolved]Perl inheritance

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

Replies are listed 'Best First'.
Re^2: Perl inheritance
by AnomalousMonk (Archbishop) on Apr 18, 2014 at 22:38 UTC

    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".

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1082803]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-25 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found