in reply to Escaping multiple escape chars

In single quote string literals, "\\" is interpreted as "\", "\'" is interpreted as "'", and everything else is left as is.

$txt = ' __\\U//__ '; print("$txt\n"); # Prints __\U//__ $txt = ' __\\\\U//__ '; print("$txt\n"); # Prints __\\U//__ $txt =~s/\\/\\\\/g; print("$txt\n"); # Prints __\\\\U//__

That only applies to string literals. If you had been reading from a file, your code would have worked:

$txt = <DATA>; print("$txt\n"); # Prints __\\U//__ $txt =~s/\\/\\\\/g; print("$txt\n"); # Prints __\\\\U//__ __DATA__ __\\U//__

Replies are listed 'Best First'.
Re^2: Escaping multiple escape chars
by JamesNC (Chaplain) on Dec 10, 2005 at 00:48 UTC
    At first, I thought I understood this, but this still is a little confusing to me. How does this work, and why doesn't using '' -vs- "" make a difference? I am intrigued and would like to understand this better. Is there a way to make perl think the data came from a stream?
    Thanks,
    James

      Why would ' vs " make a difference? \\ is used as an escape character for both.

      In single quote string literals, "\\" is interpreted as "\", "\'" is interpreted as "'", and everything else is left as is.

      In double quoted string literals, "\\" is interpreted as "\", "\"" is interpreted as """, ...

      Is there a way to make perl think the data came from a stream?

      No. That makes no sense. The script doesn't know whether the data came from a stream or not. The conversion happens during the parsing of the script by the Perl parser. If it didn't do this, you would have no way of specifying certain strings, such as the one containing solely \.

        Perhaps this example helps better what I am trying to say that context matters ( ' vs " ):
        use Devel::Peek; my $txt = 'some\text'; print $txt; #prints some\text Dump($txt);
        versus:
        use Devel::Peek; my $txt = "some\text"; print $txt; #prints some [tab] ext Dump($txt);

        If what you were saying was true, then the first line should print the same as the second.
        The substitution:
        use Devel::Peek; my $txt = '\|/'; $txt =~s/\\/\\\\/g; print $txt; Dump($txt);
        Produces the exact same results as:
        use Devel::Peek; my $txt = '\\|/'; $txt =~s/\\/\\\\/g; print $txt; Dump($txt);
        Aren't they different literals? Change the ' to " and try the same test.
        I think the logic is broken here. A literal should mean, I am to be taken literally, not, interpret part of me as a literal and part of me however you take a guess, because that is what is happening or else it would store 'some\text' the same as "some\text". The parser is just getting this case incorrect I think.

      Why would ' vs " make a difference? \\ is used as an escape character for both.

      In single quote string literals, "\\" is interpreted as "\", "\'" is interpreted as "'", and everything else is left as is.

      In double quoted string literals, "\\" is interpreted as "\", "\"" is interpreted as """, ...

      Is there a way to make perl think the data came from a stream?

      No. That makes no sense. The script doesn't know whether the data came from a stream or not. The conversion happens during the parsing of the script by the Perl parser. If it didn't do this, you would have no way of specifying certain strings, such as the one containing solely \.

Re^2: Escaping multiple escape chars
by JamesNC (Chaplain) on Dec 09, 2005 at 21:03 UTC
    Doh! Thanks ikegami! I will load the art into files.