开发者

How to "unlink" a Perl array reference from the referenced variable?

In Perl 5.10.1:

#!/usr/bin/perl

my @a = (1, 2, 3);
my $b = \@a;开发者_运维百科
print join('', @{$b}) . "\n";
@a = (6, 7, 8);
print join('', @{$b}) . "\n";

This prints 123 then 678. However, I'd like to get 123 both times (i.e. reassigning the value of @a will not change the array that $b references). How can I do this?


Make a reference to a copy of @a.

my $b = [ @a ];


Bretter use dclone for deep cloning of references pointing to anonymous data structures.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜