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
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.