Add the test before opening the output file:
#Open the output file. if (-e "$ARGV[0].out") { die "The output file already exists!"; } else { open(OUTPUT, ">$ARGV[0].out"); }
Opening the filehandle with the > at the beginning of the file name causes any existing file to be overwritten, so you must do the test first.