Global variables (and module level variables) can lead to lots of problems. Use as little as possible to save you problems down the line.
Instead of using global variables, use parameters of subroutines and functions to pass values and variables around. The use of parameters seems tedious and verbose compared to the use of global variables. However, paramters explicitly exposes what a subroutine or function may affect, or what it may be affected by. This makes it easier to track down which part of your code changes which variable incorrectly.