Advertisement
Online Test Banks
Score higher
See Online Test Banks
eLearning
Learning anything is easy
Browse Online Courses
Mobile Apps
Learning on the go
Explore Mobile Apps
Dummies Store
Shop for books and more
Start Shopping

Contrasting Single and Double Quotes in Perl

To summarize the main difference between single and double quotation marks: Single quotation marks do not interpret, and double quotation marks do. That is, if you put something in single quotation marks, Perl assumes that you want the exact characters you place between the marks — except for the slash-single quote (\') combination and double-slash (\\) combination. If you place text inside double quotation marks, however, Perl interprets variable names. Perl also interprets special characters inside double-quoted literal strings.

Take a look at the following short program, which uses single quotes in its print statement:

$Book = 'Perl For Dummies';
print 'The title is $Book.';

When you run the program, Perl displays

The title is $Book.

Now change the single quotes to double quotes in the print statement:

$Book = 'Perl For Dummies';
print "The title is $Book.";

When you run the program now, Perl displays

The title is Perl For Dummies.

In the first program, the single quotes tell Perl not to interpret anything inside the quotation marks. In the second program, Perl sees the double quotes, interprets the variable $Book, and then inserts that into the text.

Note that the period at the end of the print statement appears immediately after the value of $Book. After interpreting the variable $Book, Perl starts looking for text again, finds the period, and prints it.

You can have as many variables as you want inside double-quoted strings. For example:

$Word1 = "thank";
$Word2 = "you";
$Sentence = "I just wanted to say $Word1 $Word2.";
print $Sentence;

These lines print

I just wanted to say thank you.

Perl interprets each variable and places it directly in the variable $Sentence. Notice that when the print statement displays the contents of $Sentence, it inserts a space between the two quoted words, just as it should, and the period at the end. Perl picks out just the variables and substitutes for them but leaves other characters, such as spaces, exactly as you enter them.

  • Add a Comment
  • Print
  • Share
blog comments powered by Disqus
Advertisement
Advertisement

Inside Dummies.com

Dummies.com Sweepstakes

Win an iPad Mini. Enter to win now!