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)

In reply to REAL Basic Perl OOP Questions by o2bwise

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.