Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
By the code working I expect that the for loops over the entire array contents including the elements added in the loop body. The expected output of this code is
abcdef
Which is in fact what I get when I run it. By safe I mean it doesn't crash or do something else unexpected when I run it. I added use strict and use warnings and the code still worked fine. No warnings emitted, no strictness violations. I'm not sure what kind of sanity checks you might add to this code. I suppose you could check at the end of the loop that the array had exactly the expected six elements in it.

It is clear to me that this technique used incorrectly could lead to creation of an infinite loop, but that is just a programmer/algorithmic GIGO type issue.

Yes, you can argue that it is unsafe because a user could accidentally write their algorithm in such a way that some data causes it too loop infinitely. But that's not the kind of safe I'm asking about. By safe I mean this. If I write a piece of code using this pattern and it works as I expected in this version of perl, is it likely to break unexpectedly when I run the same code on a different version of perl, because the fact of it working as expected depended on an implementation detail of the interpreter rather than a supported feature of the interpreter.

Or to restate the original question. The following behavior works in Perl 5.10.0 (linux/multi threaded): a for loop using an array as the sole element of its loop list can loop over the entire array even if that array is extended inside the loop body -- so the definition of 'entire' changes as the loop is executed and the array gets longer. Is this behavior a supported feature of the language which can be relied upon to work generally across future versions of perl or is it an implementation detail subject to unexpected and undocumented change?

--DrWhy

"If God had meant for us to think for ourselves he would have given us brains. Oh, wait..."


In reply to Re^2: Is it safe to append to the array you are iterating over by DrWhy
in thread Is it safe to append to the array you are iterating over by DrWhy

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!
  • 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
  • Outside of code tags, you may need to use entities for some characters:
            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 contemplating the Monastery: (11)
    As of 2014-07-11 02:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      When choosing user names for websites, I prefer to use:








      Results (217 votes), past polls