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:
sub new
{
my $class = shift;
return bless($Person::list,shift);
}
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 say
sub new
{
my $class = shift;
return bless({},$class);
}
Continuing down your code, your methods aren't reading the object they should be working on. You want to say e.g.
sub name
{
my $self = shift;
$self->{NAME} = shift;
}
Do this, and you should be able to say e.g.
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?