I always wonder why I must write
foreach my $x (@arr)
instead of
foreach my $x @arr
What is the purpose of the parentheses here?
I can only think of one concrete reason for it. The following code is valid:
for ($foo) {
$_++
}
If you were to change that to
for $foo {
$_++
}
Then you would really be saying
for $foo{$_++}
(i.e. a hash lookup). Perl 6 gets around this issue by changing the way whitespace works with variables (you can't say %foo <bar>
and have it mean the same thing as %foo<bar>
).
BTW, you can use the expression form of the for
without parentheses like this:
s/foo/bar/ foreach @arr;
or
do { ... } foreach @arr;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With