slicks1 slicks1 - 3 months ago 8
Perl Question

Escaping double quotes fix

I cannot find the resources for this. I want to escape double quotes with perl and i am new to this language.

Here's what I have:

$comment =~ s/\"/\\\"/g;


It doesn't seem to be working. What is a proper solution?

Answer

One way, to avoid counting backslashes

$comment =~ s/"/q(\")/eg;

The /e modifier evaluates the replacement part, where you force a literal string by q().

Note though that in this simple case s/"/\"/ works fine. There is probably more going on in your code, or your quotes come already escaped in which case extra \ affect what you see later.

Comments