I have this simple code:
(我有这个简单的代码:)
#!/usr/bin/perl
@inp = map 2**$_, 0..6;
@cc = grep {
my $num = $inp[$_];
my $sum; #---- HERE, I have to have the var declared first, before init.
$sum += $_ for (split //, $num);
print "$sum
";
$sum % 2;
} 0..$#inp;
Here, the the $sum
will be used in for loop, However in this case:
(在这里, $sum
将用于for循环,但是在这种情况下:)
#!/usr/bin/perl
@inp = map 2**$_, 0..6;
@cc = grep {
my $num = $inp[$_];
my $sum += $_ for (split //, $num); # HERE, Trying to autovificate - wont work
print "$sum
";
$sum % 2;
} 0..$#inp;
But when I used var $sum
at the same line with for loop - that means I am trying to declare and initiate at once - where should work the autovivifaction - As i would expect to autovivificate the $sum
to zero (because used with math operator +=
), but will not work, but why so?
(但是,当我在for循环的同一行上使用var $sum
时-这意味着我试图立即声明和初始化-autovivifaction应该在哪里工作-正如我希望将$sum
实现为零(因为与数学运算符一起使用+=
),但不起作用,但为什么会这样呢?)
What are the rules for autovivification? (自我生存的规则是什么?)
ask by Herdsman translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…