schwern/perl5i — Issue #210: each() can go bad if the callback calls keys or values
Author: schwern
Created: 2011-11-09 01:24:30+00:00
State: open
Labels: bug, hard, yes do it

`%hash->each` can go into an infinite loop if `keys` or `values` is called on `%hash` inside the callback.

use perl5i::2;
use Test::More;

my %hash = (
foo => 23,
bar => 42,
baz => 99
);

my $i = 0;
note sprintf "This should run %d times", scalar keys %hash;
%hash->each(func($k,$v) {
pass();
keys %hash;
die "We're in an infinite loop" if $i++ > 10;
});

done_testing(3);

The only solution I can see is to have our `each` to do its own hash iteration. This likely requires XS.