Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Problem w/ single-quoted strings

by bassplayer (Monsignor)
on May 13, 2004 at 18:47 UTC ( [id://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

Replies are listed 'Best First'.
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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://353179]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (7)
As of 2024-04-25 08:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found