Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Problem w/ single-quoted strings

by bassplayer (Monsignor)
on May 13, 2004 at 18:47 UTC ( #353179=note: print w/ replies, xml ) Need Help??


in reply to Problem w/ single-quoted strings

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{}.

Update: added some extra detail

bassplayer


Comment on Re: Problem w/ single-quoted strings
Select or Download Code
Re: Re: Problem w/ single-quoted strings
by eweibust (Initiate) on May 13, 2004 at 19:56 UTC
    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

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://353179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (9)
As of 2014-08-29 20:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (289 votes), past polls