my $pat = quotemeta($delim); s/$pat/\\$delim/g; #### s/\Q$delim\E/\\$delim/g; #### s/\Q$delim/\\$delim/g; # \E is optional at the end of the literal.