📄 ch31_13.htm
字号:
<html><head><title>use lib (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="use lib"><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="ch31_12.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch31_01.htm">Chapter 31: Pragmatic Modules</a></td><td align="right" valign="top" width="172"><a href="ch31_14.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">31.13. use lib</h2><blockquote><pre class="programlisting">use lib "$ENV{HOME}/libperl"; # add ~/libperlno lib "."; # remove cwd</pre></blockquote><p>This pragma simplifies the manipulation of <tt class="literal">@INC</tt> at compile time.It is typically used to add extra directories to Perl's search pathso that later <tt class="literal">do</tt>, <tt class="literal">require</tt>, and <tt class="literal">use</tt> statements will findlibrary files that aren't located in Perl's default search path.It's especially important with <tt class="literal">use</tt>, since that happens at compiletime too, and setting <tt class="literal">@INC</tt> normally (that is, at run time) would be too late.</p><p>Parameters to <tt class="literal">use lib</tt> are prepended to the beginning of Perl'ssearch path. Saying<tt class="literal">use lib</tt><em class="replaceable">LIST</em> is <em class="emphasis">almost</em> the same as saying<tt class="literal">BEGIN { unshift(@INC,</tt><em class="replaceable">LIST</em><tt class="literal">) }</tt>,but <tt class="literal">use lib</tt><em class="replaceable">LIST</em> includes support forplatform-specific directories. For each given directory <tt class="literal">$dir</tt> inits argument list, the <tt class="literal">lib</tt> pragma also checks to see whether adirectory named <em class="emphasis">$dir/$archname/auto</em> exists. If so, the<em class="emphasis">$dir/$archname</em> directory is assumed to be a correspondingplatform-specific directory, so is added to <tt class="literal">@INC</tt> (in front of <tt class="literal">$dir</tt>).</p><p>To avoid redundant additions that slow access time and waste asmall amount of memory, trailing duplicate entries in <tt class="literal">@INC</tt> areremoved when entries are added.</p><p>Normally, you should only <em class="emphasis">add</em> directories to <tt class="literal">@INC</tt>. If you do needto delete directories from <tt class="literal">@INC</tt>, take care to delete only thosethat you yourself added, or those that you're somehow certain aren't neededby other modules in your program. Other modules may have addeddirectories to your <tt class="literal">@INC</tt> that they need for correct operation.</p><p>The <tt class="literal">no lib</tt> pragma deletes all instances of each named directoryfrom <tt class="literal">@INC</tt>. It also deletes any correspondingplatform-specific directory as described earlier.</p><p>When the <tt class="literal">lib</tt> pragma is loaded, it saves the current value of<tt class="literal">@INC</tt> to the array <tt class="literal">@lib::ORIG_INC</tt>, so to restore the original,just copy that array to the real <tt class="literal">@INC</tt>.</p><p>Even though <tt class="literal">@INC</tt> typically includes dot ("<tt class="literal">.</tt>"),the current directory, this really isn't as useful as you'd think. For one thing,the dot entry comes at the end, not the start, so that modulesinstalled in the current directory don't suddenly override systemversions. You could say <tt class="literal">use lib "."</tt> if that's what you reallywant. More annoyingly, it's the current directory of thePerl process, not the directory that the script was installed into,which makes it completely unreliable. If you create a programplus some modules for that program to use, it will workwhile you're developing, but it won't work when you aren't runningin the directory the files live in.</p><p>One solution for this is to use the standard <tt class="literal">FindBin</tt> module:<blockquote><pre class="programlisting">use FindBin; # where was script installed?use lib $FindBin::Bin; # use that dir for libs, too</pre></blockquote>The <tt class="literal">FindBin</tt> module tries to guess the full path to the directory in whichthe running process's program was installed. Don't use this for securitypurposes, because malicious programs can usually deceive it if they tryhard enough. But unless you're intentionally trying to break the module,it should work as intended. The module provides a <tt class="literal">$FindBin::Bin</tt>variable (which you may import) that contains the module's guess of wherethe program was installed. You can then use the <tt class="literal">lib</tt> pragma to addthat directory to your <tt class="literal">@INC</tt>, thus producing an executable-relative path.</p><p>Some programs expect to be installed in a <em class="emphasis">bin</em> directory and then findtheir library modules in "cousin" files installed in a <em class="emphasis">lib</em>directory at the same level as <em class="emphasis">bin</em>. For example, programsmight go in <em class="emphasis">/usr/local/apache/bin</em> or <em class="emphasis">/opt/perl/bin</em>, andlibraries go in <em class="emphasis">/usr/local/apache/lib</em> and <em class="emphasis">/opt/perl/lib</em>.This code takes care of that neatly:<blockquote><pre class="programlisting">use FindBin qw($Bin);use lib "$Bin/../lib";</pre></blockquote>If you find yourself specifying the same <tt class="literal">use lib</tt> in severalunrelated programs, you might consider setting the <tt class="literal">PERL5LIB</tt> environmentvariable instead. See the description of the <tt class="literal">PERL5LIB</tt>environment variable in <a href="ch19_01.htm">Chapter 19, "The Command-Line Interface"</a>.<blockquote><pre class="programlisting"># syntax for sh, bash, ksh, or zsh$ PERL5LIB=$HOME/perllib; export PERL5LIB# syntax for csh or tcsh% setenv PERL5LIB ~/perllib</pre></blockquote>If you want to use optional directories on just this program withoutchanging its source, look into the <tt class="userinput"><b>-I</b></tt> command-line switch:<blockquote><pre class="programlisting">% perl -I ~/perllib program-path args</pre></blockquote>See the <a href="ch19_01.htm">Chapter 19, "The Command-Line Interface"</a> for more about using <tt class="userinput"><b>-I</b></tt>from the command line.</p><!-- 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="ch31_12.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="ch31_14.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">31.12. use less</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">31.14. use locale</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 + -