Is there some means where I can use something similar to qq(...) or q(...) to automatically escape the quotes and apostrophes? If you look at extended pattern modfiers like (?s) or /Q.../E, you can essentially enclose the pattern. I've never used something like /Q.../E for replacement, so I don't know if you can apply those modifiers to the replacement string. But I didn't see a modifier for quotes or apostrophes.

Essentially, I want the regex expression to look like this:

s~foo~qq(bar)q(bar)~g, which would be equivalent to s~foo~\"bar\"\'bar\'~g.

Re: [FishMonger] Is there a way to use qq() or q() with replacement in regex?
[In reply to]

Can't Post

That is correct, if I'm running it from a script.

i.e.

Code

#!/usr/local/bin/perl -i.bak

while(<>) { s~test~"bar"'bar'bar~g; print; }

But if I'm running in single-line edit mode, i.e.

Code

perl -pi.bak -e "s~bar~\"bar\"'bar'bar~g" test.txt

I need to escape the quotes, because it's enclosed with the expression after -e is enclosed in quotes. I suppose this is not a regex issue but a general Perl issue. In this case, I imagine I must escape quotes with slashes.