szabgab has asked for the wisdom of the Perl Monks concerning the following question:
When using strict perl requires to declare even the loop variable of a foreach loop. Why is that? After all the $word variable in the above example is an alias to the values and won't have any effect outside of the loop. Even if written this way:use strict; use 5.010; foreach my $word (qw(abc def ghi)) { say $word; }
Why is this code not allowed?use strict; use 5.010; my $word = "hello"; foreach $word (qw(abc def ghi)) { say $word; } say $word; # word is "hello" here
use strict; use 5.010; foreach $word (qw(abc def ghi)) { say $word; } # generate syntax error if $word is used here
Back to
Seekers of Perl Wisdom