jeffa has asked for the wisdom of the Perl Monks concerning the following question:
I have a Person class that appears to be correctly coded, but I will print it at the bottom of my question just in case. My problem is in the use of said class.
The Person class has a data field called NAME and method to set and get it's value called name(). My client code instantiates a number of Person objects and stores them in a list:
No problems here, now let's say that I want to iterate through list and print the names of my employees:use Person; use strict; my @employees = []; foreach (qw(John Betty Larry Joe Sally Laura Bubba)) { my $person = Person->new(); $person->name($_); push @employees, $person; }
Uh oh - upon trying to interpret the script, Perl yacks:foreach my $employee (@employees) { print $employee->name(), "\n"; }
So I changed the print statement to:
and got:print ref($employee), "\n";
ARRAY Person Person Person Person Person Person Person
Can anyone tell me where did that ARRAY came from?
Here is my Person class as promised: (can anyone say Perltoot?)
Thanks!package Person; sub new { my $proto = shift; my $class = ref($proto) || $proto; my $self = {}; $self->{NAME} = undef; bless ($self, $class); return $self; } sub name { my $self = shift; if (@_) { $self->{NAME} = shift } return $self->{NAME}; } 1;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Blessed be the OOP'ers
by mdillon (Priest) on Jun 29, 2000 at 01:07 UTC | |
by chromatic (Archbishop) on Jun 29, 2000 at 02:26 UTC | |
by pemungkah (Priest) on Jun 29, 2000 at 02:34 UTC |