Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the difference between single and double quotes in Perl?

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.
like image 484
Vijay Avatar asked Jan 08 '10 14:01

Vijay


People also ask

What is the difference between single quoted string and double quoted string in Perl?

The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string. It interprets the Escape sequences. Each variable will be replaced by its value.

What is the difference between a single and double quote?

General Usage Rules In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech. Single quotes are used to enclose a quote within a quote, a quote within a headline, or a title within a quote.

When should we use single quote and double quote?

Should I use single or double quotation marks? The use of single and double quotation marks when quoting differs between US and UK English. In US English, you must use double quotation marks. Single quotation marks are used for quotes within quotes.

What is the difference between single quotes and double quotes?

Single quotes don't In a double quoted string you need to escape certain characters to stop them being interpreted differently. In a single quoted string you don't (except for a backslash if it is the final character in the string) Show activity on this post. " will resolve variables, and escape characters. Show activity on this post.

What is the use of double quotes in programming?

In Programming. In computer programming, quotes are often used as delimiters around strings. In HTML, CSS and JavaScript code, single and double quotes are interchangeable. XML and XHTML require the use of double quotes to delimit attributes of elements.

What is a double-quoted string?

This is a portal for geeks. Double-quoted strings: By using Double quotes the PHP code is forced to evaluate the whole string. The main difference between double quotes and single quotes is that by using double quotes, you can include variables directly within the string.

What is a double quote used for in a headline?

Single Quotes used as a quote in a headline Double quotes are used to mark speech, for titles of short works like TV shows and articles, as scare quotes to indicate irony or an author’s disagreement with a premise. In America, Canada, Australia and New Zealand, the general rule is that double quotes are used to denote direct speech.


1 Answers

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.

like image 52
Iain Collins Avatar answered Sep 21 '22 18:09

Iain Collins