Do you know where your variables are? PerlMonks

### Re^2: Escaping multiple escape chars

by JamesNC (Chaplain)
 on Dec 10, 2005 at 00:48 UTC ( #515682=note: print w/replies, xml ) Need Help??

in reply to Re: Escaping multiple escape chars
in thread Escaping multiple escape chars

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

Replies are listed 'Best First'.
Re^3: Escaping multiple escape chars
by ikegami (Pope) on Dec 10, 2005 at 06:11 UTC

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); [download] versus: use Devel::Peek; my$txt = "some\text";

print $txt; #prints some [tab] ext Dump($txt);
[download]

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);
[download]
Produces the exact same results as:
use Devel::Peek;

my $txt = '\\|/';$txt =~s/\\/\\\\/g;

print $txt; Dump($txt);
[download]
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.
' and " are different. All of this is documented.
A literal should mean, I am to be taken literally

Nonsense. Literals are always interpreted. The literal 1234 in the expression \$a = 1234; is a group of characters, yet they are interpreted as a number. And somehome, the quotes in the literal "abc" are removed.

Without interpretation, the compiler wouldn't know where the string ends and/or wouldn't allow certain characters to be part of the string. For example, what if there was a quote in your ASCII art? How would Perl know the string doesn't end at that quote, but rather the following one? (or the one after that?) For single quoted strings, you preceed the quote with a backslash. Of course, now we need a method of allowing backslashes followed by single quotes...

Whenever something is embedded in something else, be it a string in a Perl source file, a object in a data file or text between HTML tags, some form of encoding or escaping is required. To be crystal clear: You can't have strings and Perl code in the same file without some form of escaping or encoding.

Re^3: Escaping multiple escape chars
by ikegami (Pope) on Dec 10, 2005 at 06:14 UTC

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 \.

Create A New User
Node Status?
node history
Node Type: note [id://515682]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2018-01-21 05:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
How did you see in the new year?

Results (227 votes). Check out past polls.

Notices?