首页 > saving > Quote and Quote-like Operators

Quote and Quote-like Operators

2006年4月26日 hufey

While we usually think of quotes as literal values, in Perl they function as operators, providing various kinds of interpolating and pattern matching capabilities. Perl provides customary quote characters for these behaviors, but also provides a way for you to choose your quote character for any of them. In the following table, a {} represents any pair of delimiters you choose. Non-bracketing delimiters use the same character fore and aft, but the 4 sorts of brackets (round, angle, square, curly) will all nest.

Customary Generic Meaning Interpolates
q{} Literal no
“” qq{} Literal yes
qx{} Command yes (unless ” is delimiter)
qw{} Word list no
// m{} Pattern match yes
qr{} Pattern yes
s{}{} Substitution yes
tr{}{} Transliteration no (but see below)

Note that there can be whitespace between the operator and the quoting characters, except when # is being used as the quoting character. q#foo# is parsed as being the string foo, while q #foo# is the operator q followed by a comment. Its argument will be taken from the next line. This allows you to write:
s {foo} # Replace foo
{bar} # with bar.

分类: saving 标签:
本文的评论功能被关闭了.