in reply to file updating question

There is one error in your code: in your print statement, there should be no comma after file handler.

Also, if the new content you write out is a "shrinked version" of the old content, for sure, you will see some of the old content at the end of your file. The write is a kind of "in-place" overwrite, whatever not being overwritten from the old content will remain there. Don't expect it will clean up the file first, and then write out. Especially if your file record is variable-length, you will see broken records.

You can also read perlrun document for other approaches, and read that big chuck under -i option.