perlnewmod.html

来自「perl教程」· HTML 代码 · 共 376 行 · 第 1/2 页

HTML
376
字号
<?xml version="1.0" ?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<!-- saved from url=(0017)http://localhost/ -->
<script language="JavaScript" src="../../displayToc.js"></script>
<script language="JavaScript" src="../../tocParas.js"></script>
<script language="JavaScript" src="../../tocTab.js"></script>
<link rel="stylesheet" type="text/css" href="../../scineplex.css">
<title>perlnewmod - preparing a new module for distribution</title>
<link rel="stylesheet" href="../../Active.css" type="text/css" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rev="made" href="mailto:" />
</head>

<body>

<script>writelinks('__top__',2);</script>
<h1><a>perlnewmod - preparing a new module for distribution</a></h1>
<p><a name="__index__"></a></p>

<!-- INDEX BEGIN -->

<ul>

	<li><a href="#name">NAME</a></li>
	<li><a href="#description">DESCRIPTION</a></li>
	<ul>

		<li><a href="#warning">Warning</a></li>
		<li><a href="#what_should_i_make_into_a_module">What should I make into a module?</a></li>
		<li><a href="#stepbystep__preparing_the_ground">Step-by-step: Preparing the ground</a></li>
		<li><a href="#stepbystep__making_the_module">Step-by-step: Making the module</a></li>
		<li><a href="#stepbystep__distributing_your_module">Step-by-step: Distributing your module</a></li>
	</ul>

	<li><a href="#author">AUTHOR</a></li>
	<li><a href="#see_also">SEE ALSO</a></li>
</ul>
<!-- INDEX END -->

<hr />
<p>
</p>
<h1><a name="name">NAME</a></h1>
<p>perlnewmod - preparing a new module for distribution</p>
<p>
</p>
<hr />
<h1><a name="description">DESCRIPTION</a></h1>
<p>This document gives you some suggestions about how to go about writing
Perl modules, preparing them for distribution, and making them available
via CPAN.</p>
<p>One of the things that makes Perl really powerful is the fact that Perl
hackers tend to want to share the solutions to problems they've faced,
so you and I don't have to battle with the same problem again.</p>
<p>The main way they do this is by abstracting the solution into a Perl
module. If you don't know what one of these is, the rest of this
document isn't going to be much use to you. You're also missing out on
an awful lot of useful code; consider having a look at <a href="../../lib/Pod/perlmod.html">the perlmod manpage</a>,
<a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a> and <a href="../../lib/Pod/perlmodinstall.html">the perlmodinstall manpage</a> before coming back here.</p>
<p>When you've found that there isn't a module available for what you're
trying to do, and you've had to write the code yourself, consider
packaging up the solution into a module and uploading it to CPAN so that
others can benefit.</p>
<p>
</p>
<h2><a name="warning">Warning</a></h2>
<p>We're going to primarily concentrate on Perl-only modules here, rather
than XS modules. XS modules serve a rather different purpose, and
you should consider different things before distributing them - the
popularity of the library you are gluing, the portability to other
operating systems, and so on. However, the notes on preparing the Perl
side of the module and packaging and distributing it will apply equally
well to an XS module as a pure-Perl one.</p>
<p>
</p>
<h2><a name="what_should_i_make_into_a_module">What should I make into a module?</a></h2>
<p>You should make a module out of any code that you think is going to be
useful to others. Anything that's likely to fill a hole in the communal
library and which someone else can slot directly into their program. Any
part of your code which you can isolate and extract and plug into
something else is a likely candidate.</p>
<p>Let's take an example. Suppose you're reading in data from a local
format into a hash-of-hashes in Perl, turning that into a tree, walking
the tree and then piping each node to an Acme Transmogrifier Server.</p>
<p>Now, quite a few people have the Acme Transmogrifier, and you've had to
write something to talk the protocol from scratch - you'd almost
certainly want to make that into a module. The level at which you pitch
it is up to you: you might want protocol-level modules analogous to
<a href="../../lib/Net/SMTP.html">Net::SMTP</a> which then talk to higher level modules analogous
to <a href="../../Mail/Send.html">Mail::Send</a>. The choice is yours, but you do want to get
a module out for that server protocol.</p>
<p>Nobody else on the planet is going to talk your local data format, so we
can ignore that. But what about the thing in the middle? Building tree
structures from Perl variables and then traversing them is a nice,
general problem, and if nobody's already written a module that does
that, you might want to modularise that code too.</p>
<p>So hopefully you've now got a few ideas about what's good to modularise.
Let's now see how it's done.</p>
<p>
</p>
<h2><a name="stepbystep__preparing_the_ground">Step-by-step: Preparing the ground</a></h2>
<p>Before we even start scraping out the code, there are a few things we'll
want to do in advance.</p>
<dl>
<dt><strong><a name="item_look_around">Look around</a></strong>

<dd>
<p>Dig into a bunch of modules to see how they're written. I'd suggest
starting with <a href="../../lib/Text/Tabs.html">Text::Tabs</a>, since it's in the standard
library and is nice and simple, and then looking at something a little
more complex like <a href="../../lib/File/Copy.html">File::Copy</a>.  For object oriented
code, <code>WWW::Mechanize</code> or the <code>Email::*</code> modules provide some good
examples.</p>
</dd>
<dd>
<p>These should give you an overall feel for how modules are laid out and
written.</p>
</dd>
</li>
<dt><strong><a name="item_check_it_27s_new">Check it's new</a></strong>

<dd>
<p>There are a lot of modules on CPAN, and it's easy to miss one that's
similar to what you're planning on contributing. Have a good plough
through the <a href="http://search.cpan.org">http://search.cpan.org</a> and make sure you're not the one
reinventing the wheel!</p>
</dd>
</li>
<dt><strong><a name="item_discuss_the_need">Discuss the need</a></strong>

<dd>
<p>You might love it. You might feel that everyone else needs it. But there
might not actually be any real demand for it out there. If you're unsure
about the demand your module will have, consider sending out feelers
on the <code>comp.lang.perl.modules</code> newsgroup, or as a last resort, ask the
modules list at <code>modules@perl.org</code>. Remember that this is a closed list
with a very long turn-around time - be prepared to wait a good while for
a response from them.</p>
</dd>
</li>
<dt><strong><a name="item_choose_a_name">Choose a name</a></strong>

<dd>
<p>Perl modules included on CPAN have a naming hierarchy you should try to
fit in with. See <a href="../../lib/Pod/perlmodlib.html">the perlmodlib manpage</a> for more details on how this works, and
browse around CPAN and the modules list to get a feel of it. At the very
least, remember this: modules should be title capitalised, (This::Thing)
fit in with a category, and explain their purpose succinctly.</p>
</dd>
</li>
<dt><strong><a name="item_check_again">Check again</a></strong>

<dd>
<p>While you're doing that, make really sure you haven't missed a module
similar to the one you're about to write.</p>
</dd>
<dd>
<p>When you've got your name sorted out and you're sure that your module is
wanted and not currently available, it's time to start coding.</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="stepbystep__making_the_module">Step-by-step: Making the module</a></h2>
<dl>
<dt><strong><a name="item_start_with_module_2dstarter_or_h2xs">Start with <em>module-starter</em> or <em>h2xs</em></a></strong>

<dd>
<p>The <em>module-starter</em> utility is distributed as part of the
<a href="../../Module/Starter.html">Module::Starter</a> CPAN package.  It creates a directory
with stubs of all the necessary files to start a new module, according
to recent &quot;best practice&quot; for module development, and is invoked from
the command line, thus:</p>
</dd>
<dd>
<pre>
    module-starter --module=Foo::Bar \
       --author=&quot;Your Name&quot; --email=yourname@cpan.org</pre>
</dd>
<dd>
<p>If you do not wish to install the <a href="../../Module/Starter.html">Module::Starter</a>
package from CPAN, <em>h2xs</em> is an older tool, originally intended for the
development of XS modules, which comes packaged with the Perl
distribution.</p>
</dd>

⌨️ 快捷键说明

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