dclone
takes and returns references.
#! /usr/bin/perl
use Storable qw/ dclone /;
my @array = ( [1 .. 3], qw/ apple orange banana / );
my $copy = dclone \@array;
print ref($copy), "\n";
The output of the program above is ARRAY
, so to get a deep-cloned array, use
my @copy = @{ dclone \@array };
To show what was going on without the dereference, the output of
my @copy = dclone \@array;
for (0 .. 4) {
print "$_: ",
(defined $copy[$_] ? $copy[$_] : "<undef>"),
"\n";
}
is
0: ARRAY(0x1d396c8)
1: <undef>
2: <undef>
3: <undef>
4: <undef>
So assigning the result of dclone
to an array will produce a single-element array, and trying to fetch any value other than the zero-th yields the undefined value.