my %s; @s{split //, '<>&'} = qw/lt gt amp/; my $text = do { local $/; }; $text =~ s/([<>&])/"&$s{$1};"/ges;