eWebProgrammer
Distributednetworks GofPatterns
prev prev
  Course navigation
  String literals in Perl
String literals are normally enclosed in single quotation marks (') or double quotation marks (").
Single quotation marks are used to represent the exact text of the literal string; double quotation marks are used to interpolate the contents of the string.
For example,
#!/usr/bin/perl
$hi = 'Hi!';
print 'This is in single quotes: $hi'; 
print "\n";
will print this:
This is in single quotes: $hi

And this version,
#!/usr/bin/perl

$hi = 'Hi!';
print "This is in double quotes: $hi"; 
print "\n";
will print:
This is in double quotes: Hi!

In cases where either form will do, it's really a matter of style which one you choose to use. Some programmers favor the double quotation marks, and others favor single quotation marks. Rumors that one performs better than the other are largely unfounded.

Quote operators
In addition to the quotation marks themselves, there are also operators that you can use to specify your own quote characters. In short, the q operator specifies a character that works like a single quotation mark, and the qq operator specifies a character that works like a double quotation mark. These operators are covered in more detail in the "String Operators" lesson of the next module, but they essentially work like this:
$x =q(This uses parenthesis as single quotes);
$x = qq|This uses vertical bars as double quotes|;
When characters that have matched pairs are used (for example, parenthesis, brackets, and so on), the matching character is used to close the quotes; otherwise the same character is used on both sides.
For example, if you have some text that you want to use, but the text itself includes quotation marks, it can be convenient to use the quote operators to delimit the text:
$phrase =
  q{"Four score and seven years ago," said Abe.};
You will see more examples of this later in the course.
  Course navigation