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

📄 ch19_01.htm

📁 编程珍珠,里面很多好用的代码,大家可以参考学习呵呵,
💻 HTM
📖 第 1 页 / 共 5 页
字号:
<html><head><title>The Command-Line Interface (Programming Perl)</title><!-- STYLESHEET --><link rel="stylesheet" type="text/css" href="../style/style1.css"><!-- METADATA --><!--Dublin Core Metadata--><meta name="DC.Creator" content=""><meta name="DC.Date" content=""><meta name="DC.Format" content="text/xml" scheme="MIME"><meta name="DC.Generator" content="XSLT stylesheet, xt by James Clark"><meta name="DC.Identifier" content=""><meta name="DC.Language" content="en-US"><meta name="DC.Publisher" content="O'Reilly &amp; Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="The Command-Line Interface"><meta name="DC.Type" content="Text.Monograph"></head><body><!-- START OF BODY --><!-- TOP BANNER --><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home"><map name="banner-map"><AREA SHAPE="RECT" COORDS="0,0,466,71" HREF="index.htm" ALT="Programming Perl"><AREA SHAPE="RECT" COORDS="467,0,514,18" HREF="jobjects/fsearch.htm" ALT="Search this book"></map><!-- TOP NAV BAR --><div class="navbar"><table width="515" border="0"><tr><td align="left" valign="top" width="172"><a href="ch18_07.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="part3.htm">Part 3: Perl as Technology</a></td><td align="right" valign="top" width="172"><a href="ch19_02.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h1 class="chapter">Chapter 19.  The Command-Line Interface</h1><div class="htmltoc"><h4 class="tochead">Contents:</h4><p><a href="ch19_01.htm">Command Processing</a><br><a href="ch19_02.htm">Environment Variables</a><br></p></div><p>This chapter is about aiming Perl in the right direction before youfire it off.  There are various ways to aim Perl, but the twoprimary ways are through switches on the command line and throughenvironment variables.  Switches are the more immediate and preciseway to aim a particular command.  Environment variables are moreoften used to set general policy.</p><h2 class="sect1">19.1. Command Processing</h2><p><a name="INDEX-3320"></a><a name="INDEX-3321"></a><a name="INDEX-3322"></a><a name="INDEX-3323"></a><a name="INDEX-3324"></a>It is fortunate that Perl grew up in the Unix world, because that meansits invocation syntax works pretty well under the command interpretersof other operating systems, too.  Most command interpreters know how todeal with a list of words as arguments and don't care if an argumentstarts with a minus sign.  There are, of course, some sticky spotswhere you'll get fouled up if you move from one system to another.  Youcan't use single quotes under MS-DOS as you do under Unix, forinstance.  And on systems like VMS, some wrapper code has to jumpthrough hoops to emulate Unix I/O redirection.  Wildcardinterpretation is a wildcard.  Once you get past those issues,however, Perl treats its switches and arguments much the same on anyoperating system.</p><p>Even when you don't have a command interpreter per se, it's easy toexecute a Perl program from another program written in any language.Not only can the calling program pass arguments in the ordinary way,it can also pass information via environment variables and, if youroperating system supports them, inherited file descriptors (see"Passing Filehandles" in <a href="ch16_01.htm">Chapter 16, "Interprocess Communication"</a>.  Even exoticargument-passing mechanisms can easily be encapsulated in a module,then brought into your Perl program via a simple<tt class="literal">use</tt> directive.</p><p><a name="INDEX-3325"></a><a name="INDEX-3326"></a>Perl parses command-line switches in the standardfashion.<a href="#FOOTNOTE-1">[1]</a> That is, it expects anyswitches (words beginning with a minus) to come first on the commandline.  After that usually comes the name of the script, followed byany additional arguments to be passed into the script.  Some of theseadditional arguments may themselves look like switches, but if so,they must be processed by the script, because Perl quits parsingswitches as soon as it sees a nonswitch, or the special"<tt class="literal">--</tt>" switch that says, "I am the lastswitch."</p><blockquote class="footnote"><a name="FOOTNOTE-1"></a><p>[1] Presuming you agree that Unix is bothstandard and fashionable.</p></blockquote><p><a name="INDEX-3327"></a>Perl gives you some flexibility in where you place the source codefor your program.  For small, quick-and-dirty jobs, you can programPerl entirely from the command line.  For larger, more permanentjobs, you can supply a Perl script as a separate file.  Perl looksfor a script to compile and run in any one of these three ways:</p><ol><li><p><a name="INDEX-3328"></a>Specified line by line via <tt class="userinput"><b>-e</b></tt>switches on the command line.  For example:</p><p><blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -e "print 'Hello, World.'"</b></tt>Hello, World.</pre></blockquote></p></li><li><p><a name="INDEX-3329"></a><a name="INDEX-3330"></a>Contained in the file specified by the first filename on the commandline.  Systems supporting the <tt class="literal">#!</tt> notation on the firstline of an executable script invoke interpreters this way on your behalf.<a name="INDEX-3331"></a></p></li><li><p><a name="INDEX-3332"></a>Passed in implicitly via standard input.  This method works only whenthere are no filename arguments; to pass arguments to a standard-inputscript you must use method 2, explicitly specifying a"<tt class="literal">-</tt>" for the script name.  Forexample:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>echo "print qq(Hello, @ARGV.)" | perl - World</b></tt>Hello, World.</pre></blockquote></p></li></ol><p><a name="INDEX-3333"></a><a name="INDEX-3334"></a>With methods 2 and 3, Perl starts parsing the input file from thebeginning--unless you've specified a <tt class="userinput"><b>-x</b></tt> switch,in which case it scans for the first line starting with<tt class="literal">#!</tt> and containing the word"<tt class="literal">perl</tt>", and starts there instead.  This is usefulfor running a script embedded in a larger message.  If so, you mightindicate the end of the script using the <tt class="literal">__END__</tt>token.</p><p>Whether or not you use <tt class="userinput"><b>-x</b></tt>, the<tt class="literal">#!</tt> line is always examined for switches when theline is parsed.  That way, if you're on a platform that allows onlyone argument with the <tt class="literal">#!</tt> line, or worse, doesn'teven recognize the <tt class="literal">#!</tt> line as special, you canstill get consistent switch behavior regardless of how Perl wasinvoked, even if <tt class="userinput"><b>-x</b></tt> was used to find thebeginning of the script.</p><p><a name="INDEX-3335"></a><a name="INDEX-3336"></a><a name="INDEX-3337"></a><a name="INDEX-3338"></a><a name="INDEX-3339"></a>Warning: because older versions of Unix silently chop off kernelinterpretation of the <tt class="literal">#!</tt> line after 32 characters,some switches may end up getting to your program intact, and othersnot; you could even get a "<tt class="literal">-</tt>" without its letter,if you're not careful.  You probably want to make sure that all yourswitches fall either before or after that 32-character boundary.  Mostswitches don't care whether they're processed redundantly, but gettinga "<tt class="literal">-</tt>" instead of a complete switch would cause Perlto try to read its source code from the standard input instead of fromyour script.  And a partial <tt class="userinput"><b>-I</b></tt> switch couldalso cause odd results.  However, some switches do care if they areprocessed twice, like combinations of <tt class="userinput"><b>-l</b></tt> and<tt class="userinput"><b>-0</b></tt>.  Either put all the switches after the32-character boundary (if applicable), or replace the use of<tt class="userinput"><b>-0</b></tt><em class="replaceable">DIGITS</em> with<tt class="literal">BEGIN{ $/ ="\0</tt><em class="replaceable">DIGITS</em><tt class="literal">";}</tt>.  Of course, if you're not on a Unix system, you'reguaranteed not to have this particular problem.</p><p>Parsing of <tt class="literal">#!</tt> switches starts from where"<tt class="literal">perl</tt>" is first mentioned in the line.  Thesequences "<tt class="literal">-*</tt>" and "<tt class="literal">-&nbsp;</tt>" arespecifically ignored for the benefit of <em class="emphasis">emacs</em>users, so that, if you're so inclined, you can say:<blockquote><pre class="programlisting">#!/bin/sh -- # -*- perl -*- -peval 'exec perl -S $0 ${1+"$@"}'    if 0;</pre></blockquote><a name="INDEX-3340"></a><a name="INDEX-3341"></a></p><p>and Perl will see only the <tt class="userinput"><b>-p</b></tt> switch.  Thefancy "<tt class="literal">-*- perl -*-</tt>" gizmo tells<em class="emphasis">emacs</em> to start up in Perl mode; you don't need itif you don't use <em class="emphasis">emacs</em>.  The<tt class="userinput"><b>-S</b></tt> mess is explained later under thedescription of that switch.</p><p><a name="INDEX-3342"></a>A similar trick involves the <em class="emphasis">env</em>(1)program, if you have it:<blockquote><pre class="programlisting">#!/usr/bin/env perl</pre></blockquote><a name="INDEX-3343"></a>The previous examples use a relative path to the Perl interpreter,getting whatever version is first in the user's path.  If you want aspecific version of Perl, say, <em class="emphasis">perl5.6.1</em>, placeit directly in the <tt class="literal">#!</tt> line's path, whether with the<em class="emphasis">env</em> program, with the <tt class="userinput"><b>-S</b></tt>mess, or with a regular <tt class="literal">#!</tt> processing.</p><p>If the <tt class="literal">#!</tt> line does <em class="emphasis">not</em>contain the word "<tt class="literal">perl</tt>", the program named afterthe <tt class="literal">#!</tt> is executed instead of the Perl interpreter.For example, suppose you have an ordinary Bourne shell script outthere that says:<blockquote><pre class="programlisting">#!/bin/shecho "I am a shell script"</pre></blockquote><a name="INDEX-3344"></a><a name="INDEX-3345"></a></p><p>If you feed that file to Perl, then Perl will run<em class="emphasis">/bin/sh</em> for you.  This is slightly bizarre, butit helps people on machines that don't recognize<tt class="literal">#!</tt>, because--by setting their<tt class="literal">SHELL</tt> environment variable--they can tell a program(such as a mailer) that their shell is<em class="emphasis">/usr/bin/perl</em>, and Perl will then dispatch theprogram to the correct interpreter for them, even though their kernelis too stupid to do so.</p><p>But back to Perl scripts that are really Perl scripts.  After locatingyour script, Perl compiles the entire program into an internal form(see <a href="ch18_01.htm">Chapter 18, "Compiling"</a>).  If anycompilation errors arise, execution does not even begin.  (This isunlike the typical shell script or command file, which might runpart-way through before finding a syntax error.) If the script issyntactically correct, it is executed.  If the script runs off the endwithout hitting an <tt class="literal">exit</tt> or <tt class="literal">die</tt>operator, an implicit <tt class="literal">exit(0)</tt> is supplied by Perlto indicate successful completion to your caller.  (This is unlike thetypical C program, where you're likely to get a random exit status ifyour program just terminates in the normal way.)</p><h3 class="sect2">19.1.1. <tt class="literal">#!</tt> and Quoting on Non-Unix Systems</h3><p><a name="INDEX-3346"></a>Unix's <tt class="literal">#!</tt> technique can be simulated on other systems:<a name="INDEX-3347"></a></p><dl><dt><b>Macintosh</b></dt><dd><p><a name="INDEX-3348"></a>A Perl program on a Macintosh will have the appropriate Creator andType, so that double-clicking them will invoke the Perl application.</p></dd><dt><b>MS-DOS</b></dt><dd><p>Create a batch file to run your program, and codify it in<tt class="literal">ALTERNATIVE_SHEBANG</tt>.  See the<em class="emphasis">dosish.h</em> file in the top level of the Perl sourcedistribution for more information about this.<a name="INDEX-3349"></a><a name="INDEX-3350"></a></p></dd><dt><b>OS/2</b></dt><dd><p>Put this line:

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -