📄 ch19_01.htm
字号:
If the switch is of the form <span class="option">-xxx=yyy</span>, the<tt class="literal">$xxx</tt> variable is set to whatever follows the equalssign in that argument ("<tt class="literal">yyy</tt>" in this case). Thefollowing script prints "<tt class="literal">true</tt>" if and only if thescript is invoked with a <tt class="literal">-foo=bar</tt> switch.<blockquote><pre class="programlisting">#!/usr/bin/perl -sif ($foo eq 'bar') { print "true\n" }</pre></blockquote></p></dd><dt><b><span class="option">-S</span></b></dt><dd><p><a name="INDEX-3437"></a><a name="INDEX-3438"></a><a name="INDEX-3439"></a><a name="INDEX-3440"></a><a name="INDEX-3441"></a>Makes Perl use the <tt class="literal">PATH</tt> environment variable tosearch for the script (unless the name of the script containsdirectory separators).</p><p>Typically, this switch is used to help emulate<tt class="literal">#!</tt> startup on platforms that don't support<tt class="literal">#!</tt>. Onmany platforms that have a shell compatible with Bourne or C shell,you can use this:<blockquote><pre class="programlisting">#!/usr/bin/perleval "exec /usr/bin/perl -S $0 $*" if $running_under_some_shell;</pre></blockquote>The system ignores the first line and feeds the script to<em class="emphasis">/bin/sh</em>, which proceeds to try to execute thePerl script as a shell script. The shell executes the second line asa normal shell command, and thus starts up the Perl interpreter. Onsome systems, <tt class="literal">$0</tt> doesn't always contain the fullpathname, so <span class="option">-S</span> tells Perl to search for the scriptif necessary. After Perl locates the script, it parses the lines andignores them because the variable<tt class="literal">$running_under_some_shell</tt> is never true. A betterconstruct than <tt class="literal">$*</tt> would be<tt class="literal">${1+"$@"}</tt>, which handles embedded spaces and suchin the filenames but doesn't work if the script is being interpretedby <em class="emphasis">csh</em>. In order to start up<em class="emphasis">sh</em> instead of <em class="emphasis">csh</em>, somesystems have to replace the <tt class="literal">#!</tt> line with a linecontaining just a colon, which Perl will politely ignore. Othersystems can't control that and need a totally devious construct thatwill work under any of <em class="emphasis">csh</em>,<em class="emphasis">sh</em>, or <em class="emphasis">perl</em>, such as thefollowing:<blockquote><pre class="programlisting">eval '(exit $?0)' && eval 'exec /usr/bin/perl -S $0 ${1+"$@"}' & eval 'exec /usr/bin/perl -S $0 $argv:q' if 0;</pre></blockquote>Yes, it's ugly, but so are the systems that work<a href="#FOOTNOTE-5">[5]</a> this way.</p><blockquote class="footnote"><a name="FOOTNOTE-5"></a><p>[5] Weuse the term advisedly.</p></blockquote><p>On some platforms, the <span class="option">-S</span> switch also makes Perlappend suffixes to the filename while searching for it. For example,on Win32 platforms, the <em class="emphasis">.bat</em> and<em class="emphasis">.cmd</em> suffixes are appended if a lookup for theoriginal name fails and the name does not already end in one of thosesuffixes. If your Perl was built with debugging enabled, you can usePerl's <span class="option">-Dp</span> switch to watch how the search progresses.</p><p>If the filename supplied contains directory separators (even asjust a relative pathname, not an absolute one), and if the file isnot found, those platforms that implicitly append file extensions(not Unix) will do so and look for the file with thoseextensions added, one by one.</p><p>On DOS-like platforms, if the script does not contain directoryseparators, it will first be searched for in the current directorybefore being searched for in the <tt class="literal">PATH</tt>. On Unixplatforms, the script will be searched for strictly on the<tt class="literal">PATH</tt>, due to security concerns about accidentallyexecuting something in the current working directory withoutexplicitly requesting this.</p></dd><dt><b><span class="option">-T</span></b></dt><dd><p><a name="INDEX-3442"></a><a name="INDEX-3443"></a><a name="INDEX-3444"></a>Forces "taint" checks to be turned on so you can test them. Ordinarilythese checks are done only when running setuid or setgid. It's a goodidea to turn them on explicitly for programs run on another's behalf,such as CGI programs. See <a href="ch23_01.htm">Chapter 23, "Security"</a>.</p><p>Note that, for security reasons, Perl must see this optionquite early; usually this means it must appear early on the commandline or in the <tt class="literal">#!</tt> line. If it's not early enough, Perl complains.</p></dd><dt><b><span class="option">-u</span></b></dt><dd><p><a name="INDEX-3445"></a><a name="INDEX-3446"></a><a name="INDEX-3447"></a><a name="INDEX-3448"></a><a name="INDEX-3449"></a>Causes Perl to dump core after compiling your script. You can then intheory take this core dump and turn it into an executable file byusing the <em class="emphasis">undump</em> program (not supplied). Thisspeeds startup at the expense of some disk space (which you canminimize by stripping the executable). If you want to execute aportion of your script before dumping, use Perl's<tt class="literal">dump</tt> operator instead. Note: availability of<em class="emphasis">undump</em> is platform specific; it may not beavailable for a specific port of Perl. It has been superseded by thenew Perl-to-C code generator, which is much more portable (but stillexperimental).</p></dd><dt><b><span class="option">-U</span></b></dt><dd><p><a name="INDEX-3450"></a><a name="INDEX-3451"></a><a name="INDEX-3452"></a>Allows Perl to do unsafe operations. Currently the only "unsafe"operations are unlinking directories while running assuperuser, and running setuid programs with fatal taint checksturned into warnings. Note that warnings must be enabled to actually producethe taint-check warnings.</p></dd><dt><b><span class="option">-v</span></b></dt><dd><p><a name="INDEX-3453"></a><a name="INDEX-3454"></a>Prints the version and patch level of your Perl executable, alongwith a bit of extra information.</p></dd><dt><b><span class="option">-V</span></b></dt><dd><p><a name="INDEX-3455"></a><a name="INDEX-3456"></a>Prints a summary of the major Perl configuration values and the currentvalue of <tt class="literal">@INC</tt>.</p></dd><dt><b><span class="option">-V:</span><em class="replaceable">NAME</em></b></dt><dd><p>Prints to <tt class="literal">STDOUT</tt> the value of the named configuration variable.The <em class="replaceable">NAME</em> may contain regex characters, like "<tt class="literal">.</tt>" to match anycharacter, or "<tt class="literal">.*</tt>" to match any optional sequence of characters.<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -V:man.dir</b></tt>man1dir='/usr/local/man/man1'man3dir='/usr/local/man/man3'<tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -V:'.*threads'</b></tt>d_oldpthreads='undef'use5005threads='define'useithreads='undef'usethreads='define'</pre></blockquote>If you ask for a configuration variable that doesn't exist, its valuewill be reported as "<tt class="literal">UNKNOWN</tt>". Configurationinformation is available from within a program using the<tt class="literal">Config</tt> module, although patterns are not supportedfor the hash subscripts:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>perl -MConfig -le 'print $Config{man1dir}'</b></tt>/usr/local/man/man1</pre></blockquote>See the <tt class="literal">Config</tt> module in <a href="ch32_01.htm">Chapter 32, "Standard Modules"</a>.</p></dd><dt><b><span class="option">-w</span></b></dt><dd><p><a name="INDEX-3457"></a><a name="INDEX-3458"></a><a name="INDEX-3459"></a>Prints warnings about variables that are mentioned only once andscalar values that are used before being set. Also warns aboutredefined subroutines, and references to undefined filehandles orfilehandles opened read-only that you are attempting to write on.Also warns you if you use values as numbers that don't look likenumbers, if you use an array as though it were a scalar, if your subroutinesrecurse more than 100 deep, and innumerable other things. See everyentry labelled "(W)" in <a href="ch33_01.htm">Chapter 33, "Diagnostic Messages"</a>.</p><p>This switch just sets the global <tt class="literal">$^W</tt> variable. It hasno effect on lexical warnings--see the <span class="option">-W</span> and<span class="option">-X</span> switches for that. You can enable or disable specificwarnings via the <tt class="literal">use warnings</tt>pragma, described in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>.</p></dd><dt><b><span class="option">-W</span></b></dt><dd><p>Unconditionally and permanently enables all warnings throughout theprogram, even if warnings were disabled locally using <tt class="literal">nowarnings</tt> or <tt class="literal">$^W = 0</tt>. This includes allfiles loaded via <tt class="literal">use</tt>, <tt class="literal">require</tt>,or <tt class="literal">do</tt>. Think of it as the Perl equivalent of the<em class="emphasis">lint</em>(1) command.</p></dd><dt><b><span class="option">-x</span><em class="replaceable">DIRECTORY</em></b></dt><dt><b><span class="option">-x</span></b></dt><dd><p><a name="INDEX-3460"></a><a name="INDEX-3461"></a><a name="INDEX-3462"></a><a name="INDEX-3463"></a>Tells Perl to extract a script thatis embedded in a message. Leading garbage will be discarded until thefirst line that starts with <tt class="literal">#!</tt> and contains thestring "<tt class="literal">perl</tt>". Any meaningful switches on thatline after the word "<tt class="literal">perl</tt>" will be applied. If adirectory name is specified, Perl will switch to that directory beforerunning the script. The <span class="option">-x</span> switch controls thedisposal of leading garbage only, not trailing garbage. The scriptmust be terminated with <tt class="literal">__END__</tt> or<tt class="literal">__DATA__</tt> if there is trailing garbage to beignored. (The script can process any or all of the trailing garbagevia the <tt class="literal">DATA</tt> filehandle if desired. It could evenin theory <tt class="literal">seek</tt> to the beginning of the file andprocess the leading garbage.)</p></dd><dt><b><span class="option">-X</span></b></dt><dd><p><a name="INDEX-3464"></a><a name="INDEX-3465"></a><a name="INDEX-3466"></a>Unconditionally and permanently disables all warnings, the exact opposite of what the <span class="option">-W</span> flag does.</p></dd></dl><a name="INDEX-3467"></a><a name="INDEX-3468"></a><a name="INDEX-3469"></a><!-- BOTTOM NAV BAR --><hr width="515" align="left"><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="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0"></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><tr><td align="left" valign="top" width="172">18.7. Avant-Garde Compiler, Retro Interpreter</td><td align="center" valign="top" width="171"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0"></a></td><td align="right" valign="top" width="172">19.2. Environment Variables</td></tr></table></div><hr width="515" align="left"><!-- LIBRARY NAV BAR --><img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p><font size="-1"><a href="copyrght.htm">Copyright © 2001</a> O'Reilly & Associates. All rights reserved.</font></p><map name="library-map"> <area shape="rect" coords="2,-1,79,99" href="../index.htm"><area shape="rect" coords="84,1,157,108" href="../perlnut/index.htm"><area shape="rect" coords="162,2,248,125" href="../prog/index.htm"><area shape="rect" coords="253,2,326,130" href="../advprog/index.htm"><area shape="rect" coords="332,1,407,112" href="../cookbook/index.htm"><area shape="rect" coords="414,2,523,103" href="../sysadmin/index.htm"></map><!-- END OF BODY --></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -