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?
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|