Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>For Perl single character <a href="http://perldoc.perl.org/perlrebackslash.html" rel="nofollow">backslash escapes</a>, you can do this safely using a two character <code>eval</code> as part of the substitution. You need to put in the characters that are acceptable to interpret in the character class after the <code>\</code>, and then the single character after is <code>eval</code>'d and inserted into the string. </p> <p>Consider:</p> <pre><code>#!/usr/bin/perl use warnings; use strict; print "\n\n\n\n"; while (my $data = &lt;DATA&gt;) { $data=~s/\\([rnt'"\\])/"qq|\\$1|"/gee; print $data; } __DATA__ Hello!\nI\'d like to tell you a little \"secret\". A backslask:\\ Tab'\t'stop line 1\rline 2 (on Unix, "line 1" will get overwritten) line 3\\nline 4 (should result in "line 3\\nline 4") line 5\r\nline 6 </code></pre> <p>Output:</p> <pre><code>Hello! I'd like to tell you a little "secret". A backslask:\ Tab' 'stop line 2 (on Unix, "line 1" will get overwritten) line 3\nline 4 (should result in "line 3\nline 4") line 5 line 6 </code></pre> <p>The line <code>s/\\([rnt'"\\])/"qq|\\$1|"/gee</code> does the work. </p> <ul> <li><p>The <code>\\([rnt'"\\])</code> has the acceptable characters to eval inside the braces.</p></li> <li><p>The <code>gee</code> part does a double eval on the replacement string. </p></li> <li><p>The <code>"qq|\\$1|"</code> part is eval'd twice. The first <code>eval</code> replaces <code>$1</code> into the string, and the second performs the interpolation. </p></li> </ul> <p>I cannot think of a two character combination here that would be a security breach...</p> <p>This method does <em>not</em> deal with the following properly:</p> <ul> <li><p>Quoted strings. For example, Perl would not unescape the string 'line 1\nline 2' because of the single quotes.</p></li> <li><p>Escapes sequences that are longer than a single character, such as hex <code>\x1b</code> or Unicode such as <code>\N{U+...}</code> or control sequences such as <code>\cD</code></p></li> <li><p>Anchored escapes, such as \LMAKE LOWER CASE\E or \Umake upper case\E</p></li> </ul> <p>If you want more complete escape replacement, you can use this regex:</p> <pre><code>#!/usr/bin/perl use warnings; use strict; print "\n\n\n\n"; binmode STDOUT, ":utf8"; while (my $data = &lt;DATA&gt;) { $data=~s/\\( (?:[arnt'"\\]) | # Single char escapes (?:[ul].) | # uc or lc next char (?:x[0-9a-fA-F]{2}) | # 2 digit hex escape (?:x\{[0-9a-fA-F]+\}) | # more than 2 digit hex (?:\d{2,3}) | # octal (?:N\{U\+[0-9a-fA-F]{2,4}\}) # unicode by hex )/"qq|\\$1|"/geex; print $data; } __DATA__ Hello!\nI\'d like to tell you a little \"secret\". Here is octal: \120 Here is UNICODE: \N{U+0041} and \N{U+41} and \N{U+263D} Here is a little hex:\x50 \x5fa \x{5fa} \x{263B} lower case next char \lU \lA upper case next char \ua \uu A backslask:\\ Tab'\t'stop line 1\rline 2 (on Unix, "line 1" will get overwritten) line 3\\nline 4 (should result in "line 3\\nline 4") line 5\r\nline 6 </code></pre> <p>That handles all Perl <a href="http://perldoc.perl.org/perlrebackslash.html" rel="nofollow">escapes</a> except: </p> <ol> <li><p>Anchored type (\Q, \U, \L ended by \E)</p></li> <li><p>Quoted forms, such as <code>'don't \n escape in single quotes'</code> or <code>[not \n in here]</code></p></li> <li><p>named unicode characters, such as <code>\N{THAI CHARACTER SO SO}</code></p></li> <li><p>Control characters like <code>\cD</code> (that is easily added...)</p></li> </ol> <p>But that was not part of your question as I understood it...</p>
 

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