I could have posted the entire example, but it would have been much too long. I am using strict, and the code I posted does in fact show pretty much exactly what is going on.
The best way to get help here is to post a self-contained program which exhibits your problem. If we can run your program, then it's far more likely that we'll be able to find the issue.
I find that in the process of cutting a program down to an example that illustrates a particular problem, it often gives me a different perspective and enables me to find a solution without even posting here.