in reply to array element as foreach iterator
I think it's by design. Foreach aliases the variable. This limitation is so that you can't easily create arrays with aliased elements, or people would end up with modifying an array element and wondering why an other array has changed. People would keep asking why that happens.
Update: Let me meake this clearer. You can have array elements aliased to each other like this.
Clearly, no-one would actually write code like this (except in How's your Perl? (II)). If, however, foreach allowed us to create such strange arrays, that could cause strange bugs.@y = ("this", "that"); *x = &{sub {\@_}}(@y[reverse 0..@y-1], "your"); + print "@x\n"; # ==> that this your $y[0] = "great"; # I change @y print "@x\n"; # ==> that great your # hey, why did @x change, I didn't do anything with @x!
|
|---|