As far as I can tell, you only ever look at the first element of the jobs array;
No, first job can return 'wait' and loop will jump next job after this line
return 'wait' unless --$maxcnt;
not to mention getting rid of any need to flirt with array modification inside a loop
but it might not stay that way if your logic changes in the future (it does create a bit of a trap for the unwary)

Well, it's unlikelly going to change the way that loop will continue after do_finish(). My do_finish() sub is something that called before finish.

Also, let's imagine that I copied my @{$self->{jobs_a}} to temporary variable @tmp_jobs. Will it solve the problem? No. If I change the code so loop continues after do_finish(), it will still introduce the bug (however a bit more visible).

I think unit tests with 100% branch coverage would be a solution here. (+ effort to keep it 100% after any modifications)

Let me/us know which way you end up going
I don't know yet. Certainly, there are ways to make it bit better. But I actually think it's irrelevant to this topic :)

In reply to Re^6: foreach argument modification inside loop, followed by return from loop by vsespb
in thread foreach argument modification inside loop, followed by return from loop by vsespb

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.