It is not generally safe to add or remove entries of a hash while iterating it using each
. If you want to do so, it might be best to save an explicit list of keys you want to iterate to an array, and loop over that.
Instead of
while (my $k, $v) = each %hash) {
$hash{"foo_$k"} = $v;
}
do
my @keys = keys %hash;
for my $k (@keys) {
my $v = $hash{$k};
$hash{"foo_$k"} = $v;
}