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
| [reply] |
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 | [reply] |
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.
| [reply] [d/l] [select] |
I don't understand your last "should" example. But I agree that it makes sense that the reason you need to escape would follow the closing delimiter. So in qq/blab/ you would need to escape out any contained / character, but not any ' character.
| [reply] |