each
could be fine, because the loop body is not making any calls to other code that could be messing with the hash’s iterator. He just has to reset the iterator every time he wants to start over:
sub test_it {
my $entry = shift;
keys %regex; # reset iterator
while( my($type, $regex) = each %regex) {
return 1 if $entry =~ /$regex/;
}
return 0;
}
Of course since he doesn’t use the keys in this example, it’s a pointless exercise to use each
rather than values
. (Well, if %regex
is expected to grow huge then each
will take much less memory than making a list of all the values
, and would be faster too if a match is found quickly.)