How to "detach" a reference to a Perl array from a reference 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 would like to get 123 both times (i.e. reassigning the @a value @a not change the array that refers to $b ). How can i do this?

+4
source share
2 answers

Make a link to a copy of @a .

 my $b = [ @a ]; 
+3
source

Bretter uses dclone to deeply clone links pointing to anonymous data structures.

0
source

Source: https://habr.com/ru/post/1313771/


All Articles