GGGav has asked for the wisdom of the Perl Monks concerning the following question:

Folks,

I am having a problem which I have so far failed to solve myself mainly, I suspect, because I don't understand the problem well enough!

I have a package with an object TESTOBJ which includes a name and, as one of the elements, an array (__mpp). When I use this object I want to be able to store up to ten values in __mpp.

In my application I am trying to build an array of TESTOBJ using the constructor to create each object. The problem is that I seem only to have one array (__mpp) shared between all my objects!

In the sample code below I create two TESTOBJ and populate them but when I set __mpp in the second object it affects __mpp in the first. Why is this ?

use TESTOBJ; my @res = (); my $t = new TESTOBJ; $t->name("first"); $t->mpp0(111); $res[0] = $t; printf "Dump \$res[0]: name '%s' mpp '%d'\n", $res[0]->name(), $res[0]->mpp0(); $t = new TESTOBJ; $t->name("second"); $t->mpp0(222); $res[1] = $t; printf "Dump \$res[0]: name '%s' mpp '%d'\n", $res[0]->name(), $res[0]->mpp0(); printf "Dump \$res[1]: name '%s' mpp '%d'\n", $res[1]->name(), $res[1]->mpp0();

And TESTOBJ.pm contains

package TESTOBJ; my %fields = ( __name => undef, __mpp => [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ] ); sub new { my $that = shift; my $class = ref($that) || $that; my $self = { %fields }; bless $self, $class; return $self; } sub name { my $obj = shift; @_ ? $obj->{__name} = shift # modify attribute : $obj->{__name}; # retrieve attribute } sub mpp0 { my $obj = shift; @_ ? $obj->{__mpp}[0] = shift # modify attribute : $obj->{__mpp}[0]; # retrieve attribute }

Regards, Gavin

Replies are listed 'Best First'.
Re: Anonymous array in object
by ccn (Vicar) on Oct 23, 2008 at 12:09 UTC

    You have one anonymous array [-1, .... -1] for all objects.

    If you will create an array inside new the error disappears

    sub new { my $that = shift; my $class = ref($that) || $that; my $self = { __name => undef, __mpp => [ (-1) x 10 ] }; bless $self, $class; return $self; }

      Thanks ccn. That works for me.

Re: Anonymous array in object
by Corion (Patriarch) on Oct 23, 2008 at 11:57 UTC

    With

    %fields = ( __mpp => [ -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 ], )
    and
    ... my $self = { %fields }; ...

    you only copy the reference and not the array itself. So all objects use the same array, which is not what you want. Consider using Storable::dclone to copy the default structure.

Re: Anonymous array in object
by JavaFan (Canon) on Oct 23, 2008 at 12:13 UTC
    You are indeed sharing an array. Remember, [ ] isn't an array - it's a reference to an array. And it's that reference that you copied.

    One way to solve this is to write.

    my $self; $$self{__name} = $fields{__name}; $$self{__mpp} = @{$fields{__mpp}};
    Note that with the give code, you don't need to initialize $$self{__name}, as Perl will autovivify it if necessary.