I think maybe a better question would be, "why does for (LIST) {} localize $_". The answer would be that $_ (and in fact whatever iterator var is used) is aliased to value in the list. This allows all kind of funkyness, but also effectively means that the var used is localized (otherwise it couldnt be an alias temporarily.)

Contrast this to a three argument for loop (which is really a while loop in disguise, or perhaps its the other way around ;-), which doesnt alias, and doesnt localize.

Going back to the IO operator, how could we alias it? The data comes from a file. Theres nothing to alias to. A change to the var that we read in won't change the file. Wheras a change to the var in a for loop does change the thing we are iterating over. Both of these behaviours are sensible and useful, but you have to ask the right question.

E:\>perl -le "$x=10; @a=(0..5); for $x (@a) { print $x; $x=int(rand 10 +0); } print $x; print qq(@a)" 0 1 2 3 4 5 10 41 44 77 55 23 70

BTW, of course Abigail-II is right that by now theres no way to tell, (at least not without lots of research, and even still it might come down to an on the spot decision by Larrry that he may or may not remember), but i would guess that aliasing is part of it.


---
demerphq

    First they ignore you, then they laugh at you, then they fight you, then you win.
    -- Gandhi



In reply to Re: Why doesn't the angle operator localize $_ by demerphq
in thread Why doesn't the angle operator localize $_ by welchavw

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.