[kw-pm] but i just put in a "print" statement. really!

Robert P. J. Day rpjday at mindspring.com
Wed Jun 22 04:46:09 PDT 2005

  ok, this is the same thing you'd normally never believe but, i
swear, adding a simple "print" statement to a perl module for
debugging purposes suddenly made the program work.

  i have a hash with entries of the form:

	key => (reference to value hash with numerous fields)

and my subroutine accepts nothing more than a reference to the
top-level hash, and a string to search for as the value for one of the
fields in the hash that's the value corresponding to that key.
really, it's just that simple -- a "while each" loop through the hash,
for each key, get the reference to the value hash, and dereference
that to get to the field in question, then a string compare.

  however, after i initially loaded the hash and verified a number of
entries, i started searching.  the first time i looked for a
particular value, the search routine found it.

  the *second* time, however, it failed to find it, and a dump of the
hash showed, sure enough, no such entry.  whaaaa?  after much
debugging and head scratching, since the hash was fairly sizable, i
added a simple "print" statement to the top of the search routine to
print the number of keys in the hash, to see if suddenly there were
unexpectedly fewer keys, so i added the following:

  print "Currently, hash has " . (scalar keys %$href) . " elements.\n" ;

  lo and behold, suddenly the program starts to work properly.  what
the hell?  the only imaginable side effect is just taking the keys of
the hash, so i replaced that statement with:

  my @keys = keys %$href ;

and that *also* solved the problem.  so it appears that there's some
kind of side effect associated with just taking the keys of a hash.  i
don't know what else to think.

  i didn't write the rest of the surrounding code, so is it possible
that, elsewhere, a hash entry was deleted and taking the keys somehow
"cleans up" or reorganizes the hash.

  i know very well that you shouldn't mess with the keys of a hash
while iterating through it, but i don't (at first glance) see that
happening anywhere.  i'm just puzzled about what might be happening.



More information about the kw-pm mailing list