in reply to If I Want Backslashes, I'll Ask For Backslashes

A single quote form will mostly ignore backslashes, except for \\ and \'.

The single-quote form of a HERE document works exactly the same way.

Here is a common way of dealing with your issue: use some other character in your code. At the final step, (or next to the final step, as the case may be) turn that into a backslash. The other char is not special to the other layers, so it doesn't need more and more stacking.

  • Comment on Re: If I Want Backslashes, I'll Ask For Backslashes

Replies are listed 'Best First'.
Re^2: If I Want Backslashes, I'll Ask For Backslashes
by tadman (Prior) on Aug 11, 2001 at 00:33 UTC
    If that's the case, I can understand \', but what's the rationale behind \\? Seems to me to be a little unusual, which is the source of this question.

      Well, you've gotta have \\... how else do you write the escape character? Without \\, '\' is a syntax error, so you need some way of getting the escape character into the string - and doubling it seems to be common convention. However, in the single-quoted here doc, it becomes unnecessary as the only thing that could possibly need escaping is the ending string - and, um... well, if you need to escape that, then I suggest picking a better end string. :-)

      Update: Well, John's right - but the need does exist to include the quoting character inside the string, and if the escape character is at the end of the string, there needs to be some way to do that without causing the interpreter to freak out.

      His Royal Cheeziness

      Not quite what Cheezy said... I understand it this way. If \ is normally ignored, and only\'is special (meaning '), how do you really write \'? That's the only time you must write \\ when you meant \. But, since it exists, you need to use it also to write \\ (as \\\\) etc. Other than that, you don't need to write \ as \\ in a single-quote, but you could.

      —John

        Here's my, possibly skewed, reasoning as to how it should work, at least in one sense:
        $_ = '\\something'; # \\something $_ = 'It\'s a tragedy'; # It's a tragedy $_ = '\\''; # \'
        Of course, the last example would probably strike many a C programmer as being far too bizarre to comprehend.

        This is why I expected the q() operator to behave differently. As it was pointed out, the only reason for \\ is because of the requirement for \', which would imply that if \' were no longer an issue, \\ would cease to be relevant as well.

        Here's what I would expect, and yet this is not the case:
        $_ = q[\\something]; # \\something $_ = q[It's a tragedy]; # It's a tragedy $_ = q[\']; # \'
        Update:
        The last "should" example is parsed as such:
        ',\,\','
        That is to say, that this is really a single quote, followed by a single backslash, followed by the token \', followed by a single quote.