📄 ch22_02.htm
字号:
<html><head><title>Using CPAN Modules (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="Using CPAN Modules"><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="ch22_01.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0"></a></td><td align="center" valign="top" width="171"><a href="ch22_01.htm">Chapter 22: CPAN</a></td><td align="right" valign="top" width="172"><a href="ch22_03.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">22.2. Using CPAN Modules</h2><a name="INDEX-3910"></a><a name="INDEX-3911"></a><a name="INDEX-3912"></a><a name="INDEX-3913"></a><p>Most modules that you'll find on CPAN are in "tarball" form. That is,they have a file extension ending in <em class="emphasis">.tar.gz</em>, and expand into a directory with the module code and any auxiliaryfiles, usually including a <em class="emphasis">README</em> and a <em class="emphasis">Makefile.PL</em> file.</p><p><a name="INDEX-3914"></a>There are four steps for making CPAN modules available to your programs:decompression, unpacking, building, and installation. How each ofthose steps work depend on your operating system and the module beinginstalled, so we can't give you a foolproof recipe that will work allthe time. When in doubt, read the <em class="emphasis">README</em> and <em class="emphasis">INSTALL</em> files thatwere hopefully distributed with the module. Also read the <em class="emphasis">perlmodinstall</em>manpage.<a name="INDEX-3915"></a><a name="INDEX-3916"></a></p><p><a name="INDEX-3917"></a><a name="INDEX-3918"></a><a name="INDEX-3919"></a>But you may never have to think about the installation procedure ifyou use the <tt class="literal">CPAN</tt> module (bundled with the Perldistribution) or PPM (the Perl Package Manager, bundled with theActiveState distribution of Perl). To use the <tt class="literal">CPAN</tt>module (not to be confused with CPAN itself), type:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MCPAN -e "shell"</b></tt></pre></blockquote>at your command line to begin the configuration process. After you'veanswered a variety of questions about how you'd like to retrievefiles, you can install a module by typing:<blockquote><pre class="programlisting"><tt class="userinput"><b>install Some::Module</b></tt></pre></blockquote>in the <tt class="literal">CPAN</tt> module's shell, or by typing:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -MCPAN -e "install 'Some::Module'"</b></tt></pre></blockquote>from your normal command line.</p><p>If you don't have the convenience of either the <tt class="literal">CPAN</tt> module or PPM, you'llneed to go through the steps in the following sections by hand. Instructions areprovided for Unix, Windows, and Macintosh; for other operating systems,consult the <em class="emphasis">perlmodinstall</em> manpage.</p><h3 class="sect2">22.2.1. Decompressing and Unpacking CPAN Modules</h3><p><a name="INDEX-3920"></a><a name="INDEX-3921"></a><a name="INDEX-3922"></a><a name="INDEX-3923"></a><a name="INDEX-3924"></a><a name="INDEX-3925"></a>Most of the thousands of utilities on CPAN are compressed so that theytake up less space. Once you've retrieved a module tarball, you firstneed to turn it into a directory tree on your system by decompressing("unzipping") and unpacking the tarball. On Unix, you can use <em class="emphasis">gzip</em>and <em class="emphasis">tar</em> to do this. (On many systems, <em class="emphasis">tar</em> will do both.) OnWindows, <em class="emphasis">WinZip</em> will both decompress and unpack tarballs. On aMacintosh, you can either use <em class="emphasis">StuffIt</em> and <em class="emphasis">DropStuff</em>, <em class="emphasis">MacGzip</em>, or<em class="emphasis">suntar</em>.</p><h3 class="sect2">22.2.2. Building CPAN Modules</h3><a name="INDEX-3926"></a><a name="INDEX-3927"></a><a name="INDEX-3928"></a><p>A minority of CPAN modules come with C code that you'll need to compilefor your system, which is naturally a problem for systems that lack a Ccompiler. The standard procedure for building a CPAN module (with orwithout C code) is the following three commands, executed from thecommand line. (If you don't have a command line, or a<em class="emphasis">make</em>-equivalent, you'll need to resort to more drastic,system-dependent measures. Windows users have a command line butmight need to use the <em class="emphasis">nmake</em> utility instead of <em class="emphasis">make</em>.)<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl Makefile.PL</b></tt>% <tt class="userinput"><b>make</b></tt>% <tt class="userinput"><b>make test</b></tt></pre></blockquote>The <em class="emphasis">perl Makefile.PL</em> command will try to create a <em class="emphasis">Makefile</em>, whichthe subsequent <em class="emphasis">make</em> command uses to determine what utilities need to bebuilt and in what order. The final command, <em class="emphasis">make test</em>, runs thetest suite that the module author hopefully included.</p><h3 class="sect2">22.2.3. Installing CPAN Modules into the Perl Library</h3><a name="INDEX-3929"></a><a name="INDEX-3930"></a><a name="INDEX-3931"></a><a name="INDEX-3932"></a><a name="INDEX-3933"></a><p>Presuming you've followed the previous steps, you now have a modulethat has been built and tested, but not yet installed into Perl'slibrary. When Perl was installed on your system, a <em class="emphasis">lib</em> directorywas created to hold modules, pragmas, and related files. That's thePerl library, which is usually something like <em class="emphasis">/usr/local/lib/perl5</em>on Unix systems and <em class="emphasis">C:\PERL\LIB</em> by default on Windows systems.Modules installed after Perl was built are stored in the <em class="emphasis">site_perl</em>subdirectory of the Perl library. You can see the names of all your librarydirectories (and a bunch of other stuff) by saying:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl -V</b></tt></pre></blockquote>To install the module, type:<blockquote><pre class="programlisting">% <tt class="userinput"><b>make install</b></tt></pre></blockquote>Superuser access is normally required, even for installing modulesinto your site-specific Perl library directories.</p><p>With a little work, you can install the module in a directory outsideyour Perl library (such as your home directory). If you wouldnormally have typed <em class="emphasis">perl Makefile.PL</em> to create a <em class="emphasis">Makefile</em>, youcould instead use this incantation:<blockquote><pre class="programlisting">% <tt class="userinput"><b>perl Makefile.PL LIB=/my/dir/perllib \ INSTALLMAN1DIR=/my/dir/man/man1 \ INSTALLMAN3DIR=/my/dir/man/man3 \ INSTALLBIN=/my/dir/bin \ INSTALLSCRIPT=/my/dir/scripts</b></tt></pre></blockquote>This will install the modules somewhere in the <em class="emphasis">/my/dir/perllib</em> directoryand any remaining files where they need to go. (If you find yourselftyping this a lot, you could even write a little Perl program to do itfor you. Perl is good for things like that.)</p><p>Then you can have Perl search your special directory for modules byadding:<blockquote><pre class="programlisting">use lib "/my/dir/perllib";</pre></blockquote>before your program attempts to load in the module. You may also setthe <tt class="literal">PERL5LIB</tt> environment variable to that directory, or use Perl's<span class="option">-I</span> switch. See the <tt class="literal">use lib</tt> pragma in <a href="ch31_01.htm">Chapter 31, "Pragmatic Modules"</a>for examples of doing this.</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="ch22_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="ch22_03.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">22.1. The CPAN modules Directory</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">22.3. Creating CPAN Modules</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 + -