ch02_03.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 415 行 · 第 1/2 页
HTM
415 行
<blockquote><pre class="code">\cC</pre></blockquote></td><td><p>A "control" character (here, Ctrl-C)</p></td></tr><tr><td><blockquote><pre class="code">\\</pre></blockquote></td><td><p>Backslash</p></td></tr><tr><td><blockquote><pre class="code">\"</pre></blockquote></td><td><p>Double quote</p></td></tr><tr><td><blockquote><pre class="code">\l</pre></blockquote></td><td><p>Lowercase next letter</p></td></tr><tr><td><blockquote><pre class="code">\L</pre></blockquote></td><td><p>Lowercase all following letters until <tt class="literal">\E</tt></p></td></tr><tr><td><blockquote><pre class="code">\u</pre></blockquote></td><td><p>Uppercase next letter</p></td></tr><tr><td><blockquote><pre class="code">\U</pre></blockquote></td><td><p>Uppercase all following letters until <tt class="literal">\E</tt></p></td></tr><tr><td><blockquote><pre class="code">\Q</pre></blockquote></td><td><p>Quote non-word characters by adding a backslash until<tt class="literal">\E</tt></p></td></tr><tr><td><blockquote><pre class="code">\E</pre></blockquote></td><td><p>Terminate <tt class="literal">\L</tt>, <tt class="literal">\U</tt>, or<tt class="literal">\Q</tt></p></td></tr></table><p><p>Another feature of double-quoted strings is that they are<em class="emphasis">variable interpolated,</em><a name="INDEX-160" /><a name="INDEX-161" /><a name="INDEX-162" /> meaning that some variable nameswithin the string are replaced with their current values when thestrings are used. We haven't formally been introduced to what avariable looks like yet, so we'll get back to this later inthis chapter.</p></div><a name="lperl3-CHP-2-SECT-3.3" /><div class="sect2"><h3 class="sect2">2.3.3. String Operators</h3><p><a name="INDEX-163" />String values can be concatenated with the<tt class="literal">.</tt><a name="INDEX-164" /> operator. (Yes, that's a singleperiod.) This does not alter either string, any more than<tt class="literal">2+3</tt> alters either <tt class="literal">2</tt> or<tt class="literal">3</tt>. The resulting (longer) string is then availablefor further computation or to be stored into a variable. For example:</p><blockquote><pre class="code">"hello" . "world" # same as "helloworld""hello" . ' ' . "world" # same as 'hello world''hello world' . "\n" # same as "hello world\n"</pre></blockquote><p>Note that the concatenation must be explicitly requested with the<tt class="literal">.</tt> operator, unlike in some other languages whereyou merely have to stick the two values next to each other.</p><p>A special string operator is the <em class="emphasis">stringrepetition</em><a name="INDEX-165" /> <a name="INDEX-166" /> operator, consisting ofthe single lowercase letter <tt class="literal">x</tt>. This operator takesits left operand (a string) and makes as many concatenated copies ofthat string as indicated by its right operand (a number). Forexample:</p><blockquote><pre class="code">"fred" x 3 # is "fredfredfred""barney" x (4+1) # is "barney" x 5, or "barneybarneybarneybarneybarney"5 x 4 # is really "5" x 4, which is "5555"</pre></blockquote><p>That last example is worth spelling out slowly. The string repetitionoperator wants a string for a left operand, so the number<tt class="literal">5</tt> is converted to the string<tt class="literal">"5"</tt> (using rules described in detail later),giving a one-character string. This new string is then copied fourtimes, yielding the four-character string <tt class="literal">5555</tt>.Note that if we had reversed the order of the operands, as <tt class="literal">4x 5</tt>, we would have made five copies of the string<tt class="literal">4</tt>, yielding <tt class="literal">44444</tt>. This showsthat string repetition is not commutative.</p><p>The copy count (the right operand) is first truncated to an integervalue (4.8 becomes 4) before being used. A copy count of less thanone results in an empty (zero-length) string.</p></div><a name="lperl3-CHP-2-SECT-3.4" /><div class="sect2"><h3 class="sect2">2.3.4. Automatic Conversion Between Numbers and Strings</h3><p>For the most part, Perl automatically converts between<a name="INDEX-167" />numbers to<a name="INDEX-168" />strings as needed.How does it know whether a number or a string is needed? It alldepends upon the operator being used on the scalar value. If anoperator expects a number (like <tt class="literal">+</tt> does), Perl willsee the value as a number. If an operator expects a string (like<tt class="literal">.</tt> does), Perl will see the value as a string. Soyou don't need to worry about the difference between numbersand strings; just use the proper operators, and Perl will make it allwork.</p><p>When a string value is used where an operator needs a number (say,for multiplication), Perl automatically converts the string to itsequivalent numeric value, as if it had been entered as a decimalfloating-point value.<a href="#FOOTNOTE-50">[50]</a> So<tt class="literal">"12" * "3"</tt> gives the value <tt class="literal">36</tt>.Trailing nonnumber stuff and leading whitespace are discarded, so<tt class="literal">"12fred34" * " 3"</tt> will also give<tt class="literal">36</tt> without any complaints.<a href="#FOOTNOTE-51">[51]</a> At the extreme end of this, something that isn't anumber at all converts to zero. This would happen if you used thestring <tt class="literal">"fred"</tt> as a number.</p><blockquote class="footnote"> <a name="FOOTNOTE-50" /><p>[50]The trick of using a leadingzero to mean a nondecimal value works for literals, but never forautomatic conversion. Use <tt class="literal">hex( )</tt>or <tt class="literal">oct()</tt>to convert those kinds of strings.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-51" /><p>[51]Unlessyou request warnings, which we'll discuss in a moment.</p></blockquote><p>Likewise, if a numeric value is given when a string value is needed(say, for string concatenation), the numeric value is expanded intowhatever string would have been printed for that number. For example,if you want to concatenate the string <tt class="literal">Z</tt> followedby the result of 5 multiplied by 7,<a href="#FOOTNOTE-52">[52]</a> you cansay this simply as:</p><blockquote class="footnote"> <a name="FOOTNOTE-52" /><p>[52]We'll seeabout precedence and parentheses shortly.</p> </blockquote><blockquote><pre class="code">"Z" . 5 * 7 # same as "Z" . 35, or "Z35"</pre></blockquote><p>In other words, you don't really have to worry about whetheryou have a number or a string (most of the time). Perl performs allthe conversions for you.<a href="#FOOTNOTE-53">[53]</a> And ifyou're worried about efficiency, don't be. Perl generallyremembers the result of a conversion so that it's done onlyonce.<a name="INDEX-169" /></p><blockquote class="footnote"> <a name="FOOTNOTE-53" /><p>[53]It's usually not anissue, but these conversions can cause small round-off errors. Thatis, if you start with a number, convert it to a string, then convertthat string back to a number, the result may not be the same numberas you started with. It's not just Perl that does this;it's a consequence of the conversion process, so it happens toany powerful programming language.</p> </blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_02.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img alt="Home" border="0" src="../gifs/txthome.gif" /></a></td><td align="right" valign="top" width="228"><a href="ch02_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.2. Numbers</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img alt="Book Index" border="0" src="../gifs/index.gif" /></a></td><td align="right" valign="top" width="228">2.4. Perl's Built-in Warnings</td></tr></table></div><hr width="684" align="left" /><img alt="Library Navigation Links" border="0" src="../gifs/navbar.gif" usemap="#library-map" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"><area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map></body></html>
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?