each - iterator clash

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I have deduced (by elimination) that I have some
iterator stomping going on. The documentation
for each says that:

    There is a single iterator for each hash,
    shared by all "each", "keys", and "values"
    function calls in the program; it can be reset by
    reading all the elements from the hash, or by evalu-
    ating "keys HASH" or "values HASH".

I have an "each" loop that terminates early,
and I suspect that a nested each/keys/values
call is responsible.

I deduce this because if the outer "each" loop
is replaced by iterating over the array of keys
returned by a "keys" loop, all is well.

So - is there any convenient way to satisfy my curiosity
and FIND the nested keys/values/each call
doing the damage?

I thought I understood my code (which *is*
quite large), and do not understand why such a nested
call would be present.


Re: each - iterator clash

Quoted text here. Click to load it

Please post a minimal sample program that exhibits the issue you


Re: each - iterator clash

Jürgen Exner wrote:
Quoted text here. Click to load it

If I could get it small enough to do that,
I wouldn't need to post.

My requirment here is to find a fault
in a large and complex context.

I was hoping that there might be (and from
Ben's post, there *are*) techniques that could
help me.


Re: each - iterator clash

Quoted text here. Click to load it

You could try tying the hash to a class derived from Tie::StdHash (in
the Tie::Hash module) with an overriden FIRSTKEY method that logs where
it was called from. I don't know whether this counts as 'convenient' :).
You could also try running a debugging build of perl under a (C)
debugger and setting a breakpoint on Perl_hv_iterinit.


Re: each - iterator clash

Ben Morrow wrote:
Quoted text here. Click to load it

The problem (as it happened) was provoked by a single
"class", so it was easy to tie:: into the hash-of-the-class
as you suggested.

With the additional logging, the problem was (fairly...)
quickly located.

It turned out to be the v2.121 version of Data::Dumper, specifically
in Sortkeys.


(and in case you're wondering, yes, the Solaris machine
in question has an Old perl on it)

Thanks for your help.


Site Timeline