in reply to Modifying a file using RegEx

Your code isn't modifying the file because it's not writing to it; the substitution only affects the string in memory. That it has been read from a file is irrelevant at that time.

The easiest way to accomplish what you want is to use perl's -i switch; see perlrun for more.