9

This thread is about Python's string literal r, while this is about Perl's one here. However, I am not sure if there exists any equivalent command in Perl for Python's prefix r.

How can you replace equivalently Python's prefix r in Perl's string literals?

Community
  • 1
  • 1
Léo Léopold Hertz 준영
  • 134,464
  • 179
  • 445
  • 697
  • 1
    Well if you want a _command_, you can use `q` and `qq` to construct string literals in Perl. See http://perldoc.perl.org/perlop.html#Quote-and-Quote-like-Operators – simbabque Jul 02 '15 at 12:10
  • 4
    `\ ` is special in both `q` and `qq`, though, so there's no direct correspondence to Python's `r`. – choroba Jul 02 '15 at 12:12

1 Answers1

9

The uninterpolated quote operator q{} will get you most of the way there. There are only a couple of subtle differences.

  1. delimiters

    There are four delimiters used with Python raw string literals:

    • r"..." and r'...'
    • r"""...""" and r'''...'''

    The q... operator in Perl has a greater variety of delimiters

    • Paired delimiters q(...), q[...], q<...>, q{...}
    • Regular delimiters can be any other non-alphanumeric, 7-bit ASCII char: q"...", q/.../, q#...#, etc.
  2. the backslash

    The backslash character \ is always interpreted as a literal backslash character in Python raw literal strings. It will also escape a character that would otherwise be interpreted as the closing delimiter of the string

    r'Can\'t believe it\'s not butter'   =>   Can\'t believe it\'s not butter
    

    The backslash character \ in a Perl q{...} expression is also a literal backslash with two exceptions:

    • The backslash precedes a delimiter character

      q<18 \> 17 \< 19>     =>   18 > 17 < 19
      q#We're \#1#          =>   We're #1
      
    • The backslash precedes another backslash character. In this case the sequence of two backslash characters is interpreted as a single backslash.

      q[single \ backslash]           =>  single \ backslash
      q[also a single \\ backslash]   =>  single \ backslash
      q[double \\\ backslash]         =>  double \\ backslash
      q[double \\\\ backslash]        =>  double \\ backslash
      

There is one way to create a truly "raw" string in Perl: A single-quoted here-doc.

my $string = <<'EOS';
'"\\foo
EOS 

This creates the string '"\\fooNEWLINE.

(So is it possible to create a single raw string literal in Python that contains both ''' and """?)

ikegami
  • 367,544
  • 15
  • 269
  • 518
mob
  • 117,087
  • 18
  • 149
  • 283
  • 2
    Added the truest raw Perl has. – ikegami Jul 02 '15 at 15:56
  • 2
    heredoc strings always end in a newline – mob Jul 02 '15 at 15:57
  • Yup. Because there's actually a newline there. – ikegami Jul 02 '15 at 15:58
  • Is there any sense of escaping all paths? Normally, I do it in Python by `r` prefix so probably the `q(...)` is the corresponding one. I just noticed that I had backflashes in some PATHS which must be escaped in prompt but not in Perl. Etc `my $file = q(/Users/masi/7-8:2015/). $fileID. q(.raw);` should work. While `my $file = q(/Users/masi/7-8\:2015/). $fileID. q(.raw);` is not working because of the backflash. Priting `$file` gives also the backflash which is just escaping there a thing. – Léo Léopold Hertz 준영 Jul 03 '15 at 11:32