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

Hmmm... I thought I logged in for my last question.... oh well.

Thinker had some suggestions to my question but it didn't seem to work.

I get this error when I try his first sugestion of my ($self, [@$OBJS]) = @_;

Can't declare single ref constructor in "my"
whatever that means

The second suggestion of using Clone.pm should work, and it does work with his example, BUT, it doesn't seem to work with an array of objects, which are slightly more complex data structures.

I suspect I may just have to hack around this problem, bah...

Thanks for any insight....

Tosh

Replies are listed 'Best First'.
Re: Still trying to copy an array of objects
by ehdonhon (Curate) on Nov 15, 2001 at 18:03 UTC

    The square brackets are being used to build an anonymous hash out of @$OJBS in a void context there. I'd bet that is what it doesn't like. You shouldn't need them. Try this:

    my ($self, @$objs) = @_;

      Try this: my ($self, @$objs) = @_;

      This doesn't work either. There are two ways to do this, depending on how you want to call the subroutine - and disregarding for the moment that this is supposedly a method of an object.

      # (1) call with a reference to an array my $ref = [1,2,3]; foo(1,$ref); sub foo { my ($self, $aref) = @_; # change the element in $ref $aref->[0] = 3; # make a copy my @array = @$aref; $array[0] = 3; } ######################################## # (2) or call with an array of values my @arr = (1,2,3); foo(1,@arr); sub foo { my ($self, @array) = @_; # @array is already a copy $array[0] = 3; }

      A totally different problem is the deep copying of objects. This depends very heavily on the object being copied and cannot be done correctly in a general way. The reason for this is that some parts of the object might have to be duplicated normally, but some might have to be treated in a special way (think e.g. of IO objects connected to files) that depends on the purpose and implementation of the object itself.

      There should be a method of the object giving back a deep copy. Only this method can handle the operation correctly for the corresponding object.

      -- Hofmator

        Wow, that sucks, but I suspect you're right, especially in light of the flashback I just had to my OO courses in Uni.

        So..... to be a lazy monk and program around it, or to build me a copy method.....

        Actually I feel kinda dumb, because now that I think of it my objects have DBH's in them, which really shouldn't be copied I don't think.

        Thanks!!! Tosh
Re: Still trying to copy an array of objects
by princepawn (Parson) on Nov 15, 2001 at 20:14 UTC
    When doing deep copies I use the dclone function in Storable. You might also like to see the tutorial on deep copying that is part of Parse::RecDescent::FAQ.
Re: Still trying to copy an array of objects
by DrManhattan (Chaplain) on Nov 15, 2001 at 18:16 UTC
    You can copy the contents of your referenced array to a new array like this:
    my @tmp = @$OBJS; # Do whatever you want to @tmp. It won't affect $OBJS

    -Matt