in reply to Clearing user defined variables
Aside from agreeing with Tachyon that if you must have a stack of globals then a hash is the way to go, I was wondering if you had used sub routines or not?
Accessing and modifying globals from within a subroutine rather than passing in parameters is (IMHO) a bad thing.
Additionally, have you looked at the number of variables you have with respect to them being configuration options or constants? If you find a stack of those then it may help with your clean up as you wouldn't need to clear those and can use the constant pragma or have a configuration file instead.
From your post, I am assuming that you have to clean up once the application has finished servicing a command. Returning to its wait state ready for another command at some point in the future. Perhaps you should look specifically at this area and consider your service code as something you can put into a module. Perhaps one module per command. You can then handle cleanup of each command specifically within each module yet have a smaller global cleanup in your main servicing loop.
You may find this easier to maintain - this is an approach I have used before and its saved me many headaches. Its a bit like a strategy pattern implementation (but how far is down to you).