ch06_03.htm

来自「by Randal L. Schwartz and Tom Phoenix I」· HTM 代码 · 共 80 行

HTM
80
字号
<html><head><title>The Invocation Arguments (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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr></table></div><h2 class="sect1">6.3. The Invocation Arguments</h2><p>Technically, the diamond operator isn't looking literally atthe <a name="INDEX-445" />invocation arguments -- it worksfrom the <tt class="literal">@ARGV</tt><a name="INDEX-446" /> <a name="INDEX-447" /> array.This array is a special array that is preset by the Perl interpreterto be a list of the invocation arguments. In other words, this isjust like any other array, (except for its funny, all-caps name), butwhen your program starts, <tt class="literal">@ARGV</tt> is already stuffedfull of the list of invocation arguments.<a href="#FOOTNOTE-150">[150]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-150" /><p>[150]Cprogrammers may be wondering about<tt class="literal">argc</tt><a name="INDEX-448" />(there isn't one in Perl), and what happened to theprogram's own name (that's found in Perl's specialvariable <tt class="literal">$0</tt><a name="INDEX-449" />, not <tt class="literal">@ARGV</tt>). Also,depending upon how you've invoked your program, there may be alittle more happening than we say here. See the<em class="emphasis">perlrun</em> manpage for the fulldetails.</p> </blockquote><p>You can use <tt class="literal">@ARGV</tt> just like any other array; youcould <tt class="literal">shift</tt> items off of it, perhaps, or use<tt class="literal">foreach</tt> to iterate over it. You could even checkto see if any arguments start with a hyphen, so that you couldprocess them as invocation options (like Perl does with its own<tt class="literal">-w</tt> option).<a href="#FOOTNOTE-151">[151]</a></p><blockquote class="footnote"> <a name="FOOTNOTE-151" /><p>[151]If you need more thanjust one or two such options, you should almost certainly use amodule to process them in a standard way. See the documentation forthe <tt class="literal">Getopt::Long</tt> and <tt class="literal">Getopt::Std</tt> modules, which are part of the standarddistribution.</p> </blockquote><p>This is how the diamond operator knows what filenames it should use:it looks in <tt class="literal">@ARGV</tt>. If it finds an empty list, ituses the standard input stream; otherwise it uses the list of filesthat it finds. This means that after your program starts and beforeyou start using the diamond, you've got a chance to tinker with<tt class="literal">@ARGV</tt>. For example, here we can process threespecific files, regardless of what the user chose on the commandline:</p><blockquote><pre class="code">@ARGV = qw# larry moe curly #;  # force these three files to be readwhile (&lt;&gt;) {  chomp;  print "It was $_ that I saw in some stooge-like file!\n";}</pre></blockquote><p>In <a href="ch11_01.htm">Chapter 11, "Filehandles and File Tests"</a>, we'll see how to open andclose specific filenames at specific times. But this technique willsuffice for the next few chapters.<a name="INDEX-450" /> </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_02.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_04.htm"><img alt="Next" border="0" src="../gifs/txtnexta.gif" /></a></td></tr><tr><td align="left" valign="top" width="228">6.2. Input from the Diamond Operator</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.4. Output to Standard Output</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 + =
减小字号Ctrl + -
显示快捷键?