📄 ch22_03.htm
字号:
<html><head><title>Creating 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="Creating 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_02.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="ch23_01.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.3. Creating CPAN Modules</h2><a name="INDEX-3934"></a><a name="INDEX-3935"></a><a name="INDEX-3936"></a><a name="INDEX-3937"></a><a name="INDEX-3938"></a><a name="INDEX-3939"></a><p>If you have a module that you think others might find useful, considermaking the world a better place by uploading it to CPAN. The serverthat handles new module submissions is called PAUSE (the Perl AuthorsUpload Server) and can be found at <em class="emphasis">https://pause.kbx.de/pause/</em>.Before you can upload your module, you'll have to get a PAUSE account.This is about as close as you can get to being a "registered Perldeveloper".</p><p>If you call yourself a registered Perl developer, you should knowenough to document your modules. Perl has a convention of embeddingdocumentation inside your source code. (That way, you never lose it.)This embedded documentation is in a format called "pod" (for Plain OldDocumentation) and is described in <a href="ch26_01.htm">Chapter 26, "Plain Old Documentation"</a>.</p><p>You should consider making your module thread safe. See <a href="ch17_01.htm">Chapter 17, "Threads"</a>.</p><p>You should also worry a little bit about whether your cute littlemodule does things that could break the security of people who use it,because other folks may have some Really Good Reasons to be moreconcerned about security than you are (yet). See <a href="ch23_01.htm">Chapter 23, "Security"</a>, forall about how to avoid being responsible for the outbreak of World WarIII and other such nuisances.</p><p><a name="INDEX-3940"></a><a name="INDEX-3941"></a><a name="INDEX-3942"></a>Modules meant to be distributed on CPAN should include aPerl program named <em class="emphasis">Makefile.PL</em> that, when run, generates a <em class="emphasis">Makefile</em>,and a <em class="emphasis">README</em> file briefly explaining what the module is and how toinstall it. The <em class="emphasis">Makefile</em> will expect your module to include a test suite as well. You can create all these files at once with the<em class="emphasis">h2xs</em> utility:<blockquote><pre class="programlisting">h2xs -X -n Foo::Bar</pre></blockquote>(Substitute <span class="option">-A</span> for <span class="option">-X</span> if you're building a module that has an XScomponent. XS is described in <a href="ch21_01.htm">Chapter 21, "Internals and Externals"</a>.) The<em class="emphasis">h2xs</em> program creates a single directory with skeletal files for youto flesh out. When you've finished, you can upload the tarballeddirectory to PAUSE.</p><p>The <em class="emphasis">Makefile.PL</em> generated by <em class="emphasis">h2xs</em> will look something like this:<blockquote><pre class="programlisting">use ExtUtils::MakeMaker;# See lib/ExtUtils/MakeMaker.pm for details of how to influence# the contents of the Makefile that is written.WriteMakefile( NAME => 'Mytest', VERSION_FROM => 'Mytest.pm', # finds $VERSION LIBS => [''], # e.g., '-lm' DEFINE => '', # e.g., '-DHAVE_SOMETHING' INC => '', # e.g., '-I/usr/include/other');</pre></blockquote>The first thing <em class="emphasis">Makefile.PL</em> does is to pull in the<tt class="literal">ExtUtils::MakeMaker</tt> module. <tt class="literal">MakeMaker</tt>'s <tt class="literal">WriteMakefile</tt>function has oodles of options (where oodles is defined asapproximately 88) to help you customize what happens after the userdownloads your module from CPAN and types <em class="emphasis">perl MakeFile.PL</em> to beginbuilding it. The nice thing about all this is that, since the user ispresumably running the <em class="emphasis">perl</em> that will be used later, you have awealth of configuration information available (via the <tt class="literal">Config</tt> moduleor the <tt class="literal">$^O</tt> special variable) to help you decide how to drive<tt class="literal">MakeMaker</tt>. On the other hand, <tt class="literal">MakeMaker</tt> is really good atfinding decent defaults for almost everything, so the skeletal filewritten <em class="emphasis">h2xs</em> may well be all you need, with perhaps a tweak or two.For more on this, see the extensive online docs for<tt class="literal">ExtUtils::MakeMaker</tt>.</p><p><a name="INDEX-3943"></a><a name="INDEX-3944"></a>When writing a Perl module for general consumption, you should allowfor the possibility that the user's version of Perl may differ fromyours. You should always put an English description of anydependencies (particular versions of Perl, or system requirements, orother modules) into the <em class="emphasis">README</em> file. However, even that may not besufficient, since someone using the slick <tt class="literal">CPAN</tt> module toautomatically download and install your module might never see thewarning. So you should check those dependencies in <em class="emphasis">Makefile.PL</em>.Here's how you might ensure that the person who downloaded your moduleis running Perl 5.6 or greater:<blockquote><pre class="programlisting">eval { require 5.6.0 } or die <<'EOD';############### This module requires lvaluable subroutines, which are not available### in versions of Perl earlier than 5.6. Please upgrade!############EOD</pre></blockquote></p><h3 class="sect2">22.3.1. Internal Testing</h3><a name="INDEX-3945"></a><p>The standard instructions for installing a module tell the user to run<em class="emphasis">make test</em> after building the module with <em class="emphasis">make</em>. So please includea decent test script with any module that you upload to CPAN. Youshould emulate the <tt class="literal">ok</tt>/<tt class="literal">not ok</tt> style that Perl uses in its own testsuite, so that it's easy for the user to determine the outcome of eachtest case. The <em class="emphasis">test.pl</em> file generated by <em class="emphasis">h2xs</em> will help get youstarted. <a href="ch21_01.htm">Chapter 21, "Internals and Externals"</a> has some examples of teststhat you can add to the end of <em class="emphasis">test.pl</em>.</p><p>If you have many test cases, you might want to mimic Perl's test suiteby creating a subdirectory named <em class="emphasis">t/</em> in the module's directory andappending <em class="emphasis">.t</em> to the names of your different testscripts. When you run <em class="emphasis">make test</em>, all test files will beexecuted automatically.</p><h3 class="sect2">22.3.2. External Testing</h3><p><a name="INDEX-3946"></a>Modules uploaded to CPAN are tested by a variety of volunteers ondifferent platforms. These CPAN testers are notified by mail of eachnew upload, and reply to the list with <tt class="literal">PASS</tt>, <tt class="literal">FAIL</tt>, <tt class="literal">NA</tt> (notapplicable to this platform), or <tt class="literal">UNKNOWN</tt> (unknown), along with anyrelevant notations. You can find the mailing list for CPAN testers at<em class="email">cpan-testers@perl.org</em>; test results are posted at<a href="http://testers.cpan.org/">http://testers.cpan.org/</a>.</p><p>That's all just the preliminary testing, of course. The real testingbegins when someone plugs your little module into a web server that'scranking out a million pages a day. Or uses your module to help designthe airplane you'll be riding in someday soon.</p><p>So go ahead, skip writing those pesky little tests. See if we care...</p><a name="INDEX-3947"></a><a name="INDEX-3948"></a><!-- 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_02.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="ch23_01.htm"><img src="../gifs/txtnexta.gif" alt="Next" border="0"></a></td></tr><tr><td align="left" valign="top" width="172">22.2. Using CPAN Modules</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">23. Security</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 + -