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

📄 ch06_02.htm

📁 by Randal L. Schwartz and Tom Phoenix ISBN 0-596-00132-0 Third Edition, published July 2001. (See
💻 HTM
字号:
<html><head><title>Input from the Diamond 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="ch06_01.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_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">6.2. Input from the Diamond Operator</h2><p><a name="INDEX-431" /> <a name="INDEX-432" /> <a name="INDEX-433" />Another wayto read input is with the diamond<a href="#FOOTNOTE-143">[143]</a> operator:<tt class="literal">&lt;&gt;</tt>. This is useful for making programs thatwork like standard<a name="INDEX-435" />Unix<a href="#FOOTNOTE-144">[144]</a> utilities, with respect to the<a name="INDEX-436" />invocation arguments (whichwe'll see in a moment). If you want to make a Perl program thatcan be used like the utilities <i class="command">cat</i>,<i class="command">sed</i>, <i class="command">awk</i>,<i class="command">sort</i>, <i class="command">grep</i>,<i class="command">lpr</i>, and many others, the diamond operator will beyour friend. If you want to make anything else, the diamond operatorprobably won't help.</p><blockquote class="footnote"> <a name="FOOTNOTE-143" /><p>[143]The diamondoperator was named by Larry's daughter,<a name="INDEX-434" />Heidi, whenRandal went over to Larry's house one day to show off the newtraining materials he'd been writing, and complained that therewas no spoken name for "that thing". Larry didn'thave a name for it, either. Heidi (eight years old at the time)quickly chimed in, "That's a diamond, Daddy." Sothe name stuck. Thanks, Heidi!</p> </blockquote><blockquote class="footnote"> <a name="FOOTNOTE-144" /><p>[144]But not just on Unixsystems. Many other systems have adopted this way of using invocationarguments.</p> </blockquote><p>The <em class="firstterm">invocation arguments</em> to a program arenormally a number of "words" on the command line afterthe name of the program.<a href="#FOOTNOTE-145">[145]</a> In this case, they give the names of a number of files tobe processed in sequence:</p><blockquote class="footnote"> <a name="FOOTNOTE-145" /><p>[145]Whenever a program isstarted, it has a list of zero or more invocation arguments, suppliedby whatever program is starting it. Often this is the shell, whichmakes up the list depending upon what you type on the command line.But we'll see in a later chapter that you can invoke a programwith pretty much any strings as the invocation arguments. Becausethey often come from the shell's command line, they aresometimes called "command-line arguments" as well.</p></blockquote><blockquote><pre class="code">$ <tt class="userinput"><b>./my_program fred barney betty</b></tt></pre></blockquote><p>That command means to run the command <i class="command">my_program</i>(which will be found in the current directory), and that it shouldprocess file <em class="emphasis">fred</em>, followed by file<em class="emphasis">barney</em>, followed by file<em class="emphasis">betty</em>.</p><p>If you give no invocation arguments, the program should process thestandard input stream. Or, as a special case, if you give just a<a name="INDEX-437" /><a name="INDEX-438" />hyphen as one of thearguments, that means standard input as well.<a href="#FOOTNOTE-146">[146]</a> So, if the invocationarguments had been <tt class="literal">fred - betty</tt>, that would havemeant that the program should process file <em class="emphasis">fred</em>,followed by the standard input stream, followed by file<em class="emphasis">betty</em>.</p><blockquote class="footnote"><a name="FOOTNOTE-146" /><p>[146]Here's a possibly unfamilar Unix fact: most of thosestandard utilities, like <i class="command">cat </i>and <i class="command">sed</i>use this same convention, where a hyphen stands for thestandard input stream. </p> </blockquote><p>The benefit of making your programs work like this is that you maychoose where the program gets its input at run time; for example, youwon't have to rewrite the program to use it in a pipeline(which we'll discuss more later). Larry put this feature intoPerl because he wanted to make it easy for you to write your ownprograms that work like standard Unix utilities -- even onnon-Unix machines. Actually, he did it so he could make his<em class="emphasis">own</em> programs work like standard Unix utilities;since some vendors' utilities don't work just likeothers', Larry could make his own utilities, deploy them on anumber of machines, and know that they'd all have the samebehavior. Of course, this meant porting Perl to every machine hecould find.</p><p>The diamond operator is actually a special kind of<a name="INDEX-439" />line-input operator. But instead ofgetting the input from the keyboard, it comes from the user'schoice of input:<a href="#FOOTNOTE-147">[147]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-147" /><p>[147]Which may or may not include gettinginput from the keyboard.</p> </blockquote><blockquote><pre class="code">while (defined($line = &lt;&gt;)) {  chomp($line);  print "It was $line that I saw!\n";}</pre></blockquote><p>So, if we run this program with the invocation arguments<tt class="literal">fred</tt>, <tt class="literal">barney</tt>, and<tt class="literal">betty</tt>, it will say something like: "It was[a line from file <em class="filename">fred</em>] that I saw!","It was [another line from file <em class="filename">fred</em>] thatI saw!", on and on until it reaches the end of file<tt class="literal">fred</tt>. Then, it will automatically go on to file<tt class="literal">barney</tt>, printing out one line after another, andthen on to file <tt class="literal">betty</tt>. Note that there's nobreak when we go from one file to another; when you use the diamond,it's as if the input files have been merged into one bigfile.<a href="#FOOTNOTE-148">[148]</a> The diamond willreturn <tt class="literal">undef</tt> (and we'll drop out of the<tt class="literal">while</tt> loop) only at the end of all of the input.</p><blockquote class="footnote"> <a name="FOOTNOTE-148" /><p>[148]If it matters to you, or even if itdoesn't, the current file's name is kept in Perl'sspecial variable <tt class="literal">$ARGV</tt><a name="INDEX-440" />. This name may be <tt class="literal">"-"</tt>instead of a real filename if the input is coming from thestandard input stream, though.</p> </blockquote><p>In fact, since this is just a special kind of line-input operator, wemay use the same shortcut we saw earlier, to read the input into<tt class="literal">$_</tt> by default:</p><blockquote><pre class="code">while (&lt;&gt;) {  chomp;  print "It was $_ that I saw!\n";}</pre></blockquote><p>This works like the loop above, but with less typing. And you mayhave noticed that we're using the default for<tt class="literal">chomp</tt>; without an argument,<tt class="literal">chomp</tt> will work on <tt class="literal">$_</tt>. Everylittle bit of saved typing helps!</p><p>Since the diamond operator is generally being used to process all ofthe input, it's typically a mistake to use it in more than oneplace in your program. If you find yourself putting two diamonds intothe same program, especially using the second diamond inside the<tt class="literal">while</tt> loop that is reading from the first one,it's almost certainly not going to do what you wouldlike.<a href="#FOOTNOTE-149">[149]</a> In our experience, when beginners put asecond diamond into a program, they meant to use<tt class="literal">$_</tt> instead. Remember, the diamond operator<em class="emphasis">reads</em> the input, but the input itself is(generally, by default) found in <tt class="literal">$_</tt>.</p><blockquote class="footnote"> <a name="FOOTNOTE-149" /><p>[149]If you re-initialize <tt class="literal">@ARGV</tt>before using the second diamond, then you're on solidground. We'll see <tt class="literal">@ARGV</tt> in the nextsection.</p> </blockquote><p>If the diamond operator can't open one of the files and readfrom it, it'll print an allegedly helpful<a name="INDEX-441" />diagnostic message, such as:</p><a name="INDEX-442" /><blockquote><pre class="code">can't open wimla: No such file or directory</pre></blockquote><p>The diamond operator will then go on to the next file automatically,much like what you'd expect from <i class="command">cat</i> oranother standard utility.<a name="INDEX-443" /> <a name="INDEX-444" /> </p><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch06_01.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_03.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">6. I/O Basics</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.3. The Invocation Arguments</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 + -