views:

644

answers:

6

I am just begining to learn Perl. I looked at the beginning perl page and started working.

It says:

The difference between single quotes and double quotes is that single quotes mean that their contents should be taken literally, while double quotes mean that their contents should be interpreted

When I run this program:

#!/usr/local/bin/perl
print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.';

It outputs:

This string
 shows up on two lines.
This string
 shows up on only one.

Am I wrong somewhere? the version of perl below:

perl -v

This is perl, v5.8.5 built for aix

Copyright 1987-2004, Larry Wall

Perl may be copied only under the terms of either the Artistic License or the
GNU General Public License, which may be found in the Perl 5 source kit.

Complete documentation for Perl, including FAQ lists, should be found on
this system using `man perl' or `perldoc perl'.  If you have access to the
Internet, point your browser at http://www.perl.com/, the Perl Home Page.
A: 

By "interpreted", they mean that variable names and such will not be printed, but their values instead. \n is an escape sequence, so I'd think it would not be interpreted.

neohaven
+13  A: 

I am inclined to say something is up with your shell/terminal, and whatever you are outputting to is interpreting the \n as a newline and that the problem is not with Perl.

To confirm: This Shouldn't Happen(TM) - in the first case I would expect to see a new line inserted, but with single quotes it ought to output literally the characters \n and not a new line.

Iain Collins
+4  A: 

In Perl, single-quoted strings do not expand backslash-escapes like \n or \t. The reason you're seeing them expanded is probably due to the nature of the shell that you're using, which is munging your output for some reason.

friedo
They also don't do variable interpolation ("foo is $bar")
amphetamachine
+3  A: 

Everything you need to know about quoting and quote-like operators is in perlop.

To answer your specific question, double-quotes can turn certain sequences of literal characters into other characters. In your example, the double quotes turn the sequence of characters \ and n into the single character that represents a newline. In a single quoted string, that same literal sequence is just the literal \ and n characters.

brian d foy
+1  A: 

In addition to your O'Reilly link, a reference no less authoritative than the 'Programming Perl' book by Larry Wall, states that backslash interpolation does not occur in single quoted strings.

... much like Unix shell quotes: double quoted string literals are subject to   
backslash and variable interpolation; single quoted strings are not   
(except for \' and \\, so that you may ...)  

Programing Perl, 2nd ed, 1996 page 16

So it would be interesting to see what your Perl does with
print 'Double backslash n: \\n';

As above, please show us the output from 'perl -v'.

And I believe I have confused the forum editor software, because that last Perl 'print' should have indented.

gary
if i write as print `\\n`; this outputs `\n`
Vijay Sarathi
+1  A: 

If you use the double quote it will be interpreted the \n as a newline.

But if you use the single quote it will not interpreted the \n as a newline.

For me it is working correctly.

file content

print "This string \n shows up on two lines.";
print 'This string \n shows up on only one.'
muruga