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

Hi, this is making me crazy.

I have a subroutine/method in an object, looks kinda like this:

sub generate { my ($self, $OBJS) = @_; # NOTE: # $OBJS is an array of objects, and array elements can also be arrays +of objects while(SOMETHING) { if ($OBJS) { # we got anything? if (scalar(@{$OBJS})) { # 0-length array is useless my $OBJ = shift(@{$OBJS}); DO MEAN STUFF TO THE OBJ } } } return LA-LA; }
The first time into the while(SOMETHING) loop everything goes as planned, as would be expected, BUT.......!!!

The second time through the loop, $OBJ has the remnants of the MEAN STUFF that was done to it.

So, after looking around and seeing about deep copying, and FreezeThaw and Clone, and trying them out, I still can't get rid of the evidence of the MEAN STUFF I did to $OBJ.

Here's an example of what I've tried, I tried to loop through the array building copies, like this:

my $OBJScopy; foreach my $obj (@$OBJS) { push(@{$OBJScopy}, clone($obj)); }
and the subroutine from above references $OBJScopy instead of $OBJS

but later when I try to do MEAN STUFF to $OBJ it still bears the scars of earlier mistreatment.

Why is this so difficult? What am I overlooking?

THANKS!!!!!

Tosh

Replies are listed 'Best First'.
Re: how do I copy an array of objects?
by thinker (Parson) on Nov 12, 2001 at 16:13 UTC
    Hi AM,

    If I get what you are doing correctly, you want to change this
    sub generate { my ($self, $OBJS) = @_; }
    to
    sub generate { my ($self, [@$OBJS]) = @_; }
    where you dereference $OBJS then return a reference to the dereferenced array, therefore returning a deep copy. Here is an example of what I mean.
    #!/usr/bin/perl -w use strict; my $original =[1,2, 3]; my $shallow_original=$original; my $deep_original=[@$original]; $original->[1]=42; print "original: @$original\n"; print "shallow: @$shallow_original\n"; print "deep: @$deep_original\n";
    which, when run, would produce
    original: 1 42 3 shallow: 1 42 3 deep: 1 2 3
    cheers

    thinker

    Edit to add return values.
    thinker
      >># NOTE:
      >># $OBJS is an array of objects, and array elements can also be arrays of objects

      I should have read the question better before I answered :-)
      The code I gave will only dereference down 1 layer.
      The code below will explain this better, I hope.
      Anyway, I think what you really want is Clone.
      I just downloaded it to test this code, and already I see it as an invaluable tool in my Perl toolbox.
      Here is an example of how my earlier code fails, and how Clone can help you.

      Good luck

      thinker

      #!/usr/bin/perl -w use strict; use Clone qw(clone); my $original =[1,2,[3,4,5]]; my $shallow_original=$original; my $deep_original=[@$original]; #deep, but not deep enough!! my $clone_original=clone($original); # this behaves fine. $original->[1]=42; $original->[2][1]=77; print "original: @$original\n"; print "shallow: @$shallow_original\n"; print "deep: @$deep_original\n"; print "shallow[2][1]: $shallow_original->[2][1]\n"; print "deep[2][1]: $deep_original->[2][1]\n"; print "clone[2][1]: $clone_original->[2][1]\n";
      The output of which is
      original: 1 42 ARRAY(0x80cf9a4) shallow: 1 42 ARRAY(0x80cf9a4) deep: 1 2 ARRAY(0x80cf9a4) shallow[2][1]: 77 deep[2][1]: 77 clone[2][1]: 4