ch03_09.htm
来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 109 行
HTM
109 行
<html><head><title><STDIN> in List Context (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 & 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="ch03_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="ch03_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">3.9. <STDIN> in List Context</h2><p>One previously seen operator that returns a different value in anarray context is the line-input operator, <tt class="literal"><STDIN></tt><a name="INDEX-309" />. As described earlier, <tt class="literal"><STDIN></tt> returns the next line ofinput in a scalar context. Now, in list context, this operatorreturns <em class="emphasis">all</em> of the remaininglines up to the end of file. Each line is returned as a separateelement of the list. For example:</p><blockquote><pre class="code">@lines = <STDIN>; # read standard input in list context</pre></blockquote><p>When the input is coming from a file, this will read the rest of thefile. But how can there be an<a name="INDEX-310" />end-of-filewhen the input comes from the keyboard? On Unix and similar systems,including Linux and Mac OS X, you'll normally type aControl-D<a href="#FOOTNOTE-92">[92]</a> to indicate to the system that there's nomore input; the special character itself is never seen byPerl,<a href="#FOOTNOTE-93">[93]</a> even though it may be echoed to thescreen. On DOS/Windows systems, use Ctrl-Z instead.<a href="#FOOTNOTE-94">[94]</a>You'll need to check the documentation for your system or askyour local expert, if it's different from these.</p><blockquote class="footnote"> <a name="FOOTNOTE-92" /><p>[92]This is merely the default; it can bechanged by the <tt class="literal">stty</tt>command. But it's prettydependable -- we've never seen a Unix system where adifferent character was used to mean end-of-file from the keyboard.</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-93" /><p>[93]It's the OS that "sees" thecontrol key and reports "end of file" to theapplication.</p> </blockquote><blockquote class="footnote"><a name="FOOTNOTE-94" /><p>[94]There's a bug affecting some ports of Perl forDOS/Windows where the first line of output to the terminal followingthe use of Control-Z is obscured. On these systems, you can workaround this problem by simply printing a blank line(<tt class="literal">"\n")</tt>after reading the input.</p> </blockquote><p>If the person running the program types three lines, then presses theproper keys needed to indicate end-of-file, the array ends up withthree elements. Each element will be a string that ends in a newline,corresponding to the three newline-terminated lines entered.</p><p>Wouldn't it be nice if, having read those lines, you could<tt class="literal">chomp</tt> the newlines all at once?It turns out that if you give <tt class="literal">chomp</tt> a list of lines, it will remove thenewlines from each item in the list. For example:</p><blockquote><pre class="code">@lines = <STDIN>; # Read all the lineschomp(@lines); # discard all the newline characters</pre></blockquote><p>But the more common way to write that is with code similar to what weused earlier:</p><blockquote><pre class="code">chomp(@lines = <STDIN>); # Read the lines, not the newlines</pre></blockquote><p>Although you're welcome to write your code either way in theprivacy of your own cubicle, most Perl programmers will expect thesecond, more compact, notation.</p><p>It may be obvious to you (but it's not obvious to everyone)that once these lines of input have been read, they can't bere-read.<a href="#FOOTNOTE-95">[95]</a> Once you've reachedend-of-file, there's no more input out there to read.</p><blockquote class="footnote"> <a name="FOOTNOTE-95" /><p>[95]Well, yes, if the input is from a sourceupon which you can <tt class="literal">seek</tt>, then you'll be ableto go back and read again. But that's not what we'retalking about here.</p> </blockquote><p>And what happens if the input is coming from a 400MB log file? Theline input operator reads all of the lines, gobbling up lots ofmemory.<a href="#FOOTNOTE-96">[96]</a>Perl tries not to limit you in what you can do, but the other usersof your system (not to mention your system administrator) are likelyto object. If the input data is large, you should generally find away to deal with it without reading it all into memory at once.</p><blockquote class="footnote"> <a name="FOOTNOTE-96" /><p>[96]Typically, that's much more memory thanthe size of the file, too. That is, a 400MB file will typically takeup at least a full gigabyte of memory when read into an array. Thisis because Perl will generally waste memory to save time. This is agood tradeoff; if you're short of memory, you can buy more; ifyou're short on time, you're hosed.</p> </blockquote><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch03_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="ch03_10.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">3.8. Scalar and List Context</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">3.10. Exercises</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 + -
显示快捷键?