Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Ouch! Each! Reentrant it is not

by adrianh (Chancellor)
on Jul 15, 2005 at 13:47 UTC ( #475222=note: print w/replies, xml ) Need Help??


in reply to Ouch! Each! Reentrant it is not

Solution: Either go through the whole hash, or rewind it after using each:

I once worked on a largish codebase that had this as the cause of several bugs in various places. We ended up using variations of things like:

sub foreach_pair (&\%) { my ($coderef, $hashref) = @_; local ($a, $b); keys %$hashref; while ( ($a, $b) = each %$hashref ) { $coderef->($a, $b) }; }; my %foo = (a => 1, b => 2, c => 3); # if you don't mind remembering what $a and $b are foreach_pair { print "$a == $b\n" } %foo; # or if you prefer being explicit foreach_pair { my ($name, $value) = @_; print "name $name is $value\n" + } %foo;

To avoid the problem and make things a bit more explict.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://475222]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (4)
As of 2020-06-01 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?