This is not quite what the OP asked.
How? The original post asked for a way to edit a file in place, which this is. This is not the same solution that the original post presented, because it does not entail reading the entire file into an array, then writing it back out. It allows for line by line editing, as well as giving an option for renaming the original. Even if this was the same solution as the original, it's a much more concise form of it, which means you could write a quick one-liner using this version, as opposed to writing out a whole script using the alternative.
-Bird