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

📄 ch02_09.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>The chomp Operator (Learning Perl, 3rd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Randal L. Schwartz and Tom Phoenix" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596001320L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Learning Perl, 3rd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Learning Perl, 3rd Edition" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_08.htm"><img alt="Previous" border="0" src="../gifs/txtpreva.gif" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"></a></td><td align="right" valign="top" width="228"><a href="ch02_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.9. The chomp Operator</h2><p>The first time you read about the<tt class="literal">chomp</tt><a name="INDEX-242" /><a name="INDEX-243" /> operator, it seemsterribly overspecialized. It works on a variable. The variable has tohold a string. And if the string ends in a newline character,<tt class="literal">chomp</tt> can get rid of the newline. That's(nearly) all it does. For example:</p><blockquote><pre class="code">$text = "a line of text\n"; # Or the same thing from &lt;STDIN&gt;chomp($text);               # Gets rid of the newline character</pre></blockquote><p>But it turns out to be so useful, you'll put it into nearlyevery program you write. As you see, it's the best way toremove a trailing newline from a string in a variable. In fact,there's an easier way to use <tt class="literal">chomp</tt>, becauseof a simple rule: any time that you need a variable in Perl, you canuse an assignment instead. First, Perl does the assignment. Then ituses the variable in whatever way you requested. So the most commonuse of <tt class="literal">chomp</tt> looks like this:</p><blockquote><pre class="code">chomp($text = &lt;STDIN&gt;); # Read the text, without the newline character$text = &lt;STDIN&gt;;        # Do the same thing...chomp($text);           # ...but in two steps</pre></blockquote><p>At first glance, the combined <tt class="literal">chomp</tt> may not seemto be the easy way, especially if it seems more complex! If you thinkof it as two operations -- read a line, then<tt class="literal">chomp</tt> it -- then it's more natural towrite it as two statements. But if you think of it as oneoperation -- read just the text, not the newline -- it'smore natural to write the one statement. And since most other Perlprogrammers are going to write it that way, you may as well get usedto it now.</p><p><tt class="literal">chomp</tt> is actually a function. As a function, ithas a return value, which is the number of characters removed. Thisnumber is hardly ever useful:</p><blockquote><pre class="code">$food = &lt;STDIN&gt;;$betty = chomp $food; # gets the value 1 - but we knew that!</pre></blockquote><p>As you see, you may write <tt class="literal">chomp</tt> with or withoutthe parentheses. This is another general rule in Perl: except incases where it changes the meaning to remove them, parentheses arealways optional.</p><p>If a line ends with two or more newlines,<a href="#FOOTNOTE-64">[64]</a><tt class="literal">chomp</tt> removes only one. If there's nonewline, it does nothing, and returns zero.</p><blockquote class="footnote"> <a name="FOOTNOTE-64" /><p>[64]Thissituation can't arise if we're reading a line at a time,but it certainly can when we have set the input separator(<tt class="literal">$/</tt>) to something other than newline, or use the<tt class="literal">read</tt> function, or perhaps have glued somestrings together ourselves.</p> </blockquote><p>If you work with older Perl programs, you may run across the<tt class="literal">chop</tt> operator. It's similar, but removes<em class="emphasis">any</em> trailing character, not just a trailingnewline. Since that could accidentally turn<tt class="literal">pebbles</tt> into <tt class="literal">pebble</tt>, it'susually not what you want.</p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_08.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_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.8. Getting User Input</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.10. The while Control Structure</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 &copy; 2002</a> O'Reilly &amp; 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 + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -