Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Single quoting weirdness

by alien_life_form (Pilgrim)
on Jun 07, 2002 at 08:59 UTC ( [id://172459]=perlquestion: print w/replies, xml ) Need Help??

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

Greetings,

while investigating a regexp related behavior I came to write the following:

perl -e "print join(qq(\n),('+','\+','\\+','\\\+')),qq(\n)" + \+ \+ \\+
What I felt the output should have been is:
+ \+ \\+ \\\+
Do my feelings need revision? and why?
Cheers,
alf
You can't have everything: where would you put it?

Replies are listed 'Best First'.
(MeowChow) Re: Single quoting weirdness
by MeowChow (Vicar) on Jun 07, 2002 at 09:01 UTC
    Yes: "\\" is a backslash-escape, which produces a single backslash. Without it, you'd have no way to get a backslash into an interpolated string, or onto the end of an uninterpolated string. It escapes in both types of strings.
       MeowChow                                   
                   s aamecha.s a..a\u$&owag.print
Re: Single quoting weirdness
by grinder (Bishop) on Jun 07, 2002 at 09:55 UTC

    It looks odd, but it is correct behaviour. \ is a meta-character for escaping, but in single quoted strings the only thing it escapes is ' (which allows you to embed a ' in a single quoted string without having to use the q{} syntax, or itself). Nothing else is quotable.

    So in the second line, \+, + is not magical, does not have to be quoted, so \ has no effect, is not consumed, and thus \+ is emitted.

    In the third line, \\+, \ escapes the following \, so \+ is emitted, and so on for the forth line.

    You might want to look at S_scan_str in toke.c for far more than you ever wanted to know about how perl scans strings.

    The most straightforward way of writing your code would be to double up each \ you want to print:

    perl -e "print join(qq(\n),('+','\\+','\\\\+','\\\\\\+')),qq(\n)" # or, more concisely perl -le "print $_ for qw/+ \\+ \\\\+ \\\\\\+ \\\\\\\\+/"


    print@_{sort keys %_},$/if%_=split//,'= & *a?b:e\f/h^h!j+n,o@o;r$s-t%t#u'
Re: Single quoting weirdness
by Abigail-II (Bishop) on Jun 07, 2002 at 10:58 UTC
    Inside a single quoted string, you get everything as is, with the exceptions of two things:
    1. A backslash followed by a delimiter gives just that delimiter.
    2. A backslash followed by a backslash gives just a single backslash.
    The reason for the first exception is hopefully obvious. To see why the second exception is needed, imagine that the second exception didn't exists, and try to figure out how you would make a single quoted string that ends in a backslash. You can't, because then you'd escape the delimiter....

    Abigail

      I have never found the reason for the first exception to be obvious.

      Perl has enough quoting mechanisms that I think it would be more valuable to have one that makes it easy to include a row of backslashes in than it is to have every one able to put out any possible text you could want.

      Heck, just the fact that the rules for single-quoting are so widely understood (as can be seen from people wondering at what it does) strongly suggests that something is wrong with the rule as it stands.

        Escaping the escape character seems to be used in about any escaping mechanism that I can recall. That's hardly a Perl specific thing. And not being about to escape the delimiter is just very awkward. Do you like stuff like:
        echo '$foo'"'"'$bar'
        just to be able to echo
        $foo'$bar
        like you have to do in many shells?

        Abigail

Re: Single quoting weirdness
by PodMaster (Abbot) on Jun 07, 2002 at 10:05 UTC
      Greetings,

      I have, now :) Thanks.
      Cheers,
      alf


      You can't have everything: where would you put it?
Re: Single quoting weirdness
by jmcnamara (Monsignor) on Jun 07, 2002 at 09:02 UTC

    The \\ is taken as an escaped backslash so in effect you only get one.

    --
    John.

Re: Single quoting weirdness
by demerphq (Chancellor) on Jun 07, 2002 at 13:27 UTC
    Well, there is one way that you can avoid the \\ rule in strings. Use the single quote variant of heredocs. as in the following
    print join("\n",split(/\s+/,<<'END_OF_TEXT'); + \+ \\+ \\\+ END_OF_TEXT
    which outputs
    +
    \+
    \\+
    \\\+
    
    Yes its annoying but single quoted here docs are the _only_ way (without using deep voodoo) to quote backslashes without escaping them.

    Updated in respect for Abigail-II

    Yves / DeMerphq
    ---
    Writing a good benchmark isnt as easy as it might look.

      The _only_ way? Hah. You don't know Perl (yet) ;-).

      If you don't like the way Perl interpolates strings, Perl let's you change them.

      Abigail

      #!/usr/bin/perl use strict; use warnings 'all'; use overload; BEGIN { overload::constant q => sub {$_ [$_ [2] && $_ [2] eq 'q' ? 0 : 1]} +; } print '+ \+ \\+ \\\+', "\n"; print "+ \+ \\+ \\\+", "\n"; __END__ + \+ \\+ \\\+ + + \+ \+

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://172459]
Approved by broquaint
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-25 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found