I suspect from your question that you are having problems with overwriting the value of your variables. If so then what you need to do is stop using so many global variables. I strongly recommend reading Coping With Scoping to understand how to avoid having variables trample all over each other, and then strict.pm to find out how to let Perl give you feedback to avoid mistakes.