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
In reply to Re: Re: Still trying to copy an array of objects
by Hofmator
in thread Still trying to copy an array of objects
by tosh
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |