my $TestText; { local $/; open( my $in, '<', "Test.txt" ) or die "cannot open Test.txt $!"; $TestText = <$in>; close $in; } $TestText =~ s/\Q$_\E/$hash{$_}/g for keys %hash; open( my $out, '>', "TestFinal.txt" ) or die "cannot create TestFinal $!"; print $out $TestText; close $out;