To elaborate on
holli's excellent comments, you need to reduce this to as simple a testcase as possible. Right now, there's too much code to work through - starting removing stuff until you remove the bug, add the thing you just removed, and that's where the bug is.
- In general, if you think something isn't in Perl, try it out, because it usually is. :-)
- "What is the sound of Perl? Is it not the sound of a wall that people have stopped banging their heads against?"