in reply to Still trying to copy an array of objects

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) = @_;

Replies are listed 'Best First'.
Re: Re: Still trying to copy an array of objects
by Hofmator (Curate) on Nov 15, 2001 at 18:49 UTC
    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