Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

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
[moritz]: seems there's a size limit for hostenum, and the error message just sucks
[vrk]: None, other than the location of the error: auto/NetAddr/IP/
[vrk]: Aha, there's a reference to "bug report 82719" in NetAddr/'s sub _splitref.
[moritz]: wow, that's their idea of an "improved error message"
[moritz]: how about "This network is too large to enumerate all host addresses." or so?
[vrk]: Well, there's a call to notcontiguous() before the error, whose description is "counts the bit positions remaining in the mask when the rightmost '0's are removed"
[vrk]: Clear as mud!

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (9)
As of 2017-04-25 07:44 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (449 votes). Check out past polls.