The quotemeta, which implements Q
, absolutely does what you ask for
all ASCII characters not matching /[A-Za-z_0-9]/
will be preceded by a backslash
Since this is presumably in a shell script, the problem is really of how and when shell variables get interpolated and so what the Perl program ends up seeing.
The best way is to avoid working out that interpolation mess and instead properly pass those shell variables to the Perl one-liner. This can be done in several ways; see this post for details.
Either pass the shell variables simply as arguments
#!/bin/bash
# define $target
perl -pe"BEGIN { $patt = shift }; s{Q$patt}{$replacement}g" "$target" file.txt
where the needed arguments are removed from @ARGV
and utilized in a BEGIN
block, so before the runtime; then file.txt
gets processed. There is no need for E
in the regex here.
Or, use the -s
switch, which enables command-line switches for the program
# define $target, etc
perl -s -pe"s{Q$patt}{$replacement}g" -- -patt="$target" file.txt
The --
is needed to mark the start of arguments, and switches must come before filenames.
Finally, you can also export the shell variables, which can then be used in the Perl script via %ENV
; but in general I'd rather recommend either of the above two approaches.
A full example
#!/bin/bash
# Last modified: 2019 Jan 06 (22:15)
target="/{"
replacement="&"
echo "Replace $target with $replacement"
perl -wE'
BEGIN { $p = shift; $r = shift };
$_=q(ah/{yes); s/Q$p/$r/; say
' "$target" "$replacement"
This prints
Replace /{ with &
ah&yes
where I've used characters mentioned in a comment.
The other way
#!/bin/bash
# Last modified: 2019 Jan 06 (22:05)
target="/{"
replacement="&"
echo "Replace $target with $replacement"
perl -s -wE'$_ = q(ah/{yes); s/Q$patt/$repl/; say'
-- -patt="$target" -repl="$replacement"
where code is broken over lines for readability here (and thus needs the
). Same printout.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…