Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Might a Monk have words to reduce my rage over the fact that '\\' single quotes don't interpolate:
DB<11> print '\\n'; \n DB<12> print '\n'; \n
<<'HEREDOC' does work, however.

Replies are listed 'Best First'.
Re: slashes in single-quoted string
by wind (Priest) on Apr 29, 2011 at 20:46 UTC

    I agree that it is at first glance, it appears odd that '\\foo' and '\foo' are equivalent. But this is a necessary exception if we want single quoted strings to be able to contain any character without special jury rigging.

    It's not that hard to remember that if you use q{}, that you can escape [\{}] in the string, or if you use '', you can escape [\']. The fact that heredocs don't require you to escape the 'HEREDOC' should be fairly obvious I believe.

    I'm therefore for maintaining the status quo, although I do appreciate the discussion.

Re: slashes in single-quoted string
by trwww (Priest) on Apr 29, 2011 at 21:34 UTC
    For me, its enough to have a personal rule that if a string contains ' (or " when I'm interpolating), I then use the q and qq operators. This way I never have to escape a character in a string.
      > This way I never have to escape a character in a string.

      DB<100> print q{\n} \n DB<101> print q{\\n} \n DB<102> print q{\\\n} \\n

      Cheers Rolf

Re: slashes in single-quoted string
by LanX (Saint) on Apr 29, 2011 at 21:13 UTC
    if '\'' means q{'} what do you expect '\\' to mean?

    Cheers Rolf

      I'm not sure what the OP wants to achieve with the post (simply express anger, or suggest this should be changed in an upcoming release(?)), but I guess the idea is that '\\' (or q{\\}) would mean two backslashes — i.e. you simply cannot escape the delimiter, which means you'd just have to choose a different delimiter (using q) in those cases.

      Typical pitfall with the current workings is that you tend to forget to escape the double backslashes in UNC paths, such as

      my $dir = q{\\\\dfsshare\foo\bar}; ^ ^
        IMHO it's impossible to disable "escaping" and allowing arbitrary character sequences.

        I mean by using an alternate delimiter like q{} or q// the problem is just postponed.

        DB<105> print q{\}} }

        Then the only possibility left would be concatenating with a constant representing the delimiter:

         eval( '$string=' . QUOTE . 'abc' . QUOTE );

        instead of  eval( '$string=\'abc\'' );

        Possible ... but desirable?

        Cheers Rolf

        UPDATE: or eval( '$string=' . q{'} . 'abc' . q{'} );

Re: slashes in single-quoted string
by Anonymous Monk on Apr 30, 2011 at 00:46 UTC
    Might a Monk have words to reduce my rage over the fact that '\\' single quotes don't interpolate:

    Get over it? If I understood better why this RFC causes the rage I might have different words :)

Re: slashes in single-quoted string
by Anonymous Monk on Apr 29, 2011 at 20:20 UTC
    err.. what I mean is that in perl 5, I am angry cause '\\' is '\'.