Isn't this something to do with the way you are opening the file, as in clobbering existing content ready for rewriting.
You have used;
open(FILE, ">$file");
IF you want to update without erasing it first try;
open(FILE, "+>$file");
Have a good look at perlopen.