Yes, I glossed over the output management part, thinking the OP probably had a handle on that already (unfortunate pun duly noted).
As often as not for things like this, I just make the script print to STDOUT and put the file handling stuff on the command line -- why right write more lines of perl code to do stuff that the shell can do for me with an angle bracket?
text-altering-script.perl input.file > output.file
# optional step:
mv output.file input.file
|