in reply to Single quoting weirdness

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

Replies are listed 'Best First'.
Re: Re: Single quoting weirdness
by Anonymous Monk on Jun 07, 2002 at 11:52 UTC
    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

        Do I like that? Nope. But if shells were like Perl, then I could do either of the following:
        echo q($foo'$bar) echo "\$foo'\$bar"
        and I find the former in particular to be very convenient. If there was only one quote delimiter to choose from, well that is a pain. But with the range of choices in Perl, it is pretty rare to run across a single-quote situation that wouldn't work easily.

        OTOH there is no syntax in Perl that gives you a row of backslashes easily. And given the way the rest of Unix works, I often want exactly that (just to pass a single backslash through several layers of interpolation - each of which loses half of them).