Its not counterintuitive.
From the perlsyn documentation:
The foreach loop iterates over a normal list value and sets the variable VAR to be each element of the list in turn. If the variable is preceded with the keyword my, then it is lexically scoped, and is therefore visible only within the loop. Otherwise, the variable is implicitly local to the loop and regains its former value upon exiting the loop. If the variable was previously declared with my, it uses that variable instead of the global one, but it's still localized to the loop. The foreach keyword is actually a synonym for the for keyword, so you can use foreach for readability or for for brevity. (Or because the Bourne shell is more familiar to you than csh, so writing for comes more naturally.) If VAR is omitted, $_ is set to each value.
You got it backwards; its "true form" is foreach, followed by a variable, followed by a list with the special case that not writing the var makes it $_ . Also, to avoid that kind of problems, is highly recomended that you put the list inside (), so you know is a list.
Jesús Couto F. (now, where did I put my password for this site...)
In reply to Re: Re: Re: Re: Missing $ on loop variable?
by Anonymous Monk
in thread Missing $ on loop variable?
by Cody Pendant
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |