📄 ch19_01.htm
字号:
<blockquote><pre class="programlisting">extproc perl -S -your_switches</pre></blockquote>as the first line in <em class="emphasis">*.cmd</em> file (<tt class="userinput"><b>-S</b></tt>works around a bug in <em class="emphasis">cmd.exe</em>'s"<tt class="literal">extproc</tt>" handling).<a name="INDEX-3351"></a></p></dd><dt><b>VMS</b></dt><dd><p><a name="INDEX-3352"></a>Put these lines:<blockquote><pre class="programlisting">% perl <em class="replaceable">-mysw</em> 'f$env("procedure")' 'p1' 'p2' 'p3' 'p4' 'p5' 'p6' 'p7' 'p8' !$ exit++ + ++$status != 0 and $exit = $status = undef;</pre></blockquote>at the top of your program, where <em class="replaceable">-mysw</em> areany command-line switches you want to pass to Perl. You can nowinvoke the program directly by typing <tt class="literal">perl program</tt>,as a DCL procedure by saying <tt class="literal">@program</tt>, orimplicitly via <tt class="literal">DCL$PATH</tt> by using just the name ofthe program. This incantation is a bit much to remember, but Perlwill display it for you if you type in <tt class="literal">perl"-V:startperl"</tt>. If you can't remember that--well,that's why you bought this book.</p></dd><dt><b>Win??</b></dt><dd><p>When using the ActiveStatedistribution of Perl under some variant of Microsoft's Windows suiteof operating systems (that is, Win95, Win98, Win00,<a href="#FOOTNOTE-2">[2]</a> WinNT, butnot Win3.1), the installation procedure for Perl modifies the WindowsRegistry to associate the <em class="emphasis">.pl</em> extension with thePerl interpreter.<a name="INDEX-3353"></a><a name="INDEX-3354"></a></p><blockquote class="footnote"><a name="FOOTNOTE-2"></a><p>[2]Er, pardon the technical difficulties...</p></blockquote><p>If you install another port of Perl, including the one in the Win32directory of the Perl distribution, then you'll have to modify theWindows Registry yourself.</p><p>Note that using a <em class="emphasis">.pl</em> extension means you can nolonger tell the difference between an executable Perl program and a"perl library" file. You could use <em class="emphasis">.plx</em> for aPerl program instead to avoid this. This is much less of an issuethese days, since most Perl modules are now in<em class="emphasis">.pm</em> files.</p></dd></dl><p><a name="INDEX-3355"></a><a name="INDEX-3356"></a>Command interpreters on non-Unix systems often have extraordinarilydifferent ideas about quoting than Unix shells have. You'll need tolearn the special characters in your command interpreter(<tt class="literal">*</tt>, <tt class="literal">\</tt>, and <tt class="literal">"</tt>are common) and how to protect whitespace and these special charactersto run one-liners via the <tt class="userinput"><b>-e</b></tt> switch. You mightalso have to change a single <tt class="literal">%</tt> to a<tt class="literal">%%</tt>, or otherwise escape it, if that's a specialcharacter for your shell.</p><p>On some systems, you may have to change single quotes to doublequotes. But don't do that on Unix or Plan9 systems, or anythingrunning a Unix-style shell, such as systems from the MKS Toolkit or fromthe Cygwin package produced by the Cygnus folks, now at Redhat.Microsoft's new Unix emulator called Interix is also starting to look,ahem, interixing.</p><p>For example, on Unix and Mac OS X, use:</p><blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -e 'print "Hello world\n"'</b></tt></pre></blockquote><p>On Macintosh (pre Mac OS X), use:</p><blockquote><pre class="programlisting">print "Hello world\n"</pre></blockquote><p>then run "Myscript" or Shift-Command-R.</p><p>On VMS, use:</p><blockquote><pre class="programlisting"><tt class="computeroutput">$</tt> <tt class="userinput"><b>perl -e "print ""Hello world\n"""</b></tt></pre></blockquote><p>or again with <tt class="literal">qq//</tt>:</p><blockquote><pre class="programlisting"><tt class="computeroutput">$</tt> <tt class="userinput"><b>perl -e "print qq(Hello world\n)"</b></tt></pre></blockquote><p>And on MS-DOS et al., use:</p><blockquote><pre class="programlisting">A:> <tt class="userinput"><b>perl -e "print \"Hello world\n\""</b></tt></pre></blockquote><p>or use <tt class="literal">qq//</tt> to pick your own quotes:</p><blockquote><pre class="programlisting">A:> <tt class="userinput"><b>perl -e "print qq(Hello world\n)"</b></tt></pre></blockquote><p>The problem is that neither of those is reliable: it depends on thecommand interpreter you're using there. If <em class="emphasis">4DOS</em>were the command shell, this would probably work better:<blockquote><pre class="programlisting">perl -e "print <Ctrl-x>"Hello world\n<Ctrl-x>""</pre></blockquote><a name="INDEX-3357"></a>The <em class="emphasis">CMD.EXE</em> program seen on Windows NT seems tohave slipped a lot of standard Unix shell functionality in when nobodywas looking, but just try to find documentation for its quoting rules.</p><p><a name="INDEX-3358"></a><a name="INDEX-3359"></a>On the Macintosh,<a href="#FOOTNOTE-3">[3]</a> all thisdepends on which environment you are using. The MacPerl shell, orMPW, is much like Unix shells in its support for several quotingvariants, except that it makes free use of the Macintosh's non-ASCIIcharacters as control characters.</p><blockquote class="footnote"><a name="FOOTNOTE-3"></a><p>[3]At least, prior to release of Mac OS X,which, happily enough, is a BSD-derived system.</p></blockquote><p>There is no general solution to all of this. It's just a mess.If you aren't on a Unix system but want to docommand-line things, your best bet is to acquire a better commandinterpreter than the one your vendor supplied you, which shouldn'tbe too hard.</p><p>Or just write it all in Perl, and forget the one-liners.</p><h3 class="sect2">19.1.2. Location of Perl</h3><p><a name="INDEX-3360"></a><a name="INDEX-3361"></a>Although this may seem obvious, Perl is useful only when userscan easily find it. When possible, it's good for both <em class="emphasis">/usr/bin/perl</em>and <em class="emphasis">/usr/local/bin/perl</em> to be symlinks to the actual binary. Ifthat can't be done, system administrators are strongly encouragedto put Perl and its accompanying utilities into adirectory typically found along a user's standard <tt class="literal">PATH</tt>, or insome other obvious and convenient place.</p><p>In this book, we use the standard <tt class="literal">#!/usr/bin/perl</tt>notation on the first line of the program to mean whatever particularmechanism works on your system. If you care about running a specificversion of Perl, use a specific path:<blockquote><pre class="programlisting">#!/usr/local/bin/perl5.6.0</pre></blockquote><a name="INDEX-3362"></a>If you just want to be running <em class="emphasis">at least</em> someversion number, but don't mind higher ones, place a statement likethis near the top of your program:<blockquote><pre class="programlisting">use v5.6.0;</pre></blockquote>(Note: earlier versions of Perl use numbers like 5.005 or 5.004_05.Nowadays we would think of those as 5.5.0 and 5.4.5, but versionsof Perl older than 5.6.0 won't understand that notation.)</p><h3 class="sect2">19.1.3. Switches</h3><p><a name="INDEX-3363"></a><a name="INDEX-3364"></a><a name="INDEX-3365"></a><a name="INDEX-3366"></a>A single-character command-line <em class="emphasis">switch</em> without its own argument mayalways be combined (bundled) with a switch following it.<blockquote><pre class="programlisting">#!/usr/bin/perl -spi.bak # same as -s -p -i.bak</pre></blockquote>Switches are also known as options or flags. Whatever you call them,here are the ones Perl recognizes:</p><dl><dt><b><span class="option">--</span></b></dt><dd><p><a name="INDEX-3367"></a><a name="INDEX-3368"></a>Terminates switch processing, even if the next argument starts with aminus. It has no other effect.</p></dd><dt><b><span class="option">-0</span><em class="replaceable">OCTNUM</em></b></dt><dt><b><span class="option">-0</span></b></dt><dd><p><a name="INDEX-3369"></a><a name="INDEX-3370"></a><a name="INDEX-3371"></a><a name="INDEX-3372"></a><a name="INDEX-3373"></a>Specifies the record separator (<tt class="literal">$/</tt>) as an octalnumber. If <em class="replaceable">OCTNUM</em> is not present, the NULcharacter (that's ASCII character 0, Perl's <tt class="literal">"\0"</tt>)is the separator. Other switches may precede or follow the octalnumber. For example, if you have a version of<em class="emphasis">find</em>(1) that can print filenamesterminated by the null character, you can say this:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>find . -name '*.bak' -print0 | perl -n0e unlink</b></tt></pre></blockquote></p><p><a name="INDEX-3374"></a><a name="INDEX-3375"></a>The special value <tt class="literal">00</tt> makes Perl read files inparagraph mode, equivalent to setting the <tt class="literal">$/</tt>variable to <tt class="literal">""</tt>. The value <tt class="literal">0777</tt>makes Perl slurp in whole files at once. This is equivalent toundefining the <tt class="literal">$/</tt> variable. We use<tt class="literal">0777</tt> since there is no ASCII character with thatvalue. (Unfortunately, there <em class="emphasis">is</em> a Unicodecharacter with that value, <tt class="literal">\N{LATIN SMALL LETTER O WITHSTROKE AND ACUTE}</tt>, but something tells us you won't bedelimiting your records with that.)</p></dd><dt><b><span class="option">-a</span></b></dt><dd><p><a name="INDEX-3376"></a><a name="INDEX-3377"></a><a name="INDEX-3378"></a>Turns on autosplit mode, but only when used with <span class="option">-n</span>or <span class="option">-p</span>. An implicit <tt class="literal">split</tt> commandto the <tt class="literal">@F</tt> array is done as the first thing insidethe implicit <tt class="literal">while</tt> loop produced by the<span class="option">-n</span> and <span class="option">-p</span> switches. So:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -ane 'print pop(@F), "\n";'</b></tt></pre></blockquote>is equivalent to:<blockquote><pre class="programlisting">LINE: while (<>) { @F = split(' '); print pop(@F), "\n";}</pre></blockquote><a name="INDEX-3379"></a><a name="INDEX-3380"></a><a name="INDEX-3381"></a>A different field separator may be specified by passing a regularexpression for <tt class="literal">split</tt> to the <span class="option">-F</span>switch. For example, these two calls are equivalent:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>awk -F: '$7 && $7 !~ /^\/bin/' /etc/passwd</b></tt><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -F: -lane 'print if $F[6] && $F[6] !~ m(^/bin)' /etc/passwd</b></tt></pre></blockquote></p></dd><dt><b><span class="option">-c</span></b></dt><dd><p><a name="INDEX-3382"></a><a name="INDEX-3383"></a><a name="INDEX-3384"></a><a name="INDEX-3385"></a>Causes Perl to check the syntax of the script and then exit withoutexecuting what it's just compiled. Technically, it does a bit morethan that: it will execute any <tt class="literal">BEGIN</tt> or<tt class="literal">CHECK</tt> blocks and any <tt class="literal">use</tt>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -