Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I read with interest the post, here. It discusses the scope of the indexing variable in "for" and "foreach" loops. At a working level of understanding, the indexing variable is localised to the loop. It says in the man page that "foreach" is a synonym for "for".

However, "for" is not always a synonym for "foreach". Care must be used if one is to say it's a "misspelling" or they can be used "interchangeably". "For" looks to behave differently depending on it's context: c-style vs. (er) foreach-style. If one uses c-style "for" loops you don't always get localised indexing variables as you seem to in "foreach" loops.

UPDATE: the above paragraph is wrong - "for" and "foreach" can be used interchangeably. The key point for me is that "for" is overloaded and behaves differently depending on the form of the arguments. ikegami's response sums it all up nicely in my opinion. Ironically "foreach" which is supposed to (and does) aid the programmer by letting him know explicitly what form of "for" is being used, tricked me into thinking about it in a weird way.

Anyhow, I'm interested in why Perl has the two styles of "for" constructs: convenience to the programmer seems the likely answer? I certainly appreciate the "foreach" loop. I'm assuming the "foreach" loop came after the "c-style for" loop - is this the correct Perl history? And if so, why was "foreach" implemented in a way such that the indexing variable is localised. Was this an ease of implementation decision or something else?


In reply to For vs. Foreach by mattford63

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, 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, 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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (4)
    As of 2021-01-22 04:00 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?