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.
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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With