📄 ch08_46.htm
字号:
<html><head><title>CPAN (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="ch08_45.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="ch08_47.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr></table></div><h2 class="sect1">8.46. CPAN</h2><p><a name="INDEX-1334" />Lets youaccess CPAN; search for a module, a bundle, an author, or adistribution; download a module or distribution; install it; and<tt class="literal">make</tt> it. The CPAN module can be used eitherinteractively from the command line or programmatically:</p><blockquote><pre class="code">perl -MCPAN -eshell; # Run from the command line</pre></blockquote><p>or: </p><blockquote><pre class="code">use CPAN;my $obj = CPAN::Shell->install('ExtUtils::MakeMaker');</pre></blockquote><p>This section describes the use of the CPAN module from a program. See<a href="ch02_01.htm">Chapter 2, "Installing Perl"</a> for information on using itinteractively and for details of the available commands. Thesecommands, available interactively from the shell, are methods of theclass CPAN::Shell. From a program, they are available both as methods(e.g., <tt class="literal">CPAN::Shell->install(...)</tt>) and asfunctions in the calling package (e.g.,<tt class="literal">install(...)</tt>).</p><p>Each of the commands that produce listings of modules(<tt class="literal">r</tt>, <tt class="literal">autobundle</tt>, and<tt class="literal">u</tt>) returns a list of the IDs of all modules withinthe list. The IDs of all objects available within a program arestrings that can be expanded to the corresponding real objects withthe <tt class="literal">CPAN::Shell->expand("Module",@things)</tt>method. <tt class="literal">expand</tt> returns a list of CPAN::Moduleobjects according to the <tt class="literal">@things</tt> arguments. Inscalar context, it returns only the first element of the list.</p><a name="perlnut2-CHP-8-SECT-46.1" /><div class="sect2"><h3 class="sect2">8.46.1. Session and Cache Managers</h3><p>The CPAN module contains a session manager, which keeps track ofobjects that have been fetched, built, and installed in the currentsession. No status is retained between sessions.</p><p>There is also a cache manager, which keeps track of disk space usedand deletes extra space. The cache manager keeps track of the builddirectory, <tt class="literal">$CPAN::Config->{build_dir}</tt>, and usesa simple FIFO mechanism to delete directories below<tt class="literal">build_dir</tt> when they grow bigger than<tt class="literal">$CPAN::Config->{build_cache}</tt>.</p><p>The original distribution files are kept in the directory<tt class="literal">$CPAN::Config->{keep_source_where}</tt>. Thisdirectory is not covered by the cache manager but must be controlledby the user. If the same directory is used as both<tt class="literal">build_dir</tt> and<tt class="literal">keep_source_where</tt>, your sources are deleted withthe same FIFO mechanism.</p></div><a name="perlnut2-CHP-8-SECT-46.2" /><div class="sect2"><h3 class="sect2">8.46.2. Bundles</h3><p>The CPAN module recognizes a bundle as a Perl module in the namespaceBundle:: that does not define any functions or methods and usuallycontains only pod documentation. It starts like a Perl module with apackage declaration and a <tt class="literal">$VERSION</tt> variable. Afterthat, the pod section looks like any other pod except that itcontains a special section that begins with:</p><blockquote><pre class="code">=head1 CONTENTS</pre></blockquote><p>This section consists of lines such as: </p><blockquote><pre class="code"><em class="replaceable"><tt>Module_Name</em> [<em class="replaceable">Version_String</em>] [- <em class="replaceable">optional text</tt></em>]</pre></blockquote><p>in which <em class="replaceable"><tt>Module_Name</tt></em> is the name of amodule (for example, Term::ReadLine), not the name of a distributionfile, and the version and text are optional. If there is text, it ispreceded by a <tt class="literal">-</tt>. The distribution of a bundleshould follow the same convention as other distributions.</p><p>Bundles are treated specially in the CPAN package. When you tell CPANto install a bundle, it installs all the modules in the CONTENTSsection of the pod. You can install your own bundles locally byplacing a conforming bundle file somewhere in your<tt class="literal">@INC</tt> path. The <tt class="literal">autobundle</tt>command available in the shell interface does that for you byincluding all currently installed modules in a snapshot bundle file(see <a href="ch02_01.htm">Chapter 2, "Installing Perl"</a>).</p></div><a name="perlnut2-CHP-8-SECT-46.3" /><div class="sect2"><h3 class="sect2">8.46.3. Configuration</h3><p>When the CPAN module is installed, a site-wide configuration file iscreated: <em class="emphasis">CPAN/Config.pm</em>. The default valuesdefined there can be overridden locally in the file<em class="emphasis">CPAN/MyConfig.pm</em>. You can store this file in<em class="emphasis">$HOME/.cpan/CPAN/MyConfig.pm</em>, because<em class="emphasis">$HOME/.cpan</em> is added to the search path of theCPAN module before the <tt class="literal">use</tt> or<tt class="literal">require</tt> statements. <a href="ch02_01.htm">Chapter 2, "Installing Perl"</a>lists the keys defined in the hash reference<tt class="literal">$CPAN::Config</tt> and how to set and query them.</p></div><a name="perlnut2-CHP-8-SECT-46.4" /><div class="sect2"><h3 class="sect2">8.46.4. CD-ROM Support</h3><p>The <em class="emphasis">urllist</em><a name="INDEX-1335" /> parameterin the configuration table contains a list of URLs that will be usedfor downloading. If the list contains any <em class="emphasis">file</em>URLs, CPAN looks there first for files (except index files). So ifyou are using a CD-ROM containing the CPAN contents, include theCD-ROM as a <em class="emphasis">file</em> URL at the end of<em class="emphasis">urllist</em> since it is likely to be out-of-date.You can do this with:</p><blockquote><pre class="code">o conf urllist push file://localhost/CDROM/CPAN</pre></blockquote></div><hr width="684" align="left" /><div class="navbar"><table width="684" border="0"><tr><td align="left" valign="top" width="228"><a href="ch08_45.htm"><img src="../gifs/txtpreva.gif" alt="Previous" border="0" /></a></td><td align="center" valign="top" width="228"><a href="index.htm"><img src="../gifs/txthome.gif" alt="Home" border="0" /></a></td><td align="right" valign="top" width="228"><a href="ch08_47.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0" /></a></td></tr><tr><td align="left" valign="top" width="228">8.45. constant</td><td align="center" valign="top" width="228"><a href="index/index.htm"><img src="../gifs/index.gif" alt="Book Index" border="0" /></a></td><td align="right" valign="top" width="228">8.47. CPAN::FirstTime</td></tr></table></div><hr width="684" align="left" /><img src="../gifs/navbar.gif" usemap="#library-map" border="0" alt="Library Navigation Links" /><p><p><font size="-1"><a href="copyrght.htm">Copyright © 2002</a> O'Reilly & 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 + -