in reply to Classes Are Killing Me
Always turn on warnings (<samp>perl -w</samp> or <samp>use warnings</samp> in newer Perls); for that matter, use strict unless you have good reason not to. In your case, -w would have caught the bug in your constructor:
doesn't use $class, instead preferring to shift some other element off of @_ to use as the class name. Additionally, you're always blessing and returning the same object (needlessly stored in a global variable), which is probably not what you wanted to do. In your case, you're passing undef to bless; you want to saysub new { my $class = shift; return bless($Person::list,shift); }
Continuing down your code, your methods aren't reading the object they should be working on. You want to say e.g.sub new { my $class = shift; return bless({},$class); }
Do this, and you should be able to say e.g.sub name { my $self = shift; $self->{NAME} = shift; }
my $person = new Person; $person->name('Me');
I'm curious, though, why you say ``the code in "Perl Cookbook" didn't work''. Could you please point us to what isn't working there?
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Re: Classes Are Killing Me
by straywalrus (Friar) on May 19, 2002 at 12:50 UTC | |
by Anonymous Monk on May 19, 2002 at 21:49 UTC | |
by Anonymous Monk on May 19, 2002 at 22:33 UTC | |
by Anonymous Monk on May 20, 2002 at 00:02 UTC | |
by Anonymous Monk on May 20, 2002 at 03:34 UTC |
In Section
Seekers of Perl Wisdom