Note that there are some explanatory texts on larger screens.

plurals
  1. POPreserving backslashes in Perl strings
    primarykey
    data
    text
    <p>Is there a way in Perl to preserve and print all backslashes in a string variable? For example:</p> <pre><code>$str = 'a\\b'; </code></pre> <p>The output is</p> <pre><code>a\b </code></pre> <p>but I need</p> <pre><code>a\\b </code></pre> <p>The problem is can't process the string in any way to escape the backslashes because I have to read complex regular expressions from a database and don't know in which combination and number they appear and have to print them exactly as they are on a web page.</p> <p>I tried with template toolkit and html and html_entity filters. The only way it works so far is to use a single quoted here document:</p> <pre><code>print &lt;&lt;'XYZ'; a\\b XYZ </code></pre> <p>But then I can't interpolate variables which makes this solution useless.</p> <p>I tried to write a string to a web page, into file and on the shell, but no luck, always one backslash disappears. Maybe I am totally on the wrong track, but what is the correct way to print complex regular expressions including backslashes in all combinations and numbers without any changes?</p> <p>In other words: I have a database containing hundreds of regular expressions as string data. I want to read them with perl and print them on a web page exatly as they are in the database. There are all the time changes to these regular expressions by many administrators so I don't know in advance how and what to escape. A typical example would look like this: <code>'C:\\test\\file \S+'</code> but it could change the next day to <code>'\S+ C:\\test\\file'</code> Maybe a correct conclusion would be to escape every backslash exactly one time no matter in which combination and in which number it appears? This would mean it works to double them up. Then the problem isn't as big as I feared. I tested it on the bash and it works with two and even three backslashes in a row (4 backslaches print 2 ones and 6 backslashes print 3 ones).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload