in reply to arrays of object in OO perl

Use use strict;! There's a major error in init.

As for the problem with getTheFirst, since it would be
$array[0]->getDNA()
if you were dealing with an array instead of a reference to an array, then it's
${$array_ref}[0]->getDNA()
when using an array ref. It can also be written as
$array_ref->[0]->getDNA()
So all together, we get
$this->{individual}->[0]->getDNA()</c>

See

Replies are listed 'Best First'.
Re^2: arrays of object in OO perl
by IL_MARO (Initiate) on Nov 19, 2008 at 06:47 UTC
    Guys, may the allmighty Spaghetti Monster bless you all!
    I used strict, as suggested; I corrected all the mistakes. And now it works.
    I know, THOU SHALL USE STRICT... but I was lazy. Now I made another step toward enlightment. Thanks again.


    EDIT: it's not working... I mean: if I use the first slot of the array, it works:
    return $this->{individual}->[0]->getDNA();
    But if I use another slot, it turns out that is undefined.
    return $this->{individual}->[2]->getDNA(); This means that I don't have an array of 'individual', despite I pushed 5 of them into the array. Mmm...
    sub init { my $i; foreach $i (0 .. 4) { my $this = shift; my $empl = Animal->new(); $empl->setDNA(4, 6, 8, 10); push (@{$this->{individual}}, $empl); print " ".$i."\n"; print $this->{individual}->[$i]->getDNA(); } }

    In this way, the output is
    0 4 6 8 10 1 Can't call method getDNA ...
    And thus definitively I don't have and array of Animal, but individual is just a scalar. Damn.
      You want to take the
      my $this = shift;
      Outside of the for-loop.

      GreetZ!,
        ChOas

      print "profeth still\n" if /bird|devil/;
        Ok, I admit I'm a dumb;
        I hope I'm just soooooo tired... :-P