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.
source share