I'm struggling to work out how to successfully search and replace this string in the same file and save it off.
If you inserting or removing something from a file you have to rewrite all the file starting from the place where you're inserting or removing data. Therefore a practical approach is to read data from file, change it as needed, and save into a new file. Then rename new file to replace the original. You don't have to implement it yourself though, just use edit_file_lines function from the File::Slurp module.