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

Esteemed Monks,

I am having basic obstacles with my very first foray into OOP perl. I am trying to follow along with Damian Conway’s Object Oriented Perl. At this point, all I wanted to do is create an extremely simple class and validate success by printing out the value assigned to an object of that class.

My first question is related to Conway’s Listing 3.1 (p. 81), specifically the bless function where it appears to me that it is not returning anything (no reference to the object that I can see anyway). I used what I think is analogous code:

#!/usr/bin/perl -w package Message; use strict; sub new { my ($class) = @_; bless { _name => $_[1], _subject => $_[2], _message => $_[3], }, $class; } 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($objType, $name, $subject, $message ); my $name2 = Message->name($message_obj); print "\nMy name is $name2\n";


It complains about the following line under sub name:

return $self->{_name};

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

Note: I have a few commented lines in the code on another box. Line 23 is the line I specified (above) that is in sub name.

Anyway, I was perplexed in that sub new didn’t seem to return anything, so I resorted to a method like Conway’s sub new code on the top of p. 79 of his book:

#!/usr/bin/perl -w package Message; use strict; sub new { my ($class) = @_; my $objref = { _name => $_[1], _subject => $_[2], _message => $_[3], }; 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($objType, $name, $subject, $message ); my $name2 = Message->name($message_obj); print "\nMy name is $name2\n";


where I explicitly return a reference to the object. And I get the same error anyway (complaining about the same line in sub name).

So, my questions are:
1. Why is it complaining about that line number?

2. Why is it OK for sub new to NOT return a reference to the object?

Thanks in advance…

Tony (o2)

Replies are listed 'Best First'.
Re: REAL Basic Perl OOP Questions
by Zaxo (Archbishop) on Jun 19, 2005 at 20:39 UTC

    On #1, your right arrow usage is wrong.

    You're putting an extra argument into your new call. The Message->new syntax causes 'Message' to appear as the first argument. Your $objType argument causes "Message" to appear as the _name element of the object instance. Call new like this: my $message_obj = Message->new($name, $subject, $message );

    The name method should be called as my $name2 = $message_obj->name(); The expression Method->name($message_obj) calls name with two arguments; name('Method', $message_obj). The string "Method" is going into the $self slot and then name tries to dereference it as the object, which has been ignored.

    After Compline,
    Zaxo

Re: REAL Basic Perl OOP Questions
by merlyn (Sage) on Jun 19, 2005 at 20:12 UTC
    Anyway, I was perplexed in that sub new didn’t seem to return anything,
    Ahh, but all subroutines automatically return the last expression evaluated, and that new evaluates the bless as the last thing, and bless happens to return the blessed object.

    So, it's doing the right thing.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Thanks, Merlyn.

      In fact, after posting this, I noticed this on p. 79, 2nd par. of Object Oriented Perl by Damien Conway.

      It felt good to understand something before someone told me!

      Tony