Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Blessed be the OOP'ers

by jeffa (Chancellor)
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 ./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!

Comment on Blessed be the OOP'ers
Select or Download Code
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
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? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2014-07-10 05:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (199 votes), past polls