You're using an undocumented side-effect of an optimization in some foreach loops. (See an earlier
post of mine on different kinds of
foreach loops.) There's no guarantee it will continue working in future versions of Perl. Why don't you use a processing queue instead?
my @todo = ( ... );
while (@todo) {
my $item = shift @todo;
...
push @todo, ...
...
}