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

The regular expression has a mechanism for specifying which delimiter you would prefer, such that the regular slash character can be used within it to no ill-effect.

I am curious if there was a similar mechanism for quotation, such that backslashes are not special in anyway way. I expected qq to operate this way, since it is quite like a regular expression in terms of style, but in fact, it does not even come close:
$foo = qq[\\something]; #1 $foo = qq!\\something!; #2
I'd expect that in either case, the quotations are not special. Even within here-documents, the same problem persists, with backslashes having special meaning:
$foo = <<END; \\something END
However, by some curious twist, this works as I would like:
$foo = <<'END'; \\something END
Which seems to be an internal inconsistency with respect to the way the functionality of the single quote is portrayed.

There is the "expanding backslash" problem which I have been hit with in the past, where by every iteration of some program you require more and more backslashes to get the job done. One case was a YACC program that wrote a C program which wrote some Perl, which in turn wrote some JavaScript. At the very beginning you needed something like eight backslashes to put in a single one in the final product. Of course, if you miscounted, you could break the thing horribly with only 7 or an extra one.

Replies are listed 'Best First'.
Re: If I Want Backslashes, I'll Ask For Backslashes
by chipmunk (Parson) on Aug 10, 2001 at 06:18 UTC
    There is no way to specify an alternate escaping character in Perl. Except perhaps by hacking the core. :)

    Backslashes are not special inside a single-quoted here-doc because there is nothing you would need to escape. Whereas in a regular single-quoted string, you might need to escape single quotes (or whatever delimiter you are using).

      Yea there is: ask Damian.

      Seriosly, use a source filter modeule.

Re: If I Want Backslashes, I'll Ask For Backslashes
by busunsl (Vicar) on Aug 10, 2001 at 10:27 UTC
    Since qq[] is the same as "", everything in it will be interpolated.
    Use q[] to avoid that, q[] is the same as ''.
Re: If I Want Backslashes, I'll Ask For Backslashes
by faure (Sexton) on Aug 10, 2001 at 06:22 UTC
    The heredoc is the way to quote metas.

    I think :)

    Update:

    Here's what I got:

    #!/usr/bin/perl5.6 -w use strict; my $foo = "interpolated"; print <<END; \n \ $foo END print <<'END'; \n \ $foo END print qq[\n \ $foo ]; print q[\n \ $foo ]; ------- output: ------- interpolated \n \ $foo interpolated \n \ $foo
Re: If I Want Backslashes, I'll Ask For Backslashes
by John M. Dlugosz (Monsignor) on Aug 10, 2001 at 08:46 UTC
    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.

      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