in reply to Can someone tell me why this hangs??

There are a couple of ways to debug this kind of problem. You can alter the code by littering the landscape with print statements. This has the advantage of making you look at the code to determine where you should be dropping your "DEBUG Reached line NNN, variable xxx=$xxx<<<<\n" statements. Quite often when I use this style of debugging I would run across my logic error in the process. Then I had to go back and remove my previous debugging statements. When I initially was introduced to Perl, some mumble years ago, that is how I did debugging.

After a few months I discovered the Perl Debugger --  perl -d; I have not gone back to print statements, except as a last resort (and usually in CGI coding).

I step through the code until the code takes off in a different direction from what I expected. I look at the variables involved in the control construct that surprised me. I track back to where the erroneous value appeared, fix my bug and go on.

Whether one method or the other is 'better' is not something that I can really say. I get a better feel for the logic by watching the way the Program flows as I step from point to point in the execution. I know other programmers who can parse the logic-flow as they read the code, and decide on where they want to insert debugging statements based on their mental map of the logic. It all depends on what you are comfortable with.

----
I Go Back to Sleep, Now.

OGB