in reply to file editing/writing

One, don't do that. Store your data in a separate file. Don't rewrite the program.

Two, if you must, see Inline::Files, the latest crazy thing from "the Damian". It has program rewriting capabilities as you desire.

-- Randal L. Schwartz, Perl hacker