IL_MARO has asked for the wisdom of the Perl Monks concerning the following question:
Now, imagine you want to create another class, say, called Population. And you want a bag of Animals, like:package Animal; # constructor sub new { my $this = {}; # object properties $this->{DNA} = []; bless $this; return $this; } sub getDNA { my $this = shift; return "@{$this->{DNA}}"; } sub setDNA { my $this = shift; @{$this->{DNA}} = @_; return @{$this->{DNA}}; }
As you can see, the method init simply pushes 5 not empty Animals into a Population object.package Population; use Animal; # constructor sub new { my $this = {}; # object properties $this->{individual} = []; bless $this; return $this; } sub init { for ($i=0; $i<5; $i++) { my $empl = Animal->new(); $empl->setDNA(4, 6, 8, 10); push (@{$this->{individual}}, $empl); } }
But of course it's not working.sub getTheFirst{ my $this = shift; return @{$this->{individual}}[0]->getDNA(); }
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: arrays of object in OO perl
by ikegami (Patriarch) on Nov 19, 2008 at 06:17 UTC | |
by IL_MARO (Initiate) on Nov 19, 2008 at 06:47 UTC | |
by ChOas (Curate) on Nov 19, 2008 at 07:50 UTC | |
by IL_MARO (Initiate) on Nov 19, 2008 at 07:57 UTC | |
Re: arrays of object in OO perl
by gwadej (Chaplain) on Nov 19, 2008 at 04:58 UTC | |
by IL_MARO (Initiate) on Nov 19, 2008 at 06:34 UTC | |
Re: arrays of object in OO perl
by ikegami (Patriarch) on Nov 19, 2008 at 06:14 UTC | |
Re: arrays of object in OO perl
by matrixmadhan (Beadle) on Nov 19, 2008 at 06:10 UTC | |
by GrandFather (Saint) on Nov 19, 2008 at 08:49 UTC | |
by matrixmadhan (Beadle) on Nov 20, 2008 at 04:20 UTC | |
by GrandFather (Saint) on Nov 20, 2008 at 04:34 UTC |