http://www.perlmonks.org?node_id=190659

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

Can anyone tell me why the method getName() returns "Shape=HASH(0x81073ec)->getName()"? Thanks Monks!



# Shape.pm package Shape; sub new { my $class = shift; my $self = { COLOR => "clear", LNGTH => "0", WIDTH => "0", NAME => "blank", @_, }; return bless $self, $class; } sub getName { my $self = shift; return $self->{NAME}; } 1; # in lego.pl use Shape; $circle = Shape->new( COLOR => "red", LNGTH => "2", WIDTH => "2", NAME => "barney" ); print "The color of \$circle is: $circle->getName()\n"; exit(0);

Replies are listed 'Best First'.
Re: learning oo
by cephas (Pilgrim) on Aug 16, 2002 at 14:58 UTC
    Your object reference is getting interpolated in the double quoted string. Try:

    print 'The color of $circle is: ', $circle->getName(),"\n";


    cephas
Re: learning oo
by Zaxo (Archbishop) on Aug 16, 2002 at 15:13 UTC

    Calls to subs are not interpolated. cephas' solution is correct, or else either

    print "The color of \$circle is: @{[$circle->getName()]}\n";
    or
    print "The color of \$circle is: ${\$circle->getName()}\n";
    depending on whether you want list or scalar context.

    After Compline,
    Zaxo

some OO (ahem) references, ;-)
by BorgCopyeditor (Friar) on Aug 16, 2002 at 15:18 UTC

    If the usual docs aren't enough for you to see your problem (perlref, perltoot, perlobj, perlbot), you might read the slower, more detailed discussion that's offered as a sample chapter from Object Oriented Perl.

    Update: Oops! Forgot merlyn's perlboot. You know that feeling you get when you spot a movie star at the grocery store? I'm having that feeling right now.

    BCE
    --Your punctuation skills are insufficient!