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

📄 ch02_11.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>The undef Value (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_10.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_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">2.11. The undef Value</h2><p>What happens if you use a scalar variable before you give it a value?Nothing serious, and definitely nothing fatal. Variables have thespecial <tt class="literal">undef</tt><a name="INDEX-252" /><a name="INDEX-253" /> value before they are first assigned,which is just Perl's way of saying "nothing here to lookat -- move along, move along." If you try to use this"nothing" as a "numeric something," it actslike 0. If you try to use it as a "string something," itacts like the empty string. But <tt class="literal">undef</tt> is neither anumber nor a string; it's an entirely separate kind of scalarvalue.</p><p>Because <tt class="literal">undef</tt> automatically acts like zero whenused as a number, it's easy to make an numeric accumulator thatstarts out empty:</p><blockquote><pre class="code"># Add up some odd numbers$n = 1;while ($n &lt; 10) {  $sum += $n;  $n += 2; # On to the next odd number}print "The total was $sum.\n";</pre></blockquote><p>This works properly when <tt class="literal">$sum</tt> was<tt class="literal">undef</tt> before the loop started. The first timethrough the loop, <tt class="literal">$n</tt> is one, so the first lineinside the loop adds one to <tt class="literal">$sum</tt>. That'slike adding one to a variable that already holds zero (becausewe're using <tt class="literal">undef</tt> as if it were a number).So now it has the value <tt class="literal">1</tt>. After that, sinceit's been initialized, adding works in the traditional way.</p><p>Similarly, you could have a <a name="INDEX-254" />string accumulator that starts outempty:</p><blockquote><pre class="code">$string .= "more text\n";</pre></blockquote><p>If <tt class="literal">$string</tt> is <tt class="literal">undef</tt>, this willact as if it already held the empty string, putting <tt class="literal">"moretext\n"</tt> into that variable. But if it already holds astring, the new text is simply appended.</p><p>Perl programmers frequently use a new variable in this way, lettingit act as either zero or the empty string as needed.</p><p>Many operators return <tt class="literal">undef</tt> when the arguments areout of range or don't make sense. If you don't doanything special, you'll get a zero or a null string withoutmajor consequences. In practice, this is hardly a problem. In fact,most programmers will rely upon this behavior. But you should knowthat when warnings are turned on, Perl will typically warn aboutunusual uses of the undefined value, since that may indicate a bug.For example, simply copying <tt class="literal">undef</tt> from onevariable into another isn't a problem, but trying to<tt class="literal">print</tt> it would generally cause a warning.</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_10.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_12.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">2.10. The while Control Structure</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.12. The defined Function</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 + -