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

Why did the double backslash get interpolated into a single backslash? I'm using single quotes, not double quotes.
#!/usr/bin/perl -w use strict; my $m = '\\'; print length($m)."\n"; print "$m\n";

Replies are listed 'Best First'.
Re: Interpolation between single quotes?
by davorg (Chancellor) on Aug 03, 2005 at 14:42 UTC

    The backslash is needed as an escape character in single-quoted strings so that you can include single quotes within them. As it is used as an escape character, then it can't be used as a normal character. Therefore you need to use two of them (an escape character followed by a backslash) in order to put one within single quotes.

    The single quote and the backslash are the only two characters that need to be escaped in a single quoted string.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: Interpolation between single quotes?
by blazar (Canon) on Aug 03, 2005 at 14:41 UTC
    From perldoc perldata:
    String literals are usually delimited by either single or double quotes. They work much like quotes in the standard Unix shells: dou- ble-quoted string literals are subject to backslash and variable sub- stitution; single-quoted strings are not (except for "\'" and "\\").
    The allowed backslash substitutions are self-explanatory: although people would better use alternate delimiters, it is apparent that for completeness and consistency the possibility of backslashing "'" is given, and once that is provided, it is obvious that the backslash itself needs a backslash substitution as well.
Re: Interpolation between single quotes?
by ikegami (Patriarch) on Aug 03, 2005 at 15:09 UTC

    Welcome to the illustrated version of davorg's post, where we explore the reason Perl was written the way it was written using examples.

    What if you wanted to do "'" using single quotes? ''' won't work, so the slash was introduced: '\''.

    Now, what if you wanted to do "\\'" using single quotes? '\'' won't work -- we know from the previous paragraph that it would give a lone single quote -- so we need to escape the slash: '\\''. But that still doesn't work because the quote is not escaped. We escape the quote as well: '\\\''

    In single quotes, when the slash isn't followed by another slash or by a single quote, Perl doesn't require you to double it.

Re: Interpolation between single quotes?
by itub (Priest) on Aug 03, 2005 at 14:54 UTC
    *** FRAGMENT FROM "Regexp Quote-Like Operators" in perlop ***
     q/STRING/
     'STRING'
         A single-quoted, literal string.  A backslash represents a back-
         slash unless followed by the delimiter or another backslash, in
         which case the delimiter or backslash is interpolated.
    
             $foo = q!I said, "You said, 'She said it.'"!;
             $bar = q('This is it.');
             $baz = '\n';                # a two-character string
    
Re: Interpolation between single quotes?
by calin (Deacon) on Aug 03, 2005 at 19:04 UTC

    You can use the " ' " form of a here-doc quote to avoid backslash interpretation:

    print <<'EOS'; Test string \\ \' EOS