As others have suggested: Always use strictures (use strict; use warnings; - see The strictures, according to Seuss)!
You seem to be writing Perl circa 4.x rather than 5.x. There were many enhancements made to Perl between 4.x and 5.x with most of them improving robustness of code in various ways (using strictures is an example of that). A few things we do differently now:
- Use chomp instead of chop. chop simply removes the last character - whatever it was. chomp removes the a trailing copy of the contents of $/ (the input record separator special variable), normally a \n.
- You don't need to initialise variables - Perl always does that by setting them to undef.
- Don't call subs using &. That has a special meaning and will most often result in surprises - surprises are generally bad.
- Don't use global variables. In small programs it's not such an issue, but as programs grow global variables make it very difficult to figure out how things work.
True laziness is hard work