in reply to Problem in Passing A Multidimensional Array into a Function
Perl doesn't talk about "pointers", it uses "references". It doesn't have multi-dimensional arrays, Perl has arrays of references (each of which may be to an array). Perl always passes parameters to subs by reference.
If you want a temporary copy of a complicated data structure (that is really pretty much anything using references) you need to make a "deep copy". Perl doesn't have an inbuilt way to do that, but there are a number of modules that do the trick. Most common is Storable. Consider:
use strict; use warnings; use Storable; use Data::Dump::Streamer; my $ref = [ {a => 1, b => 2, c => 3}, [1, 2, 3], {x => [4, 5, 6], y => [7, 8, 9]}, ]; my $org = Dump ($ref)->Out (); mangle ($ref); my $after = Dump ($ref)->Out (); print "Structure unmangled" if "$org" eq "$after"; sub mangle { my $cloned = Storable::dclone ($_[0]); $cloned->[0]{a} = 6; $cloned->[1][0] = 23; Dump $cloned; }
Prints:
$ARRAY1 = [ { a => 6, b => 2, c => 3 }, [ 23, 2, 3 ], { x => [ 4, 5, 6 ], y => [ 7, 8, 9 ] } ]; Structure unmangled
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Problem in Passing A Multidimensional Array into a Function
by smiffy (Pilgrim) on Oct 02, 2008 at 02:25 UTC |