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.
This example, when run, produces the following:
This example, when run, produces the following:
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.
The backslash "escapes" the quotation mark that follows it, so that running the above produces the following:
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 discriminate between them and the single-quotes that delimit the string:
This example, when run, produces the following:
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.
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 non-whitespace characters. 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.
This example, when run, produces the following:
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.
qq
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 write this:
This example, when run, produces the following:
This example, when run, produces the following:
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:
Thanks due the PerlMonks community members who contributed suggestions and comments in discussion below.
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-quotes
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-quotes
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 discriminate 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, because 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 non-whitespace characters. 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.
q
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.
#!/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
qw
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 end tag specifier must follow the << without any intermediate space.
- The actual end tag must be exactly the same as specified in the introduction line.
- The introduction line must end with a semicolon.
- apotheon
CopyWrite Chad Perrin
CopyWrite Chad Perrin
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: quotes in Perl
by perlcapt (Pilgrim) on Oct 20, 2004 at 22:49 UTC | |
by apotheon (Deacon) on Oct 20, 2004 at 22:53 UTC | |
by perlcapt (Pilgrim) on Oct 21, 2004 at 00:00 UTC | |
by jZed (Prior) on Oct 21, 2004 at 02:35 UTC | |
by apotheon (Deacon) on Oct 21, 2004 at 06:20 UTC | |
Re: quotes in Perl
by ikegami (Patriarch) on Oct 21, 2004 at 05:28 UTC | |
by ihb (Deacon) on Oct 21, 2004 at 11:32 UTC | |
Re: quotes in Perl
by TheEnigma (Pilgrim) on Oct 20, 2004 at 23:10 UTC | |
by revdiablo (Prior) on Oct 21, 2004 at 05:07 UTC | |
by apotheon (Deacon) on Oct 20, 2004 at 23:20 UTC | |
Re: quotes in Perl
by bobf (Monsignor) on Oct 21, 2004 at 21:10 UTC | |
Re: quotes in Perl
by ihb (Deacon) on Oct 21, 2004 at 11:20 UTC | |
by apotheon (Deacon) on Oct 21, 2004 at 14:13 UTC | |
Re: quotes in Perl (opinionated note on coding style)
by dimar (Curate) on Feb 09, 2005 at 18:04 UTC | |
by hv (Prior) on Feb 10, 2005 at 14:56 UTC | |
by TimToady (Parson) on Feb 10, 2005 at 17:36 UTC | |
Re: quotes in Perl
by japhy (Canon) on Aug 03, 2005 at 16:52 UTC | |
Re: quotes in Perl
by nedals (Deacon) on Oct 21, 2004 at 07:02 UTC | |
by Happy-the-monk (Canon) on Oct 21, 2004 at 09:27 UTC | |
by nedals (Deacon) on Oct 22, 2004 at 01:06 UTC | |
by apotheon (Deacon) on Oct 21, 2004 at 07:24 UTC | |
Re: quotes in Perl
by Anonymous Monk on Apr 08, 2009 at 08:44 UTC | |
Re: quotes in Perl
by Anonymous Monk on Aug 06, 2012 at 21:00 UTC |
Back to
Tutorials