in reply to Localizable / customizable 'each' iterator for hashes

This looks wrong to me:
sub FIRSTKEY { ... $iterator = 0 unless defined $iterator;
Tied hash iterators have a strange division of responsibility: perl's actual iterator on the tied HV is responsible for tracking whether FIRSTKEY or NEXTKEY should be called, and the tied class is responsible for determining which key should be returned next. This allows things like void context keys() or values() to reset the iterator without actually calling any tie functions. So FIRSTKEY should always start at the beginning.

Replies are listed 'Best First'.
Re^2: Localizable / customizable 'each' iterator for hashes
by davido (Cardinal) on Jul 20, 2004 at 00:38 UTC
    The idea is that if the iterator already has a value, I don't want to change that value automatically blindly. I'm allowing the iterator to be pre-set prior to the first call to FIRSTKEY. The only time I want to set the iterator to zero, is if it has already reached the last key (I do this check elsewhere), or if it has not yet been set at all. Try it out, it works. :)

    By the way, I should mention that this class is pretty much unneeded. Anytime the functionality of this type of class could be needed, it could be accomplished more easily by creating an external keys list with keys, and iterating over that list. I really just wanted to see if I could figure out how to make it work internally via each. It works, but it's ugly and unwieldy.


    Dave

      Sorry, I should have taken time to try it (which I still have not yet done :).