in reply to file updating question

If I just print to STDOUT it ALWAYS looks correct
Then why not just print to STDOUT?

You may just want to simplify your script.
... while(<>) { # do stuff print $whatever; }
Then you could simply run the script like so ...
$ myscript < inputfile > outputfile