in reply to Extracting data from file and modifying without making change in file.
If you don't want to modify $wholefile with a substitution,
$wholefile_copy = $wholefile; and run substitution on the copy. Not trying to be snarky, but unless the file is humongous, the memory usage won't matter.