We don't bite newbies here... much PerlMonks

### Problem w/ single-quoted strings

by eweibust (Initiate)
 on May 13, 2004 at 18:35 UTC Need Help??

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

I have just started learning Perl and this is my first Perl question. I'm working through "Learning Perl" and I've got a problem with strings. Specifically the difference b/t single-quoted and double-quoted strings and the use of the backslash character.

In the Single-quoted strings section it says that, "Any character other than a single quote or a backslash between the quote marks (including newline characters, if the string continues onto successive lines) stands for itself inside a string. To get a backslash, put two backslashes in a row, and to get a single quote, put a backslash followed by a single quote."

I tried the following:
```    print 'Testing\z';
print 'Testing\\n';
print 'Testing\\\n';
print "\n";
And get:
```    erikweibust@daleweibust ~/perl/lrn_perl \$ ./test1-2.pl
Testing\nTesting\nTesting\\n
erikweibust@daleweibust ~/perl/lrn_perl \$
So I'm scared that either the books wrong (please don't get the wrath of Randal Schwartz) or I'm missing something. I would have expected the output to be:
```    erikweibust@daleweibust ~/perl/lrn_perl \$ ./test1-2.pl
TestingnTesting\nTesting\n
erikweibust@daleweibust ~/perl/lrn_perl \$
Can anybody help clear this up?

Thanks,
Erik

Replies are listed 'Best First'.
Re: Problem w/ single-quoted strings
by bassplayer (Monsignor) on May 13, 2004 at 18:47 UTC
Variables and some escaped characters are not interpolated when between single quotes. The newline character referred to in your quote is the real character, not the \n representation. To get a new line, your test code would have to be doing something like this:
```print 'Testing
';
Concerning your code sample, the first line prints the word 'Testing' followed by the characters '\' and 'n'. The second line prints the word 'Testing' followed by a backslash, created by escaping a backslash (\\), followed by 'n'. The third line prints the word 'Testing' followed by a backslash created by escaping a backslash, followed by the characters '\' and 'n'.

Double quotes will allow escaped characters and variables to be interpolated. You can also use qq{}.

bassplayer

First off, using the \n was wrong on my part. I'm not concerned about newlines. The key to my post was the behavior I'm seeing when using backslashes inside single-quoted strings. Please look back at the quote I pulled from the book. It is my cause of concern. It said that to get a backslash to print as part of a single-quoted string you need to use two and what I showed proved that to be false. So I'm not sure if I'm misreading what the author was saying. If you have "Learning Perl" I'm quoting the 3rd edition, page 23.
It said that to get a backslash to print as part of a single-quoted string you need to use two and what I showed proved that to be false.
I don't believe that you have proven the statement false. When you used two, it interpolated it as one (as your quote described) and it came out as \n. Basically, it only interpolates \' and //, so \n is not going to become a newline. I apologize for any lack of clarity. nmcfarl provides a nice example below, and Stevie-O's #4 (below) lays it out nicely.

bassplayer

Re: Problem w/ single-quoted strings
by Stevie-O (Friar) on May 13, 2004 at 21:46 UTC
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
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
Re: Problem w/ single-quoted strings
by nmcfarl (Pilgrim) on May 13, 2004 at 21:25 UTC

I don't know about clear it up, but it looks to me that the text and the behavior do not match. The text more clearly describes what happens in quoted strings, not what happens in single quoted strings as this example shows:

```print '\ \\ \\\ \\\\ \\\\\ ';
print "\n";
print "\ \\ \\\ \\\\ \\\\\ ";
print "\n";
result:
```\ \ \\ \\ \\\
\ \ \\ \\

As you can see in the quoted string "\ " displays a space while in the single quotes it displays itself. Thus the backslash does not appear to always be an exception to the every char stands for itself rule. What it stands for appears to depend on what follows it. And sometimes it appears to stand for itself.

Ok if you read perlop manpage under q/STRING/ 'STRING' you will find that it perfectly describes what is happening:

' A backslash represents a backslash unless followed by the delimiter or another backslash, in which case the delimiter or backslash is interpolated.'

Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://353176]
Approved by cfreak
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2023-03-30 18:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
Voting Booth?
Which type of climate do you prefer to live in?

Results (74 votes). Check out past polls.

Notices?