in reply to Out of memory

You have implemented an "infinite-stack state machine". (: You have several cases of, you want to "do_this and then do_that" so you write code that says:

sub do_this { ... &do_that; }
which means that you won't return from do_this() until you have finished do_that() but do_that() does the same thing so each time you do something new you tie up a new slot on the stack for a subroutine that will never return.

You really need to replace at least one set of these calls with a loop.

It would also help to indent your code and read up on "here doc"s (in "perldoc perldata") for large blocks of text. And please use &mysub(); instead of &mysub; (see (tye)Re: A question of style for more on that).

        - tye (but my friends call me "Tye")