📄 ch19_02.htm
字号:
<html><head><title>Environment Variables (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 & Associates, Inc."><meta name="DC.Source" content="" scheme="ISBN"><meta name="DC.Subject.Keyword" content=""><meta name="DC.Title" content="Environment Variables"><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="ch19_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch19_01.htm">Chapter 19: The Command-Line Interface</a></td><td align="right" valign="top" width="172"><a href="ch20_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr></table></div><hr width="515" align="left"><!-- SECTION BODY --><h2 class="sect1">19.2. Environment Variables</h2><p><a name="INDEX-3470"></a><a name="INDEX-3471"></a>In addition to the various switches that explicitly modify Perl'sbehavior, you can set various environment variables to influencevarious underlying behaviors. How you set up these environmentvariables is system dependent, but one trick you should knowif you use <em class="emphasis">sh</em>, <em class="emphasis">ksh</em>, or <em class="emphasis">bash</em> is that you can temporarilyset an environment variable for a single command, as if it werea funny kind of switch. It has to be set in front of the command:<blockquote><pre class="programlisting"><tt class="computeroutput">$</tt> <tt class="userinput"><b>PATH='/bin:/usr/bin' perl myproggie</b></tt></pre></blockquote><a name="INDEX-3472"></a>You can do something similar with a subshell in <em class="emphasis">csh</em> and <em class="emphasis">tcsh</em>:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>(setenv PATH "/bin:/usr/bin"; perl myproggie)</b></tt></pre></blockquote>Otherwise, you'd typically set environment variables in some file witha name resembling <em class="emphasis">.chsrc</em> or<em class="emphasis">.profile</em> in your home directory. Under<em class="emphasis">csh</em> and <em class="emphasis">tcsh</em> you'd say:<blockquote><pre class="programlisting"><tt class="computeroutput">%</tt> <tt class="userinput"><b>setenv PATH '/bin:/usr/bin'</b></tt></pre></blockquote>And under <em class="emphasis">sh</em>, <em class="emphasis">ksh</em>, and <em class="emphasis">bash</em> you'd say:<blockquote><pre class="programlisting"><tt class="computeroutput">$</tt> <tt class="userinput"><b>PATH='/bin:/usr/bin'; export PATH</b></tt></pre></blockquote>Other systems will have other ways of setting these on a semi-permanent basis.Here are the environment variables Perl pays attention to:</p><dl><dt><b><tt class="literal">HOME</tt></b></dt><dd><p>Used if <tt class="literal">chdir</tt> is called without an argument.<a name="INDEX-3473"></a><a name="INDEX-3474"></a></p></dd><dt><b><tt class="literal">LC_ALL</tt>, <tt class="literal">LC_CTYPE</tt>, <tt class="literal">LC_COLLATE</tt>, <tt class="literal">LC_NUMERIC</tt>, <tt class="literal">PERL_BADLANG</tt></b></dt><dd><p><a name="INDEX-3475"></a>Environment variables that control how Perl handles data specific toparticular natural languages. See the online docs for <em class="emphasis">perllocale</em>.</p></dd><dt><b><tt class="literal">LOGDIR</tt></b></dt><dd><p><a name="INDEX-3476"></a>Used if <tt class="literal">chdir</tt> has no argument, but <tt class="literal">HOME</tt> is not set.</p></dd><dt><b><tt class="literal">PATH</tt></b></dt><dd><p><a name="INDEX-3477"></a>Used in executing subprocesses, and for finding the program if the<tt class="userinput"><b>-S</b></tt> switch is used.</p></dd><dt><b><tt class="literal">PERL5LIB</tt></b></dt><dd><p> Acolon-separated list of directories in which to look for Perl libraryfiles before looking in the standard library and the currentdirectory. Any architecture-specific directories under the specifiedlocations are automatically included if they exist. If<tt class="literal">PERL5LIB</tt> is not defined, <tt class="literal">PERLLIB</tt>is consulted for backward compatibility with older releases.<a name="INDEX-3478"></a><a name="INDEX-3479"></a></p><p>When running taint checks (either because the program was runningsetuid or setgid, or the <tt class="userinput"><b>-T</b></tt> switch was used),neither of these library variables is used. Such programs must employthe <tt class="literal">use lib</tt> pragma for that purpose.</p></dd><dt><b><tt class="literal">PERL5OPT</tt></b></dt><dd><p><a name="INDEX-3480"></a><a name="INDEX-3481"></a>Default command-line switches. Switches in this variable are taken asif they were on every Perl command line. Only the<tt class="userinput"><b>-[DIMUdmw]</b></tt> switches are allowed. When runningtaint checks (because the program was running setuid or setgid, or the<tt class="userinput"><b>-T</b></tt> switch was used), this variable is ignored.If <tt class="literal">PERL5OPT</tt> begins with <tt class="userinput"><b>-T</b></tt>,tainting will be enabled, causing any subsequent options to beignored.</p></dd><dt><b><tt class="literal">PERL5DB</tt></b></dt><dd><p><a name="INDEX-3482"></a><a name="INDEX-3483"></a>The command used to load the debugger code. The default is:<blockquote><pre class="programlisting">BEGIN { require 'perl5db.pl' }</pre></blockquote>See <a href="ch20_01.htm">Chapter 20, "The Perl Debugger"</a> for more uses ofthis variable.</p></dd><dt><b><tt class="literal">PERL5SHELL</tt> <em class="emphasis">(Microsoft ports only)</em></b></dt><dd><p><a name="INDEX-3484"></a><a name="INDEX-3485"></a><a name="INDEX-3486"></a>May be set to an alternative shell that Perl must use internally forexecuting commands via backticks or <tt class="literal">system</tt>.Default is <tt class="literal">cmd.exe /x/c</tt> on WinNT and<tt class="literal">command.com /c</tt> on Win95. The value is consideredto be space separated. Precede any character that needs to beprotected (like a space or backslash) with a backslash.</p><p>Note that Perl doesn't use <tt class="literal">COMSPEC</tt> for this purposebecause <tt class="literal">COMSPEC</tt> has a high degree of variabilityamong users, leading to portability concerns. Besides, Perl can use ashell that may not be fit for interactive use, and setting<tt class="literal">COMSPEC</tt> to such a shell may interfere with theproper functioning of other programs (which usually look in<tt class="literal">COMSPEC</tt> to find a shell fit for interactive use).<a name="INDEX-3487"></a></p></dd><dt><b><tt class="literal">PERLLIB</tt></b></dt><dd><p>A colon-separated list of directories in which to look for Perl libraryfiles before looking in the standard library and the current directory.If <tt class="literal">PERL5LIB</tt> is defined, <tt class="literal">PERLLIB</tt> is not used.</p></dd><dt><b><tt class="literal">PERL_DEBUG_MSTATS</tt></b></dt><dd><p><a name="INDEX-3488"></a><a name="INDEX-3489"></a><a name="INDEX-3490"></a>Relevant only if Perl is compiled with the <tt class="literal">malloc</tt> functionincluded with the Perl distribution (that is, if <tt class="literal">perl -V:d_mymalloc</tt>yields "<tt class="literal">define</tt>"). If set, this causes memory statistics to be displayedafter execution. If set to an integer greater than one, also causesmemory statistics to be displayed after compilation.</p></dd><dt><b><tt class="literal">PERL_DESTRUCT_LEVEL</tt></b></dt><dd><p><a name="INDEX-3491"></a><a name="INDEX-3492"></a><a name="INDEX-3493"></a><a name="INDEX-3494"></a><a name="INDEX-3495"></a>Relevant only if your Perl executable was built with debugging enabled,this controls the behavior of global destruction of objects and otherreferences.</p></dd></dl><p>Apart from these, Perl itself uses no other environment variables,except to make them available to the program being executed andto any child processes that program launches. Some modules, standardor otherwise, may care about other environment variables. Forexample, the <tt class="literal">use re</tt> pragma uses <tt class="literal">PERL_RE_TC</tt> and <tt class="literal">PERL_RE_COLORS</tt>,the <tt class="literal">Cwd</tt> module uses <tt class="literal">PWD</tt>, and the <tt class="literal">CGI</tt> module uses the manyenvironment variables set by your HTTP daemon (that is, your webserver) to pass information to the CGI script.<a name="INDEX-3496"></a></p><p>Programs running setuid would do well to execute the following linesbefore doing anything else, just to keep people honest:<blockquote><pre class="programlisting">$ENV{PATH} = '/bin:/usr/bin'; # or whatever you need$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};</pre></blockquote>See <a href="ch23_01.htm">Chapter 23, "Security"</a> for details.</p><a name="INDEX-3497"></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="ch19_01.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="ch20_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">19.1. Command Processing</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">20. The Perl Debugger</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 + -