Quotation marks are fairly ubiquitous in Perl, as in most programming languages. Different methods of quoting exist, and each has different uses and a different set of behaviors associated with it.

This document is intended as a treatment of the subject of delimiting strings — "quoting". I try to keep it from devolving into lengthy digressions on tangential topics, such as escape characters and interpolation. Such topics do tie in with the use of such delimiters, however, and must to some degree be addressed. Further information can be found at perlop.


Single quotation marks are used to enclose data you want taken literally. Just as the <code></code> tags here at the Monastery make all text they enclose literally rendered, whitespace and all, so too does a set of single-quotes in Perl ensure that what they enclose is used literally:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = 'it is worth $foo'; print $bar;

This example, when run, produces the following:
it is worth $foo


Double quotation marks are used to enclose data that needs to be interpolated before processing. That means that escaped characters and variables aren't simply literally inserted into later operations, but are evaluated on the spot. Escape characters can be used to insert newlines, tabs, and other special characters into a string, for instance. The values, or contents, of variables are used in double-quoted strings, rather than the names of variables. For instance:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = "it is worth $foo"; print $bar;

This example, when run, produces the following:
it is worth 7

Double-quotes interpolate scalar and array variables, but not hashes. On the other hand, you can use double-quotes to interpolate slices of both arrays and hashes.

escape characters

The interpolating effects of double-quotes creates a necessity of using escaped characters to reproduce characters within a string that would be displayed literally within single-quotes, however. For instance, to add quotes to the above-printed string in the double-quote example, you would have to do something like this:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = "it is \"worth\" $foo"; print $bar;

The backslash "escapes" the quotation mark that follows it, so that running the above produces the following:
it is "worth" 7

An exception to the literal interpretation behavior in the use of single-quotes is when you wish to include single-quotes inside the string. In that case, you must escape the single-quotes you want inside the string so the Perl compiler can discern between them and the single-quotes that delimit the string:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = 'it is \'worth\' $foo'; print $bar;

This example, when run, produces the following:
it is 'worth' $foo

In any interpolating quotation scheme, such as double-quotes (or qq and interpolating uses of <<, as described below), @ and $ characters must be escaped when you want them used literally. If they are not, they will be treated by the Perl compiler as indicators of variable names.

quoting without quotes

In Perl, you can use methods other than quotation marks to "quote" a string. This functionality makes using strings that contain quotation marks much easier sometimes, since those quotation marks no longer need to be escaped. There are three simple methods of doing this with the letter q.

In the following three sections, on the subjects of q, qq, and qw notation, I refer exclusively to the use of parentheses as delimiters. Other delimiters can be used, however, which allows you to avoid having to escape the characters you choose to use as delimiters if you must also include instances of them in your string. For instance, instead of using parentheses, you could also use brackets ( [ ] ), braces ( { } ), asterisks ( * * ), and (almost?) any other characters other than whitespace. It's worth noting that in the case of an alphanumeric character there must be a space between the q (or qw, or qq) and the character in question.


The first way to quote without quotes is to use q() notation. Instead of using quotation marks, you would use parentheses with a q preceding them:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = q(it is 'worth' $foo); print $bar;

This example, when run, produces the following:
it is 'worth' $foo

The q() function works the same as single-quoting your string, with the exception that you no longer need to escape single-quotes that appear within the string. You would, however, have to escape any parentheses you need in the string.


In the same way that double-quotes add interpolation to the functionality of single-quotes, doubling the q adds interpolation to quoting without quotation marks. For instance, if you wanted to avoid escape characters and interpolate $foo in the above code, and wanted to use double-quotes around the word worth, you might do this:
#!/usr/bin/perl -w use strict; my $foo; my $bar; $foo = 7; $bar = qq(it is "worth" $foo); print $bar;

This example, when run, produces the following:
it is "worth" 7


You can use qw to quote individual words without interpolation. Use whitespace to separate terms you would otherwise have to separate by quoting individually and adding commas. This is often quite useful when assigning lists to array variables. The two following statements are equivalent:
@baz = ('one', 'two', 'three'); @baz = qw(one two three);

here documents

If you want to quote many lines of text literally, you can use "Here Document" notation. This consists of an introductory line which has two less-than signs (aka "angles" or "angle brackets": << ) followed by a keyword — the end tag — for signalling the end of the quote. All text and lines following the introductory line are quoted. The quote ends when the end tag is found, by itself, on a line. For example, if the end tag is EOT:
#!/usr/bin/perl -w use strict; my $foo = 123.45; my $bar = "Martha Stewedprune"; print <<"EOT"; ===== This is an example of text taken literally except that variables are expanded where their variable names appear. foo: $foo bar: $bar EOT

This example, when run, produces the following:
===== This is an example of text taken literally except that variables are expanded where their variable names appear. foo: 123.45 bar: Martha Stewedprune

The type of quotation marks placed around the end tag is important. For instance, in the above example, EOT is double-quoted, and as a result the values of $foo and $bar are substituted for the variable names. Double-quoting the end tag causes the quoted block of text to be interpolated as it would be with double-quotes. Use of single-quotes would cause it to be used literally, without interpolation, as though the quoted block of text were delimited by single-quotes rather than being referenced as a "here document". Omitting the quotation marks entirely defaults to behavior the same as using double-quotes.

some notes to keep in mind:
The use of here documents is particularly useful in Perl scripts that include HTML and other markup, because it allows you to keep the markup relatively free of escape characters that would otherwise reduce the readability of it.

Thanks due the PerlMonks community members who contributed suggestions and comments in discussion below.

- apotheon
CopyWrite Chad Perrin