📄 ch02_04.htm
字号:
<html><head><title>Getting and Installing Modules (Perl in a Nutshell, 2nd Edition)</title><link rel="stylesheet" type="text/css" href="../style/style1.css" /><meta name="DC.Creator" content="Stephen Spainhour" /><meta name="DC.Format" content="text/xml" scheme="MIME" /><meta name="DC.Language" content="en-US" /><meta name="DC.Publisher" content="O'Reilly & Associates, Inc." /><meta name="DC.Source" scheme="ISBN" content="0596002416L" /><meta name="DC.Subject.Keyword" content="stuff" /><meta name="DC.Title" content="Perl in a Nutshell, 2nd Edition" /><meta name="DC.Type" content="Text.Monograph" /></head><body bgcolor="#ffffff"><img src="gifs/smbanner.gif" usemap="#banner-map" border="0" alt="Book Home" /><map name="banner-map"><area shape="rect" coords="1,-2,616,66" href="index.htm" alt="Java and XSLT" /><area shape="rect" coords="629,-11,726,25" href="jobjects/fsearch.htm" alt="Search this book" /></map><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch02_03.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228" /><td align="right" valign="top" width="228"><a href="ch02_05.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">2.4. Getting and Installing Modules</h2><p><a name="INDEX-79" /><a name="INDEX-80" /><a name="INDEX-81" />As you'll see whenyou look at the lists of modules and their authors on CPAN, manyusers have made their modules freely available. If you find aninteresting problem and are thinking of writing a module to solve it,check the <em class="emphasis">modules</em> directory on CPAN first to seeif there is a module there that you can use. The chances are goodthat there is a module that does what you need, or perhaps one thatyou can extend, rather than starting from scratch.<a href="#FOOTNOTE-4">[4]</a> </p><blockquote class="footnote"> <a name="FOOTNOTE-4" /><p> [4]Ifyou are interested in writing and contributing modules, there areseveral good starting points for learning to do so—see the<em class="emphasis">perlmodlib</em> manpage, the "Perl 5Module List," and the "Perl AuthorsUpload Server" (<a href="http://www.perl.com/CPAN/modules/04pause.html">http://www.perl.com/CPAN/modules/04pause.html</a>).</p></blockquote><p>Before you download a module, you might also check your system to seeif it's already installed. The following commandsearches the libraries in the <tt class="literal">@INC</tt> array andprints the names of all modules it finds:</p><blockquote><pre class="code">find `perl -e 'print "@INC"'` -name '*.pm' -print</pre></blockquote><a name="perlnut2-CHP-2-SECT-4.1" /><div class="sect2"><h3 class="sect2">2.4.1. Locating Modules</h3><p><a name="INDEX-82" />If you start from the<em class="emphasis">modules</em> directory on CPAN,you'll see that the modules are categorized intothree subdirectories:</p><blockquote><pre class="code">by-authors <em class="emphasis">Modules by author's registered CPAN name</em>by-category <em class="emphasis">Modules by subject matter (see below)</em>by-module <em class="emphasis">Modules by namespace (i.e., MIME)</em></pre></blockquote><p>If you know what module you want, you can go directly to it byclicking on the <em class="emphasis">by-module</em> entry. If you arelooking for a module in a particular category, you can find it in the<em class="emphasis">by-category</em> subdirectory. If you know theauthor, click on <em class="emphasis">by-author</em>. However, if youaren't familiar with the categories and want to finda module that performs a certain task, you might want to get the file<em class="emphasis">00modlist.long.html</em>, also in the<em class="emphasis">modules</em> directory. This file is the"Perl 5 Modules List." It containsa list of all the modules, by category, with a brief description ofthe purpose of each module and a link to theauthor's CPAN directory for downloading.</p><p><a name="INDEX-83" />Here is alist of the Perl Module categories, plus two for modules thatdon't fit anywhere else:</p><blockquote><pre class="code">02_Perl_Core_Modules03_Development_Support04_Operating_System_Interfaces05_Networking_Devices_IPC06_Data_Type_Utilities07_Database_Interface08_User_Interfaces09_Language_Interfaces10_File_Names_Systems_Locking11_String_Lang_Text_Proc12_Opt_Arg_Param_Proc13_Internationalization_Locale14_Security_and_Encryption15_World_Wide_Web_HTML_HTTP_CGI16_Server_and_Daemon_Utilities17_Archiving_and_Compression18_Images_Pixmaps_Bitmaps19_Mail_and_Usenet_News20_Control_Flow_Utilities21_File_Handle_Input_Output22_Microsoft_Windows_Modules23_Miscellaneous_Modules24_Commercial_Software_Interfaces99_Not_In_Modulelist99_Not_Yet_In_Modulelist</pre></blockquote><p>If you are in the <em class="emphasis">by-categories</em> subdirectory andhave selected an area from which you'd like todownload a module, you'll find a list of the filesin the directory. <em class="emphasis">tar</em> files have a<em class="emphasis">.tar.gz</em> extension, and README files have a<em class="emphasis">.readme</em> extension. You'llgenerally find a README file for each module; take a look at itbefore you decide to download the file.</p><p>Here's a sample directory listing from category 14,under the MD5 directory:</p><blockquote><pre class="code">Digest-MD5-2.09.readmeDigest-MD5-2.09.tar.gzGAASGARYMD5-1.5.3.readmeMD5-1.5.3.tar.gzMD5-1.6.readmeMD5-1.6.tar.gzMD5-1.7.readmeMD5-1.7.tar.gzNWINT</pre></blockquote><p><a name="INDEX-84" />You'll notice thatmultiple versions are sometimes listed—for example, the MD5module has Versions 1.5.3 through 1.7 available. Generally, this isto facilitate the transition to a new version of the module.</p><p>Select the <em class="emphasis">.readme</em> file of the most currentarchive and review its contents carefully. README files often givespecial instructions about building the module; they warn you aboutother modules needed for proper functioning and if the modulecan't be built under certain versions of Perl. Ifyou're satisfied with what you read, download thefile<a name="INDEX-85" />.</p></div><a name="perlnut2-CHP-2-SECT-4.2" /><div class="sect2"><h3 class="sect2">2.4.2. Module Installation</h3><p><a name="INDEX-86" />If you're running thestandard distribution of Perl, on either a Unix or Win32 system, andyou want to install a module, this section explains how to do it. Ifyou are running the ActiveState Win32 port, you can follow theinstructions covered in this section, unless you'rerunning on a system without a development toolkit; if this is thecase, see the next section.</p><p><a name="INDEX-87" />Beforeinstalling modules, you should understand at least a little about<em class="emphasis">make</em>. <em class="emphasis">make</em> is a commanddesigned to automate compilations; it guarantees that programs arecompiled with the correct options and are linked to the currentversion of program modules and libraries. But it'snot just for programmers—<em class="emphasis">make</em> is usefulfor any situation in which there are dependencies among a group ofrelated files.</p><p><a name="INDEX-88" /><em class="emphasis">make</em>uses a file known as a Makefile, which is a text file that describesthe dependencies and contains instructions that tell<em class="emphasis">make</em> what to do. A Perl programmer who writes amodule creates a file called <em class="emphasis">Makefile.PL</em> thatcomes with the module when you download it.<em class="emphasis">Makefile.PL</em> is a Perl script that uses anothermodule, ExtUtils::MakeMaker (generally referred to as simplyMakeMaker), to generate a Makefile specific to that module on yoursystem.</p><p>Before you can actually install the module, you need to decide whereit should go. Modules can be installed either globally, for everyoneto use, or locally, for your own use. Most system administratorsinstall popular software, including Perl modules, to be globallyavailable. In that case, the modules are generally installed in abranch of the <em class="emphasis">lib</em> directory with the rest of thePerl libraries.</p><p>If you have root privileges or write access to the locations wherePerl modules are installed on your system, you can proceed by movingthe downloaded module file to the correct directory and running<em class="emphasis">gunzip</em> and <em class="emphasis">tar</em> to unpackit. Then <em class="emphasis">cd</em> to the module directory and checkany README or INSTALL files, check the MANIFEST file to be sureeverything is there. If all is well, you can run the following tocomplete the installation:</p><blockquote><pre class="code">% perl Makefile.PL% make% make test% make install</pre></blockquote><p>If you're on a Win32 platform and are using Mingw32,do the following:</p><blockquote><pre class="code">C:\modulename-version> perl Makefile.PLC:\modulename-version> dmakeC:\modulename-version> dmake testC:\modulename-version> dmake install</pre></blockquote><p>It's possible that you'll need tocustomize <em class="emphasis">Makefile.PL</em> before running it. If so,see the discussion of ExtUtils::MakeMaker in <a href="ch08_01.htm">Chapter 8, "Standard Modules"</a>. Or, if you know the MakeMaker options thatyou'd like to add to<em class="filename">Makefile.PL</em>, you can add these options on thecommand line. A typical scenario would be on a system whereyou've installed a precompiled version of Perl, andthe CC and LD options in <em class="filename">Config.pm</em>don't match your programming environment; thus, Perlmodules won't build correctly. To solve thisproblem, you can do the following:</p><blockquote><pre class="code">% perl Makefile.PL CC=gcc LD=gcc</pre></blockquote><p>If you are going to install the module locally (for example, if youdon't have permission to install globally or youwant to test it locally before installing it for general use), youneed to pass a PREFIX argument to Perl when you run<em class="emphasis">Makefile.PL</em> to generate the Makefile. Thisargument tells MakeMaker to use the directory following PREFIX as thebase directory when installing the module.</p><p>For example, to install a module in the directory<em class="emphasis">/home/mydir/Perl/Modules</em>, the PREFIX argumentwould look like this:</p><blockquote><pre class="code">% perl Makefile.PL PREFIX=/home/mydir/Perl/Modules</pre></blockquote><p>Then follow the remaining steps, as above:</p><blockquote><pre class="code">% make% make test% make install</pre></blockquote><p>The module is now available, but when you write Perl code to use themodule, there's another detail to take care of.Since Perl looks in system-wide directories as specified in thespecial array <tt class="literal">@INC</tt>, it won't findlocal modules unless you tell it where they are. Instead,you'll receive an error message such as thefollowing:</p><blockquote><pre class="code">Can't locate <ModuleName>.pm in @INC.BEGIN failed--compilation aborted.</pre></blockquote><p>Thus, if you installed the module in<em class="emphasis">/home/mydir/Perl/Modules</em>, you need to tell Perlto look in that location with the command <tt class="literal">use</tt><tt class="literal">lib'</tt><em class="replaceable"><tt>path</tt></em><tt class="literal">'</tt>:</p><blockquote><pre class="code">#!/usr/local/bin/perl -wuse lib '/home/mydir/Perl/Modules';use ModuleName;</pre></blockquote></div><a name="perlnut2-CHP-2-SECT-4.3" /><div class="sect2"><h3 class="sect2">2.4.3. Installing Modules with ActiveState Perl</h3><p><a name="INDEX-89" /><a name="INDEX-90" />Prior to Perl 5.005,ActiveState's Perl for Win32 did not supportMakeMaker. If you are running Perl 5.004 (or earlier), you shouldupgrade because the absense of MakeMaker prevents you from installingand using most current Perl modules. While some modules can beinstalled manually, this is <em class="emphasis">not</em> suggested, sinceit's likely that something will be forgotten, andthe module won't work correctly! You should followall module documentation to determine which installation technique isthe proper one, so that everything will be okay.</p><p>With 5.6 and later, you can use MakeMaker to install the modules, oryou can use the Perl Package Manager that comes with ActivePerl.</p><a name="perlnut2-CHP-2-SECT-4.3.1" /><div class="sect3"><h3 class="sect3">2.4.3.1. Using MakeMaker</h3><p><a name="INDEX-91" />To installa module using MakeMaker, follow the procedure described earlier forinstalling when you are running the standard distribution, replacing<em class="emphasis">make</em> with <em class="emphasis">nmake</em> or<em class="emphasis">dmake</em> as appropriate.</p></div><a name="perlnut2-CHP-2-SECT-4.3.2" /><div class="sect3"><h3 class="sect3">2.4.3.2. Using the Perl Package Manager</h3><p><a name="INDEX-92" /><a name="INDEX-93" />The Perl Package Manager (PPM) providesa command-line interface for obtaining and installing Perl modulesand extensions. To run PPM, connect to CPAN and type:</p>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -