in reply to reg perl substitution
There are several options, perhaps the easiest is to write to a temporary file first, and then move this to the location of the old file.
Or you could use Tie::File - that changes the file that it's tied to.