While
tadman gives a perfectly fine solution, I'd pick a nit with certain of his advice:
Although using local will solve your problem, I'd stay away from it.
I'd really suggest steering away from using local declared variables.
That is really not good advice.
local does not "declare" a variable, it localizes its value. This is a critically important capability -- as
young_david's problem clearly demonstrates.
In many cases we don't have to worry about variables being localized, because the ops we use do it implicitly. (I'm thinking specifically of
foreach.) But others, such as
while, do not, and so we have to localize the variable explicitly --
if we want to be safe.
So rather than say "Don't use
local", I'd say "
use strict."
That is a practice which will encourage good programming habits.
jdporter
The 6th Rule of Perl Club is -- There is no Rule #6.