No such thing as a small change | |
PerlMonks |
quotes in Perlby apotheon (Deacon) |
on Oct 20, 2004 at 22:33 UTC ( [id://401006]=perltutorial: print w/replies, xml ) | Need Help?? |
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-quotesSingle 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:
This example, when run, produces the following: it is worth $foo double-quotesDouble 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:
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 charactersThe 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:
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:
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 quotesIn 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. qThe 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:
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.
This example, when run, produces the following: it is "worth" 7 qwYou 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:
here documentsIf 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:
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:
- apotheon
CopyWrite Chad Perrin
Back to
Tutorials
|
|