in reply to Substitution in text files

you need not
$message =~ s/\$subKey/$subVar/o;
but rather
my $qkey = quotemeta $key; $message =~ s/$qkey/$recorsData{$key}/;
but your code snippet and your question gives me an idea that you're completely lost. Where's reading of a file after local($/)=undef;? (which is easier to write as just local $/;) where's file writing?

To say, to answer a question you should know 50% of answer...

Give us more details and your problem will be easier to advice...