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

Hi Monks,

First off, I am kind of discouraged because I posted this twice before, it showed as posted, and then when I went back to check for replies, my post was not present. Was it deleted?

I don’t understand.

Anyway, I am trying to do Perl OOP. I am following Damien Conway’s book. I wrote some very basic code that I think should work and seems compatible with what Conway does in the beginning of his OOP chapter (3). But, I get this error and I am pretty sure whatever I am doing wrong must be something extremely basic.

I am just wondering what is wrong with the code.

The error is in Line 23, which is in sub new. The error is:

Can’t use string (“Message”) as a HASH while “strict refs” in use at line 23.

The following is Line 23:

return $self->{_name};

And the following is all of the code:

#!/usr/bin/perl -w package Message; use strict; sub new { my ($class) = @_; # bless { my $objref = { _name => $_[1], _subject => $_[2], _message => $_[3], # }, $class; }; bless $objref, $class; return $objref; } sub name { my ($self) = @_; return $self->{_name}; } package main; use strict; my $objType = "Message"; my $name = "John Doe"; my $subject = "money"; my $message = "FRN's should be outlawed."; my $message_obj = Message->new( $name, $subject, $message ); my $name2 = Message->name( $message_obj ); print "\nMy name is $name2\n";
I’m really stuck so if someone can give me a hand, that would be great.

o2bwise

Replies are listed 'Best First'.
Re: Real Basic Perl OOP Question
by Zaxo (Archbishop) on Jun 21, 2005 at 22:18 UTC

    I'm not sure why you couldn't find your previous post. Merlyn answered your second question there, and I answered the first.

    As I showed there, you're misusing arguments in right arrow method calls.

    After Compline,
    Zaxo

Re: Real Basic Perl OOP Question
by BUU (Prior) on Jun 21, 2005 at 22:16 UTC
    You mean this: node?
Re: Real Basic Perl OOP Question
by Joost (Canon) on Jun 21, 2005 at 22:18 UTC
Re: Real Basic Perl OOP Question
by Codon (Friar) on Jun 21, 2005 at 22:16 UTC
    This is a very basic error. bless() returns a blessed reference; it does not operate on the referent. In otherwords, when you do this:
    bless $objref, $class
    you are not getting the benefits of the bless(). Try this instead:
    my $self = bless $objref, $class; return $self;
    Ivan Heffner
    Sr. Software Engineer, DAS Lead
    WhitePages.com, Inc.

      That is not correct - bless alters its first argument in-place:

      use Data::Dumper; my ( $ref1, $ref2 ) = ( {}, {} ); my $ref3 = bless $ref1, 'Foo'; # capturing results bless $ref2, 'Foo'; # in void context print Dumper $ref3; print Dumper $ref2; __END__ # results identical: $VAR1 = bless( {}, 'Foo' ); $VAR1 = bless( {}, 'Foo' );

      bless returns the reference as a convenience, however it certainly operates on the reference. (It doesn't alter that which is referenced {the "referent"?}... but he's not doing that, and that isn't what you are demonstrating either.)

        This is awesome. I love this site.

        Thanks, man.
        My "this is awesome" comment was meant to be a reply to you...
      Hi Codon,

      Thank you. Though I am confused.

      Based on a recommendation by Zaxo (where I posted this before and didn't realize it was available), I changed:

      my $name2 = Message->name( $message_obj );

      to

      my $name2 = $message_obj->name( );

      and it worked! :-)

      Tony
Re: Real Basic Perl OOP Question
by dynamo (Chaplain) on Jun 21, 2005 at 22:21 UTC
    I had written a reply here, but going over it I now realize it was incorrect. There's no delete facility, and I don't want to leave misinformation up, so please ignore this node. Or use it as an opportunity to comment on what I should have done rather than write this.

      People make mistakes all the time. Just a couple days ago I made one here, and was promptly (and politely) corrected. The thing is though, seeing the mistake that was made can still help people, as long as it is marked as a mistake. Someone else may be doing the exact same thing, and not realize that what they are doing is potentially wrong.

      If you look the node where I made my mistake (Re: Image display firefox problem) you'll see that it is clearly marked as being wrong, but people can still tell what I did wrong. You obviously put some effort into your original post, but now it has no chance of ever helping anyone.

      In the future, I would consider leaving your answer in place and either correcting the mistake, or clearly marking it as wrong.

        The thing is, the error I made in the original reply was only made because I thought one of the lines above was not commented out when it was. It was a perceptual error on my part. Anyway, thanks for the advice, and especially the example. I didn't think to just strikeout the text of the original.