Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Blessed be the OOP'ers

by jeffa (Bishop)
on Jun 29, 2000 at 01:02 UTC ( #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 ./ line xx

So I changed the print statement to:

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


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;

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://20253]
Approved by root
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2017-03-26 07:51 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (313 votes). Check out past polls.