Because you're modifying the array you're iterating over which is a no-no. See perlsyn for the details, but the cliffs notes is use while if you're modifying the array in place just as you've discovered.
Update: OK, it's not really extensive details; here's the warning in its entirety:
If any part of LIST is an array, foreach will get very confused if you add or remove elements within the loop body, for example with splice. So don't do that.
In reply to Re: 'For' loop bails out early?
by Fletch
in thread 'For' loop bails out early?
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |