http://www.perlmonks.org?node_id=1043484


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

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 :)