You have now substituted within the $data variable. Next you need to write the contents of $data back to the file. Try adding something like this after the substitution
# untested
seek $fh, 0, 0; # rewind to the start of the file
print $fh $data; # Write the entire file
close $fh;
Of course this may not be very efficient if you have a large file to alter, but then inserting into the middle of a very big file never is. If you are frequently updating random records in a file you may need to start thinking about using a database.
Cheers,
R.
Pereant, qui ante nos nostra dixerunt!