I'm surprised nobody has mentioned it yet, but this is one of the aspects or programming that Objects have come to solve. Namely, this is solved by encapsulation. That's when you hide your data (those pesky global variables) inside the object, and then you attach methods to the object to handle the data, and it all happens in a nice, self-contained (encapsulated, you might say) way.
It's not hard to write your own little module implementing a class which wraps up the global variables and their associated methods. See perltoot for more info.