Perl dynamic link to array

I am trying to understand in Perl the difference between a normal array reference \ @array and [@array].

The following article http://perl-begin.org/tutorials/perl-for-newbies/part2/ says: "An array surrounded by square brackets ([@array]) returns a dynamic link to the array. This link does not directly affect other values, so it's called dynamic. "
The last sentence above, where it says that the link does not directly affect other values, is not clear to me, what other values โ€‹โ€‹do they mean? Several websites copied and entered the same explanation. Can someone give a better explanation that emphasizes the differences?

Here is an example they provided:

use strict; use warnings; sub vector_sum { my $v1_ref = shift; my $v2_ref = shift; my @ret; my @v1 = @{$v1_ref}; my @v2 = @{$v2_ref}; if (scalar(@v1) != scalar(@v2)) { return undef; } for(my $i=0;$i<scalar(@v1);$i++) { push @ret, ($v1[$i] + $v2[$i]); } return [ @ret ]; } my $ret = vector_sum( [ 5, 9, 24, 30 ], [ 8, 2, 10, 20 ] ); print join(", ", @{$ret}), "\n"; 

However, in the above example, if I change return [@ret]; to \ @ret, the program returns the same result, so I'm not sure how this serves as an example to illustrate a dynamic link.

Thanks.

+4
source share
3 answers

I ask this question. When perl docs use the term dynamic, they almost always refer to the scope of a variable. You will not find the consideration of "dynamic arrayref" in perlref and perlreftut .

That said:

 \@array # reference to @array [@array] # reference to an unnamed *copy* of @array 

Consider what happens when we link to a link, or a link to a copy, @ARGV:

 $ perl -E '$a = \@ARGV; $a->[0] = "FOO"; say for @ARGV' blah blah FOO blah $ perl -E '$a = [@ARGV]; $a->[0] = "FOO"; say for @ARGV' blah blah blah blah 
+5
source

I am trying to understand in Perl the difference between a normal array reference \@array and [@array] .

They are both the same links; they just produce references to different arrays.

 [ ... ] 

- this is basically the same as

 do { my @anon = (...); \@anon } 

So,

 my @abc = qw( abc ); my $ref1 = \@abc; my $ref2 = [ @abc ]; say @$ref1, @$ref2; # abcabc @abc = qw( def ); say @$ref1, @$ref2; # defabc 

"This link does not directly affect other values, so itโ€™s called dynamic.

It is not called โ€œdynamic,โ€ and it is not a definition of dynamic that I have ever encountered.

+2
source

Simple illustration:

 my @array = (2, 3, 5, 6); my $ref = \@array; my $dynamic_ref = [@array]; print q{$array[1] = }, $array[1], "\n"; print q{$ref->[1] = }, $ref->[1], "\n"; print q{$dynamic_ref->[1] = }, $dynamic_ref->[1], "\n"; $array[1] = 10; print "=" x 20, "\n"; print q{$array[1] = }, $array[1], "\n"; print q{$ref->[1] = }, $ref->[1], "\n"; print q{$dynamic_ref->[1] = }, $dynamic_ref->[1], "\n"; 

In other words, you have a link to a copy of the original array, and all changes in the original array do not affect this copy.

0
source

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


All Articles