Corresponding Perl string literal for Python r prefix "text"?

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

How can you replace the equivalent Python r prefix in Perl string literals?

+5
source share
1 answer

The uninterpreted quote operator q{} takes you most of the way. There are only a few subtle differences.

  • delimiters

    There are four delimiters that are used with Python string literals:

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

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

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

    The backslash character \ always interpreted as the literal backslash character in Python literal string strings. It will also skip the character, which is otherwise interpreted as a trailing line separator.

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

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

    • The backslash precedes the delimiter character

       q<18 \> 17 \< 19> => 18 > 17 < 19 q#We're \#1# => We're #1 
    • A backslash precedes another backslash character. In this case, the sequence of two backslash characters is interpreted as one 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: the one-shot doc here.

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

This creates the string '"\\fooNEWLINE .

(So, is it possible to create one string string literal in Python that contains both ''' and """ ?)

+6
source

All Articles