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

📄 ch06_01.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>I/O Basics (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="ch05_05.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="ch06_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div>




<h1 class="chapter">Chapter 6. I/O Basics</h1>
<div class="htmltoc"><h4 class="tochead">Contents:</h4>
  <p> <a href="#lperl3-CHP-6-SECT-1">Input from Standard Input</a><br />
<a href="ch06_02.htm">Input from the Diamond Operator</a><br />
<a href="ch06_03.htm">The Invocation Arguments</a><br />
<a href="ch06_04.htm">Output to Standard Output</a><br />
<a href="ch06_05.htm">Formatted Output with printf</a><br />
<a href="ch06_06.htm">Exercises</a><br /></p></div>

<p><a name="INDEX-419" /></a> <a name="INDEX-420" /></a> <a name="INDEX-421" /></a> <a name="INDEX-422" /></a>We've <a name="INDEX-423" /></a>already
<a name="INDEX-424" /></a>seen how to do some
input/output (I/O), in order to make some of the earlier exercises
possible. But now we'll learn a little more about those
operations. As the title of this chapter implies, there will be more
about Perl's I/O operations in <a href="ch11_01.htm">Chapter 11, "Filehandles and File Tests"</a>.
</p>

<div class="sect1"><a name="lperl3-CHP-6-SECT-1" /></a>
<h2 class="sect1">6.1. Input from Standard Input</h2>

<p>Reading from the standard input stream is easy.<a href="#FOOTNOTE-139">[139]</a> We've been doing it already with the
<tt class="literal">&lt;STDIN&gt;</tt> operator.<a href="#FOOTNOTE-140">[140]</a>
Evaluating this operator in a scalar context gives you the next line
of input:
</p><blockquote class="footnote"> <a name="FOOTNOTE-139" /></a><p>[139]If
you're already familiar with the workings of standard input,
output, and error streams, you're ahead of the game. If not,
we'll get you caught up when we get to <a href="ch14_01.htm">Chapter 14, "Process Management"</a>. For now, just think of "standard
input" as being "the keyboard," and "standard
output" as being "the display screen."</p>
</blockquote><blockquote class="footnote"> <a name="FOOTNOTE-140" /></a><p>[140]What
we're calling the line-input operator here,
<tt class="literal">&lt;STDIN&gt;</tt>, is actually a line-input operator
(represented by the angle brackets) around a
<em class="firstterm">filehandle</em>. We'll learn about
filehandles in <a href="ch11_01.htm">Chapter 11, "Filehandles and File Tests"</a>.</p> </blockquote>

<blockquote><pre class="code">$line = &lt;STDIN&gt;;                # read the next line
chomp($line);                   # and chomp it

chomp($line = &lt;STDIN&gt;);         # same thing, more idiomatically</pre></blockquote>

<p>Since the line-input operator will return <tt class="literal">undef</tt>
when you reach end-of-file, this is handy for dropping out of loops:
</p>

<blockquote><pre class="code">while (defined($line = &lt;STDIN&gt;)) {
  print "I saw $line";
{</pre></blockquote>

<p><a name="INDEX-425" /></a>
<a name="INDEX-426" /></a>There's a
lot going on in that first line: we're reading the input into a
variable, checking that it's defined, and if it is (meaning
that we haven't reached the end of the input) we're
running the body of the <tt class="literal">while</tt> loop. So, inside the
body of the loop, we'll see each line, one after another, in
<tt class="literal">$line</tt>.<a href="#FOOTNOTE-141">[141]</a> This is something you'll want to do
fairly often, so naturally Perl has a shortcut for it. The shortcut
looks like this:
</p><blockquote class="footnote"> <a name="FOOTNOTE-141" /></a><p>[141]You probably noticed that
we never chomped that input. In this kind of a loop, you can't
really put <tt class="literal">chomp </tt>into the conditional
expression, so it's often the first item in the loop body, when
it's needed. We'll see examples of that in the next
section.</p> </blockquote>

<blockquote><pre class="code">while (&lt;STDIN&gt;) {
  print "I saw $_";
}</pre></blockquote>

<p>Now, to make this shortcut, Larry chose some useless syntax. That is,
this is <em class="emphasis">literally</em> saying, "Read a line of
input, and see if it's true. (Normally it is.) And if it is
true, enter the <tt class="literal">while</tt> loop, but <em class="emphasis">throw
away that line of input!</em>" Larry knew that it was a
useless thing to do; nobody should ever need to do that in a real
Perl program. So, Larry took this useless syntax and made it useful.
</p>

<p>What this is <em class="emphasis">actually</em> saying is that Perl should
do the same thing as we saw in our earlier loop: it tells Perl to
read the input into a variable, and (as long as the result was
defined, so we haven't reached end-of file) then enter the
<tt class="literal">while</tt> loop. However, instead of storing the input
into <tt class="literal">$line</tt>, Perl will use its favorite default
variable, <tt class="literal">$_</tt>, just as if you had written this:
</p>

<blockquote><pre class="code">while (defined($_ = &lt;STDIN&gt;)) {
  print "I saw $_";
}</pre></blockquote>

<p>Now, before we go any further, we must be very clear about something:
this shortcut works <em class="emphasis">only</em> if you write it just as
we did. If you put a line-input operator anywhere else (in
particular, as a statement all on its own) it won't read a line
into <tt class="literal">$_</tt> by default. It works
<em class="emphasis">only</em> if there's nothing but the line-input
operator in the conditional of a <tt class="literal">while</tt>
loop.<a href="#FOOTNOTE-142">[142]</a> If you put
anything else into the conditional expression, this shortcut
won't apply.
</p><blockquote class="footnote"> <a name="FOOTNOTE-142" /></a><p>[142]Well, okay, the conditional of a <tt class="literal">for</tt>
loop is just a <tt class="literal">while</tt> conditional in
disguise, so it works there, too.</p> </blockquote>

<p>There's no connection between the line-input operator
(<tt class="literal">&lt;STDIN&gt;</tt><a name="INDEX-427" /></a>) and Perl's favorite
<a name="INDEX-428" /></a>default variable
(<tt class="literal">$_</tt>). In this case, though, it just happens that
the input is being stored in that variable.
</p>

<p>On the other hand, evaluating the line-input operator in a list
context gives you all of the (remaining) lines of input as a
list -- each element of the list is one line:
</p>

<blockquote><pre class="code">foreach (&lt;STDIN&gt;) {
  print "I saw $_";
}</pre></blockquote>

<p>Once again, there's no connection between the line-input
operator and Perl's favorite default variable. In this case,
though, the default control variable for <tt class="literal">foreach</tt>
is <tt class="literal">$_</tt>. So in this loop, we'll see each line
of input in <tt class="literal">$_</tt>, one after the other.
</p>

<p>That may sound familiar, and for good reason: That's the same
behavior as the <tt class="literal">while</tt> loop would do. Isn't
it?
</p>

<p>The difference is under the hood. In the <tt class="literal">while</tt>
loop, Perl reads a line of input, puts it into a variable, and runs
the body of the loop. Then, it goes back to find another line of
input. But in the <tt class="literal">foreach</tt> loop, the line-input
operator is being used in a list context (since
<tt class="literal">foreach</tt> needs a list to iterate through). So it
has to read all of the input before the loop can start running. That
difference will become apparent when the input is coming from your
400MB web server log file! It's generally best to use code like
the <tt class="literal">while</tt> loop's shortcut, which will
process input a line at a time, whenever
possible.<a name="INDEX-429" /></a> <a name="INDEX-430" /></a> 
</p>

</div>












<hr width="684" align="left" />
<div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch05_05.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="ch06_02.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">5.5. Exercises</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">6.2. Input from the Diamond Operator</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 + -