开发者

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

开发者 https://www.devze.com 2023-01-04 10:40 出处:网络
In Perl 5.10.1: #!/usr/bin/perl my @a = (1, 2, 3); my $b = \\@a;开发者_运维百科 print join(\'\', @{$b}) . \"\\n\";

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

精彩评论

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