in reply to sub routine trouble
What everyone else said AND
Use my to declare all variables and start your script with
use strict; [download]
It looks like all your variables are used, but you never know until you add use strict;