Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Blessed be the OOP'ers

by jeffa (Bishop)
on Jun 29, 2000 at 01:02 UTC ( [id://20253]=perlquestion: print w/replies, xml ) Need Help??

jeffa has asked for the wisdom of the Perl Monks concerning the following question:

I am playing with objects and came across a stumbling block.

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:

use Person; use strict; my @employees = []; foreach (qw(John Betty Larry Joe Sally Laura Bubba)) { my $person = Person->new(); $person->name($_); push @employees, $person; }
No problems here, now let's say that I want to iterate through list and print the names of my employees:
foreach my $employee (@employees) { print $employee->name(), "\n"; }
Uh oh - upon trying to interpret the script, Perl yacks:
Can't call method "name" on unblessed reference at ./foo.pl line xx

So I changed the print statement to:

print ref($employee), "\n";
and got:

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?)

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;
Thanks!

Replies are listed 'Best First'.
Re: Blessed be the OOP'ers
by mdillon (Priest) on Jun 29, 2000 at 01:07 UTC
    you are initializing @employees as an array containing a single element, which is an anonymous array. the initialization should read: my @employees = (); this will initialize it as an array with no elements.
      More specifically, the original code could be written this way: my $employees = []; Hopefully that makes it clear that a list enclosed in square brackets is an anonymous list. Assigning that somewhere creates a reference. (That's why you can stuff it in a scalar.)

      The original code creates @employees and sets the first (and only) element to a reference to that anonymous list.

        And the push would then be
        push @$employees, $person;
        
        with printing them looking like
        foreach $p (@$employees) {
           print $p->name(),"\n";
        }
        
        Using the array reference would make it easy to stuff this list of employees into another object/list/whatever.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://20253]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (6)
As of 2024-04-23 12:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found