Does File::Copy do the right thing with regard to permissions and ownership on your platform?
If so,
Copy the original file to a tempfile.
Open and write to the tempfile.
Rename the tempfile to the original name.
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
"Science is about questioning the status quo. Questioning authority".
The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.