Why is Perl foreach variable assignment modifying the values in the array?

前端 未结 7 2293
一整个雨季
一整个雨季 2020-12-18 19:58

OK, I have the following code:

use strict;
my @ar = (1, 2, 3);
foreach my $a (@ar)
{
  $a = $a + 1;
}

print join \", \", @ar;

and the outp

7条回答
  •  情歌与酒
    2020-12-18 20:02

    Try

    foreach my $a (@_ = @ar)
    

    now modifying $a does not modify @ar. Works for me on v5.20.2

提交回复
热议问题