in reply to how do I copy an array of objects?
tosub 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.sub generate { my ($self, [@$OBJS]) = @_; }
which, when run, would produce#!/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";
cheersoriginal: 1 42 3 shallow: 1 42 3 deep: 1 2 3
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: how do I copy an array of objects?
by thinker (Parson) on Nov 12, 2001 at 21:02 UTC |