"be consistent" PerlMonks

Re: Problem w/ single-quoted strings

by Stevie-O (Friar)
 on May 13, 2004 at 21:46 UTC ( #353219=note: print w/ replies, xml ) Need Help??

in reply to Problem w/ single-quoted strings

OK, first off, I'm a bit confused, because your first line says 'Testing\z'. Perhaps you meant 'Testing\n' ?

With that out of the way, you are missing something -- or rather, the document you just quoted is missing something.

It handles three cases:

1. Any character OTHER than a backslash or termination character is inserted literally. (The termination character is normally ' but may differ if q// is used.)
2. Any backslash followed by the termination character inserts the termination character.
3. Any backslash followed by a backslash inserts a backslash.
What it doesn't tell you is the fourth case:
1. Any backslash followed by a character OTHER than the termination character or a backslash inserts a backslash and that literal character.
The reason for the double-backslash thing is in case you wanted to include a backslash at the end of the string. For example:
```\$foo = 'C:\\'; # if you had 'C:\' then the ' would instead be escaped
--Stevie-O
```\$"=\$,,\$_=q>|\p4<6 8p<M/_|<('=>
.q>.<4-KI<l|2\$<6%s!<qn#F<>;\$,
.=pack'N*',"@{[unpack'C*',\$_]
}"for split/</;\$_=\$,,y[A-Z a-z]
{}cd;print lc

Comment on Re: Problem w/ single-quoted strings
Re: Re: Problem w/ single-quoted strings
by eweibust (Initiate) on May 14, 2004 at 18:23 UTC
Stevie-O,

Very good post. Let me try to break it down.

Case 1. Simply means you get what's b/t the single-quotes when there aren't any backslashes (complicating things).

Case 2. Is an exception that allows you to use your termination character inside your string. To do this an "evil" backslash is used. You only get the term char in your string; the backslash disappears....

Case 3. I guess is a way to get a backslash into your string by using two consecutive backslashes. This confuses me b/c....

Case 4. Says that a backslash that is followed by anything other then a backslash or term char adds the backslash and the char following it. To me this seems redundant, b/c Case 3 also got you a backslash in your string.

I am posting some code as an example.
```print 'case 3 ','Test\\ing';
print "\n";

print 'case 4 ','Test\ing';
print "\n";

erikweibust@daleweibust ~/perl/lrn_perl \$ ./test2.pl
case 3 Test\ing
case 4 Test\ing

Create A New User
Node Status?
node history
Node Type: note [id://353219]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (7)
As of 2013-12-21 12:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?