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

📄 ch12_08.htm

📁 By Tom Christiansen and Nathan Torkington ISBN 1-56592-243-3 First Edition, published August 1998
💻 HTM
字号:
<HTML><HEAD><TITLE>Recipe 12.7. Keeping Your Own Module Directory (Perl Cookbook)</TITLE><METANAME="DC.title"CONTENT="Perl Cookbook"><METANAME="DC.creator"CONTENT="Tom Christiansen &amp; Nathan Torkington"><METANAME="DC.publisher"CONTENT="O'Reilly &amp; Associates, Inc."><METANAME="DC.date"CONTENT="1999-07-02T01:41:35Z"><METANAME="DC.type"CONTENT="Text.Monograph"><METANAME="DC.format"CONTENT="text/html"SCHEME="MIME"><METANAME="DC.source"CONTENT="1-56592-243-3"SCHEME="ISBN"><METANAME="DC.language"CONTENT="en-US"><METANAME="generator"CONTENT="Jade 1.1/O'Reilly DocBook 3.0 to HTML 4.0"><LINKREV="made"HREF="mailto:online-books@oreilly.com"TITLE="Online Books Comments"><LINKREL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"><LINKREL="prev"HREF="ch12_07.htm"TITLE="12.6. Automating Module Clean-Up"><LINKREL="next"HREF="ch12_09.htm"TITLE="12.8. Preparing a Module for Distribution"></HEAD><BODYBGCOLOR="#FFFFFF"><img alt="Book Home" border="0" src="gifs/smbanner.gif" usemap="#banner-map" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Perl Cookbook"><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><p><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_07.htm"TITLE="12.6. Automating Module Clean-Up"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.6. Automating Module Clean-Up"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><B><FONTFACE="ARIEL,HELVETICA,HELV,SANSERIF"SIZE="-1"><ACLASS="chapter"REL="up"HREF="ch12_01.htm"TITLE="12. Packages, Libraries, and Modules"></A></FONT></B></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_09.htm"TITLE="12.8. Preparing a Module for Distribution"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.8. Preparing a Module for Distribution"BORDER="0"></A></TD></TR></TABLE></DIV><DIVCLASS="sect1"><H2CLASS="sect1"><ACLASS="title"NAME="ch12-21229">12.7. Keeping Your Own Module Directory</A></H2><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-928">Problem<ACLASS="indexterm"NAME="ch12-idx-1000005234-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005234-1"></A></A></H3><PCLASS="para">You don't want to install your own personal modules in the standard per-system extension library.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-934">Solution</A></H3><PCLASS="para">You have several choices: use Perl's <BCLASS="emphasis.bold">-I</B><ACLASS="indexterm"NAME="ch12-idx-1000005235-0"></A> command line switch; set your <CODECLASS="literal">PERL5LIB</CODE> environment variable; or employ the <CODECLASS="literal">use</CODE> <CODECLASS="literal">lib</CODE> pragma, possibly in conjunction with the FindBin module.</P></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-940">Discussion</A></H3><PCLASS="para">The <CODECLASS="literal">@INC</CODE><ACLASS="indexterm"NAME="ch12-idx-1000005236-0"></A> array contains a list of directories that are consulted every time a <CODECLASS="literal">do</CODE>, <CODECLASS="literal">require</CODE>, or <CODECLASS="literal">use</CODE> compiles code from another file, library, or module. You can print these out easily from the command line:</P><PRECLASS="programlisting">% perl -e 'for (@INC) { printf &quot;%d %s\n&quot;, $i++, $_ }'<CODECLASS="userinput"><B><CODECLASS="replaceable"><I>0 /usr/local/perl/lib/i686-linux/5.004</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>1 /usr/local/perl/lib</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>2 /usr/local/perl/lib/site_perl/i686-linux</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>3 /usr/local/perl/lib/site_perl</I></CODE></B></CODE><CODECLASS="userinput"><B><CODECLASS="replaceable"><I>4 .</I></CODE></B></CODE></PRE><PCLASS="para">The first two directories, elements 0 and 1 of <CODECLASS="literal">@INC</CODE>, are the standard architecture-dependent and architecture-independent directories, which all standard libraries, modules, and pragmas will go into. You have two of them because some modules contain information or formatting that makes sense only on that particular architecture. For example, the Config module contains information that cannot be shared across several architectures, so it goes in the 0th array element. Modules that include compiled C components, such as <EMCLASS="emphasis">Socket.so</EM>, are also placed there. Most modules, however, go in the platform-independent directory in the 1st element.</P><PCLASS="para">The next pair, elements 2 and 3 above, fulfills roles analogous to elements 0 and 1, but on a site-specific basis. Suppose you have a module that didn't come with Perl, like a module from CPAN or one you wrote yourself. When you   or (more likely) your system administrator  installs this module, its components go into one of the site-specific directories. You are encouraged to use these for any modules that your entire site should be able to access conveniently.</P><PCLASS="para">The last standard component, <CODECLASS="literal">&quot;.&quot;</CODE> (your current working directory), is useful only when developing and testing your software, not when deploying it. If your modules are in the same directory that you last <CODECLASS="literal">chdir</CODE>ed to, you're fine. If you're anywhere else, it doesn't work.</P><PCLASS="para">So sometimes none of the <CODECLASS="literal">@INC</CODE> directories work out. Maybe you have your own personal modules. Perhaps your project group has particular modules that are relevant only to that project. In these cases, you need to augment the standard <CODECLASS="literal">@INC</CODE> search.</P><PCLASS="para">The first approach involves using a command-line flag, <BCLASS="emphasis.bold">-I</B><EMCLASS="emphasis">dirlist</EM>. The <EMCLASS="emphasis">dirlist</EM> is a colon-separated[<ACLASS="footnote"HREF="#ch12-pgfId-1000005036">1</A>] list of one or more directories, which will be prepended to the front of the <CODECLASS="literal">@INC</CODE> array. This works well for simple command lines, and thus can be used on a per-command basis, such as when you call a quick one-liner from a shell script.</P><BLOCKQUOTECLASS="footnote"><DIVCLASS="footnote"><PCLASS="para"><ACLASS="footnote"NAME="ch12-pgfId-1000005036">[1]</A> Comma-separated on MacOS.</P></DIV></BLOCKQUOTE><PCLASS="para">This technique should not be included in the <CODECLASS="literal">#!</CODE> (pound-bang) line. First, it's not much fun to modify each program. More importantly, some older operating systems have bugs related to how long that line can be, typically 32 characters, including the <CODECLASS="literal">#!</CODE> part. That means if you have a very long path, such as <CODECLASS="literal">#!/opt/languages/free/extrabits/perl</CODE>, you may get the mysterious <CODECLASS="literal">&quot;Command</CODE> <CODECLASS="literal">not</CODE> <CODECLASS="literal">found&quot;</CODE> error. Perl does its best to rescan the line manually, but it's still too dicey to rely on. <BCLASS="emphasis.bold"></B><ACLASS="indexterm"NAME="ch12-idx-1000005238-0"></A></P><PCLASS="para">Often, a better solution is to set the <CODECLASS="literal">PERL5LIB</CODE><ACLASS="indexterm"NAME="ch12-idx-1000005242-0"></A> environment variable. This can be done in your shell start-up file. Or, your system administrator may want to do so in a systemwide start-up file so all users can benefit. For example, suppose you have all your own modules in a directory called <EMCLASS="emphasis">~/perllib</EM>. You would place one of the following lines in your shell start-up file, depending on which shell you use:</P><PRECLASS="programlisting"># syntax for sh, bash, ksh, or zsh$ export PERL5LIB=$HOME/perllib# syntax for csh or tcsh% setenv PERL5LIB ~/perllib</PRE><PCLASS="para">Probably the most convenient solution from your users' perspective is for you to add a <CODECLASS="literal">use</CODE><ACLASS="indexterm"NAME="ch12-idx-1000005243-0"></A> <CODECLASS="literal">lib</CODE> pragma near the top of your script. That way the users of the program don't need to take any special action to run your program. Imagine a hypothetical project called Spectre whose programs rely on its own set of libraries. Those programs could have a statement like this at their start:</P><PRECLASS="programlisting">use lib &quot;/projects/spectre/lib&quot;;</PRE><PCLASS="para">What happens when you don't know the exact path to the library? Perhaps you've allowed the whole project to be installed in an arbitrary path. You could create an elaborate installation procedure to dynamically update the script, but even if you did, paths would still be frozen at installation time. If someone moved the files later, the libraries wouldn't be found.</P><PCLASS="para">The <ACLASS="indexterm"NAME="ch12-idx-1000005244-0"></A>FindBin module conveniently solves this problem. This module tries to compute the full path to the executing script's enclosing directory, setting an importable package variable called <CODECLASS="literal">$Bin</CODE> to that directory. Typical usage is either to look for modules in the same directory as the program or in a <EMCLASS="emphasis">lib</EM> directory at the same level.</P><PCLASS="para">To demonstrate the first case, suppose you have a program called <EMCLASS="emphasis">/wherever/spectre/myprog</EM> that needs to look in <EMCLASS="emphasis">/wherever/spectre</EM> for its modules, but you don't want to hardcode that path.</P><PRECLASS="programlisting">use FindBin;use lib $FindBin::Bin;</PRE><PCLASS="para">The second case would be used if your program lives in <EMCLASS="emphasis">/wherever/spectre/bin/myprog</EM> but needs to look at <EMCLASS="emphasis">/wherever/spectre/lib</EM> for its modules.</P><PRECLASS="programlisting">use FindBin qw($Bin);use lib &quot;$Bin/../lib&quot;;<ACLASS="indexterm"NAME="ch12-idx-1000005331-0"></A><ACLASS="indexterm"NAME="ch12-idx-1000005331-1"></A></PRE></DIV><DIVCLASS="sect2"><H3CLASS="sect2"><ACLASS="title"NAME="ch12-pgfId-1002">See Also</A></H3><PCLASS="para">The documentation for the standard <CODECLASS="literal">use</CODE> <CODECLASS="literal">lib</CODE> pragma and the standard FindBin module; the discussion of the PERL5LIB environment in <ICLASS="filename">perl</I> (1); your shell's syntax for setting environment variables</P></DIV></DIV><DIVCLASS="htmlnav"><P></P><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><TABLEWIDTH="684"BORDER="0"CELLSPACING="0"CELLPADDING="0"><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_07.htm"TITLE="12.6. Automating Module Clean-Up"><IMGSRC="../gifs/txtpreva.gif"ALT="Previous: 12.6. Automating Module Clean-Up"BORDER="0"></A></TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="book"HREF="index.htm"TITLE="Perl Cookbook"><IMGSRC="../gifs/txthome.gif"ALT="Perl Cookbook"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228"><ACLASS="sect1"HREF="ch12_09.htm"TITLE="12.8. Preparing a Module for Distribution"><IMGSRC="../gifs/txtnexta.gif"ALT="Next: 12.8. Preparing a Module for Distribution"BORDER="0"></A></TD></TR><TR><TDALIGN="LEFT"VALIGN="TOP"WIDTH="228">12.6. Automating Module Clean-Up</TD><TDALIGN="CENTER"VALIGN="TOP"WIDTH="228"><ACLASS="index"HREF="index/index.htm"TITLE="Book Index"><IMGSRC="../gifs/index.gif"ALT="Book Index"BORDER="0"></A></TD><TDALIGN="RIGHT"VALIGN="TOP"WIDTH="228">12.8. Preparing a Module for Distribution</TD></TR></TABLE><HRALIGN="LEFT"WIDTH="684"TITLE="footer"><FONTSIZE="-1"></DIV<!-- LIBRARY NAV BAR --> <img src="../gifs/smnavbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links"><p> <a href="copyrght.htm">Copyright &copy; 2002</a> O'Reilly &amp; Associates. All rights reserved.</font> </p> <map name="library-map"> <area shape="rect" coords="1,0,85,94" href="../index.htm"><area shape="rect" coords="86,1,178,103" href="../lwp/index.htm"><area shape="rect" coords="180,0,265,103" href="../lperl/index.htm"><area shape="rect" coords="267,0,353,105" href="../perlnut/index.htm"><area shape="rect" coords="354,1,446,115" href="../prog/index.htm"><area shape="rect" coords="448,0,526,132" href="../tk/index.htm"><area shape="rect" coords="528,1,615,119" href="../cookbook/index.htm"><area shape="rect" coords="617,0,690,135" href="../pxml/index.htm"></map> </BODY></HTML>

⌨️ 快捷键说明

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