How perverse! Too bad they didn't limit it to 'foreach' and created the confusion.

From the Camel Book (4th Edition, 2012, page 142):

For historical reasons, the foreach keyword is a synonym for the for keyword, so you can use for and foreach interchangeably, whichever you think is more readable in a given situation. We tend to prefer for because we are lazy and because it is more readable, especially with the my. (Don’t worry—Perl can easily distinguish for (@ARGV) from for ($i=0; $i<$#ARGV; $i++) because the latter contains semicolons.)
And to think I stayed away from 'foreach' specifically because many books recommend doing so because it uses more memory (and I'm working with large arrays)...

I could be wrong, but I think this advice is outdated (if it was ever correct). Again from the Camel Book (page 143):

If LIST consists of assignable values (meaning variables, generally, not enumerated constants), you can modify each of those variables by modifying VAR inside the loop. That’s because the loop variable becomes an implicit alias for each item in the list that you’re looping over.

If I’m reading that correctly, no copying is needed, so there’s no reason a foreach-style loop would use more memory than a C-style for loop.

And there are significant benefits. See Foreach Loops in perlsyn, where a C-style for loop is rewritten as a foreach-style loop in idiomatic Perl, and the latter is stated to be “cleaner, safer, and faster.” And the Camel Book has this in its cosideration of “Time Efficiency” (pages 691–2):

Avoid subscripting when a foreach or list operator will do.... There’s often a better way to do it. Consider using foreach, shift, and splice operations.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re^3: Why/how are these different? by Athanasius
in thread Why/how are these different? by perl-diddler

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.