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.

@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!
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.

In reply to Re: array element as foreach iterator by ambrus
in thread array element as foreach iterator by kingm61

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.