in reply to misbehaving program

put
use strict; use warnings;
at the top of your script, then go through and declare all your variables as your script will no longer compile as is. Then sort out your indenting and see if you can spot your errors.

Also try changing system for backticks so you can log any errors, or at least check the return code from system ($!).

perldoc -f system