Its always better to use strict before u start a perl program. Most of the times the problem comes because we are not clear of the scoping of the perl variables. Better to use strict.
In your case using strict will solve all the problems that you have.