in reply to Re: Still trying to copy an array of objects
in thread Still trying to copy an array of objects

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

Replies are listed 'Best First'.
Re: Re: Re: Still trying to copy an array of objects
by Anonymous Monk on Nov 16, 2001 at 07:45 UTC
    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