Subject: | each() and keys() do not work properly |
If you do
while (($key, $value) = each %h1) {
print "$key => $value\n"
}
you only get back one value. (at least that's what it does for me with Perl v5.6.1)
The problem is that this calls first the FIRSTKEY() which prepares the iteration hash and then FETCH(). The FETCH() calls _get_sources_list() which calls get_defaults_list() which calls _delete_iteration_hash() ... and the hash you dutifully created is gone.
I guess you should reconsider what functions should call the _delete_iteration_hash(). But it definitely should not be called by FETCH().
(Did you see Devel::TraceSubs? Handy module :-)
Another thing is ... I'm not sure it's wise to copy the whole data to the iteration hash. This will be a terrible waste of space if the data are bigger.
I created my own module (before I got notified about yours, but then I took some ideas) for hashes with defaults (+ case insensitivity). There I do it differently. I iterate over the actual hashes and only keep a hash with the number of times I seen a key. That hash will be much smaller.
This has another problem though. If someone iterates over any of the defaults it affects my own iteration.
I guess the best solution would be to create an iteration hash that would not contain the data, but only an idex of the hash in which to look for the data (-1 means the base). Then if the FIRSTKEY or NEXTKEY is called in scalar context we return just the key, in list we look up the value.
What do you think?
My module is at http://Jenda.Krynicky.cz/Beta together with a benchmark script if you want to look.
Jenda