Consider using
log4perl to help solve your problem.... That way, you can have debug statements everywhere in your program, but only write it to the log when you need it, which might narrow down where the bug is... (Excuse english, not enough coffee yet...)
----
Zak
"There is no room in this country for hyphenated Americanism" ~ Theodore Roosevelt (1915)