To answer the original question, why is it a syntax error?
Going back to perlsyn, here's what it says about the variable in a foreach loop:
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.
So even though $array[0] is a scalar value, it cannot exist because there is no local version of @array within the loop. Even if there were a local version of @array within the loop, you would still have to copy those values to the external version of @array from within the loop. | [reply] |