⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch02_06.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 3 页
字号:
<tr><td><tt class="literal">\u</tt></td><td>Force next character to uppercase ("titlecase" in Unicode).<a name="INDEX-572"></a></td></tr><tr><td><tt class="literal">\l</tt></td><td>Force next character to lowercase.<a name="INDEX-573"></a></td></tr><tr><td><tt class="literal">\U</tt></td><td>Force all following characters to uppercase.</td></tr><tr><td><tt class="literal">\L</tt></td><td>Force all following characters to lowercase.</td></tr><tr><td><tt class="literal">\Q</tt></td><td>Backslash all following nonalphanumeric characters.<a name="INDEX-574"></a></td></tr><tr><td><tt class="literal">\E</tt></td><td>End <tt class="literal">\U</tt>, <tt class="literal">\L</tt>, or <tt class="literal">\Q</tt>.<a name="INDEX-575"></a></td></tr></table><a name="INDEX-576"></a><a name="INDEX-577"></a><p>You may also embed newlines directly in your strings; that is, theycan begin and end on different lines.  This is often useful,but it also means that if you forget a trailing quote, the errorwill not be reported until Perl finds another line containing thequote character, which may be much further on in the script.Fortunately, this usually causes an immediate syntax error on thesame line, and Perl is then smart enough to warn you that you mighthave a runaway string where it thought the string started.</p><p><a name="INDEX-578"></a><a name="INDEX-579"></a>Besides the backslash escapes listed above, double-quoted strings aresubject to <em class="emphasis">variable interpolation</em> of scalar and list values.  Thismeans that you can insert the values of certain variables directly intoa string literal.  It's really just a handy form of stringconcatenation.<a href="#FOOTNOTE-12">[12]</a> Variableinterpolation may be done for scalar variables, entire arrays (but nothashes), single elements from an array or hash, or slices (multiplesubscripts) of an array or hash.  Nothing else interpolates.  In otherwords, you may only interpolate expressions that begin with <tt class="literal">$</tt> or<tt class="literal">@</tt>, because those are the two characters (along with backslash) thatthe string parser looks for.  Inside strings, a literal <tt class="literal">@</tt> that isnot part of an array or slice identifier but is followed by analphanumeric character must be escaped with a backslash (<tt class="literal">\@</tt>), orelse a compilation error will result.  Although a complete hashspecified with a <tt class="literal">%</tt> may not be interpolated into the string, singlehash values or hash slices are okay, because they begin with <tt class="literal">$</tt> and<tt class="literal">@</tt> respectively.<a name="INDEX-580"></a><a name="INDEX-581"></a></p><blockquote class="footnote"><a name="FOOTNOTE-12"></a><p>[12]With warnings enabled, Perl may reportundefined values interpolated into strings as using the concatenationor join operations, even though you don't actually use those operatorsthere.  The compiler created them for you anyway.</p></blockquote><p>The following code segment prints out "<tt class="literal">The price is $100.</tt>":<blockquote><pre class="programlisting">$Price = '$100';                    # not interpolatedprint "The price is $Price.\n";     # interpolated</pre></blockquote><a name="INDEX-582"></a><a name="INDEX-583"></a><a name="INDEX-584"></a></p><p>As in some shells, you can put braces around the identifier todistinguish it from following alphanumerics: <tt class="literal">"How ${verb}able!"</tt>.An identifier within such braces is forced to be a string, as isany single identifier within a hash subscript.  Forexample:<blockquote><pre class="programlisting">$days{'Feb'}</pre></blockquote>can be written as:<blockquote><pre class="programlisting">$days{Feb}</pre></blockquote>and the quotes will be assumed.  Anything more complicated in thesubscript is interpreted as an expression, and then you'd have toput in the quotes:<blockquote><pre class="programlisting">$days{'February 29th'}   # Ok.$days{"February 29th"}   # Also ok. "" doesn't have to interpolate.$days{ February 29th }   # WRONG, produces parse error.</pre></blockquote>In particular, you should always use quotes in slices such as:<blockquote><pre class="programlisting">@days{'Jan','Feb'}       # Ok.@days{"Jan","Feb"}       # Also ok.@days{ Jan,  Feb }       # Kinda wrong (breaks under use strict)</pre></blockquote></p><p><a name="INDEX-585"></a>Apart from the subscripts of interpolated array and hash variables,there are no multiple levels of interpolation.  Contrary to theexpectations of shell programmers, backticks do not interpolatewithin double quotes, nor do single quotes impede evaluation ofvariables when used within double quotes.  Interpolation is extremelypowerful but strictly controlled in Perl.  It happens only insidedouble quotes, and in certain other "double-quotish" operationsthat we'll describe in the next section:<blockquote><pre class="programlisting">print "\n";              # Ok, print a newline.print  \n ;              # WRONG, no interpolative context.</pre></blockquote></p><a name="INDEX-586"></a><a name="ch02-sect-pick"></a><h3 class="sect2">2.6.3. Pick Your Own Quotes</h3><a name="INDEX-587"></a><a name="INDEX-588"></a><a name="INDEX-589"></a><a name="INDEX-590"></a><p>Although we usually think of quotes as literal values, in Perl theyfunction more like operators, providing various kinds of interpolatingand pattern-matching capabilities.  Perl provides the customaryquote characters for these behaviors, but also provides a more general way foryou to choose your quote character for any of them.  In<a href="ch02_06.htm#perl2-ch-2-tab-5a">Table 2-3</a>, any nonalphanumeric, nonwhitespacedelimiter may be used in place of <tt class="literal">/</tt>.  (The newlineand space characters are no longer allowed as delimiters, although ancientversions of Perl once allowed this.)</p><a name="perl2-ch-2-tab-5a"></a><h4 class="objtitle">Table 2.3. Quote Constructs</h4><table border="1"><tr><th>Customary</th><th>Generic</th><th>Meaning</th><th>Interpolates</th></tr><tr><td><tt class="literal">''</tt></td><td><tt class="literal">q//</tt></td><td>Literal string</td><td>No</td></tr><tr><td><tt class="literal">""</tt></td><td><tt class="literal">qq//</tt></td><td>Literal string</td><td>Yes</td></tr><tr><td><tt class="literal">``</tt></td><td><tt class="literal">qx//</tt></td><td>Command execution</td><td>Yes</td></tr><tr><td><tt class="literal">()</tt></td><td><tt class="literal">qw//</tt></td><td>Word list</td><td>No</td></tr><tr><td><tt class="literal">//</tt></td><td><tt class="literal">m//</tt></td><td>Pattern match</td><td>Yes</td></tr><tr><td><tt class="literal">s///</tt></td><td><tt class="literal">s///</tt></td><td>Pattern substitution</td><td>Yes</td></tr><tr><td><tt class="literal">y///</tt></td><td><tt class="literal">tr///</tt></td><td>Character translation</td><td>No</td></tr><tr><td><tt class="literal">""</tt></td><td><tt class="literal">qr//</tt></td><td>Regular expression</td><td>Yes</td></tr></table><p><a name="INDEX-591"></a>Some of these are simply forms of "syntactic sugar" to let you avoidputting too many backslashes into quoted strings, particularly intopattern matches where your regular slashes and backslashes tend toget all tangled.</p><p><a name="INDEX-592"></a>If you choose single quotes for delimiters, no variable interpolationis done even on those forms that ordinarily interpolate.  If theopening delimiter is an opening parenthesis, bracket, brace, orangle bracket, the closing delimiter will be the correspondingclosing character.  (Embedded occurrences of the delimiters mustmatch in pairs.)  Examples:<blockquote><pre class="programlisting">$single = q!I said, "You said, 'She said it.'"!;$double = qq(Can't we get some "good" $variable?);$chunk_of_code = q {    if ($condition) {        print "Gotcha!";    }};</pre></blockquote><a name="INDEX-593"></a>The last example demonstrates that you can use whitespace between thequote specifier and its initial bracketing character.  For two-elementconstructs like <tt class="literal">s///</tt> and <tt class="literal">tr///</tt>,if the first pair of quotes is a bracketing pair, the second part getsits own starting quote character.  In fact, the second pair needn't bethe same as the first pair.  So you can write things like<tt class="literal">s&lt;foo&gt;(bar)</tt> or<tt class="literal">tr(a-f)[A-F]</tt>.  Because whitespace is also allowedbetween the two inner quote characters, you could even write that lastone as:<blockquote><pre class="programlisting">tr (a-f)   [A-F];</pre></blockquote>Whitespace is not allowed, however, when <tt class="literal">#</tt> is being used as thequoting character.  <tt class="literal">q#foo#</tt> is parsed as the string <tt class="literal">'foo'</tt>, while<tt class="literal">q #foo#</tt> is parsed as the quote operator <tt class="literal">q</tt> followed by a comment.Its delimiter will be taken from the next line.  Comments can also beplaced in the middle of two-element constructs, which allows you towrite:<blockquote><pre class="programlisting">s {foo}   # Replace foo  {bar};  #    with bar.tr [a-f]  # Transliterate lowercase hex   [A-F]; #            to uppercase hex</pre></blockquote><a name="INDEX-594"></a></p><h3 class="sect2">2.6.4. Or Leave the Quotes Out Entirely</h3><a name="INDEX-595"></a><a name="INDEX-596"></a><a name="INDEX-597"></a><a name="INDEX-598"></a><p>A name that has no other interpretation in the grammar will betreated as if it were a quoted string.  These are known as<em class="emphasis">barewords</em>.<a href="#FOOTNOTE-13">[13]</a> As with filehandles and labels, abareword that consists entirely of lowercase letters risks conflictwith future reserved words.  If you have warnings enabled, Perl willwarn you about barewords.  For example:<blockquote><pre class="programlisting">@days = (Mon,Tue,Wed,Thu,Fri);print STDOUT hello, ' ', world, "\n";</pre></blockquote><a name="INDEX-599"></a></p><blockquote class="footnote"><a name="FOOTNOTE-13"></a><p>[13] Variable names, filehandles, labels, and thelike are not considered barewords because they have a meaning forced bya preceding token or a following token (or both).  Predeclared namessuch as subroutines aren't barewords either.  It's only a bareword whenthe parser has no clue.</p></blockquote><p>sets the array <tt class="literal">@days</tt> to the short form of the weekdays 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -