perlmodstyle.html

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

HTML
809
字号
<p>Be consistent with existing modules.</p>
</li>
<li>
<p>Reflect the functionality of the module, not the implementation.</p>
</li>
<li>
<p>Avoid starting a new top-level hierarchy, especially if a suitable
hierarchy already exists under which you could place your module.</p>
</li>
</ul>
<p>You should contact <a href="mailto:modules@perl.org">modules@perl.org</a> to ask them about your module name
before publishing your module.  You should also try to ask people who 
are already familiar with the module's application domain and the CPAN
naming system.  Authors of similar modules, or modules with similar
names, may be a good place to start.</p>
<p>
</p>
<hr />
<h1><a name="designing_and_writing_your_module">DESIGNING AND WRITING YOUR MODULE</a></h1>
<p>Considerations for module design and coding:</p>
<p>
</p>
<h2><a name="to_oo_or_not_to_oo">To OO or not to OO?</a></h2>
<p>Your module may be object oriented (OO) or not, or it may have both kinds 
of interfaces available.  There are pros and cons of each technique, which 
should be considered when you design your API.</p>
<p>According to Damian Conway, you should consider using OO:</p>
<ul>
<li>
<p>When the system is large or likely to become so</p>
</li>
<li>
<p>When the data is aggregated in obvious structures that will become objects</p>
</li>
<li>
<p>When the types of data form a natural hierarchy that can make use of inheritance</p>
</li>
<li>
<p>When operations on data vary according to data type (making
polymorphic invocation of methods feasible)</p>
</li>
<li>
<p>When it is likely that new data types may be later introduced
into the system, and will need to be handled by existing code</p>
</li>
<li>
<p>When interactions between data are best represented by
overloaded operators</p>
</li>
<li>
<p>When the implementation of system components is likely to
change over time (and hence should be encapsulated)</p>
</li>
<li>
<p>When the system design is itself object-oriented</p>
</li>
<li>
<p>When large amounts of client code will use the software (and
should be insulated from changes in its implementation)</p>
</li>
<li>
<p>When many separate operations will need to be applied to the
same set of data</p>
</li>
</ul>
<p>Think carefully about whether OO is appropriate for your module.
Gratuitous object orientation results in complex APIs which are
difficult for the average module user to understand or use.</p>
<p>
</p>
<h2><a name="designing_your_api">Designing your API</a></h2>
<p>Your interfaces should be understandable by an average Perl programmer.  
The following guidelines may help you judge whether your API is
sufficiently straightforward:</p>
<dl>
<dt><strong><a name="item_write_simple_routines_to_do_simple_things_2e">Write simple routines to do simple things.</a></strong>

<dd>
<p>It's better to have numerous simple routines than a few monolithic ones.
If your routine changes its behaviour significantly based on its
arguments, it's a sign that you should have two (or more) separate
routines.</p>
</dd>
</li>
<dt><strong><a name="item_separate_functionality_from_output_2e">Separate functionality from output.</a></strong>

<dd>
<p>Return your results in the most generic form possible and allow the user 
to choose how to use them.  The most generic form possible is usually a
Perl data structure which can then be used to generate a text report,
HTML, XML, a database query, or whatever else your users require.</p>
</dd>
<dd>
<p>If your routine iterates through some kind of list (such as a list of
files, or records in a database) you may consider providing a callback
so that users can manipulate each element of the list in turn.
File::Find provides an example of this with its 
<code>find(\&amp;wanted, $dir)</code> syntax.</p>
</dd>
</li>
<dt><strong><a name="item_provide_sensible_shortcuts_and_defaults_2e">Provide sensible shortcuts and defaults.</a></strong>

<dd>
<p>Don't require every module user to jump through the same hoops to achieve a
simple result.  You can always include optional parameters or routines for 
more complex or non-standard behaviour.  If most of your users have to
type a few almost identical lines of code when they start using your
module, it's a sign that you should have made that behaviour a default.
Another good indicator that you should use defaults is if most of your 
users call your routines with the same arguments.</p>
</dd>
</li>
<dt><strong><a name="item_naming_conventions">Naming conventions</a></strong>

<dd>
<p>Your naming should be consistent.  For instance, it's better to have:</p>
</dd>
<dd>
<pre>
        <span class="variable">display_day</span><span class="operator">();</span>
        <span class="variable">display_week</span><span class="operator">();</span>
        <span class="variable">display_year</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>than</p>
</dd>
<dd>
<pre>
        <span class="variable">display_day</span><span class="operator">();</span>
        <span class="variable">week_display</span><span class="operator">();</span>
        <span class="variable">show_year</span><span class="operator">();</span>
</pre>
</dd>
<dd>
<p>This applies equally to method names, parameter names, and anything else
which is visible to the user (and most things that aren't!)</p>
</dd>
</li>
<dt><strong><a name="item_parameter_passing">Parameter passing</a></strong>

<dd>
<p>Use named parameters. It's easier to use a hash like this:</p>
</dd>
<dd>
<pre>
    <span class="variable">$obj</span><span class="operator">-&gt;</span><span class="variable">do_something</span><span class="operator">(</span>
            <span class="string">name</span> <span class="operator">=&gt;</span> <span class="string">"wibble"</span><span class="operator">,</span>
            <span class="string">type</span> <span class="operator">=&gt;</span> <span class="string">"text"</span><span class="operator">,</span>
            <span class="string">size</span> <span class="operator">=&gt;</span> <span class="number">1024</span><span class="operator">,</span>
    <span class="operator">);</span>
</pre>
</dd>
<dd>
<p>... than to have a long list of unnamed parameters like this:</p>
</dd>
<dd>
<pre>
    <span class="variable">$obj</span><span class="operator">-&gt;</span><span class="variable">do_something</span><span class="operator">(</span><span class="string">"wibble"</span><span class="operator">,</span> <span class="string">"text"</span><span class="operator">,</span> <span class="number">1024</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>While the list of arguments might work fine for one, two or even three
arguments, any more arguments become hard for the module user to
remember, and hard for the module author to manage.  If you want to add
a new parameter you will have to add it to the end of the list for
backward compatibility, and this will probably make your list order
unintuitive.  Also, if many elements may be undefined you may see the
following unattractive method calls:</p>
</dd>
<dd>
<pre>
    <span class="variable">$obj</span><span class="operator">-&gt;</span><span class="variable">do_something</span><span class="operator">(</span><span class="keyword">undef</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="keyword">undef</span><span class="operator">,</span> <span class="number">1024</span><span class="operator">);</span>
</pre>
</dd>
<dd>
<p>Provide sensible defaults for parameters which have them.  Don't make
your users specify parameters which will almost always be the same.</p>
</dd>
<dd>
<p>The issue of whether to pass the arguments in a hash or a hashref is
largely a matter of personal style.</p>
</dd>
<dd>
<p>The use of hash keys starting with a hyphen (<code>-name</code>) or entirely in 
upper case (<a href="../../lib/Pod/perlfunc.html#item_name"><code>NAME</code></a>) is a relic of older versions of Perl in which
ordinary lower case strings were not handled correctly by the <code>=&gt;</code>
operator.  While some modules retain uppercase or hyphenated argument
keys for historical reasons or as a matter of personal style, most new
modules should use simple lower case keys.  Whatever you choose, be
consistent!</p>
</dd>
</li>
</dl>
<p>
</p>
<h2><a name="strictness_and_warnings">Strictness and warnings</a></h2>
<p>Your module should run successfully under the strict pragma and should
run without generating any warnings.  Your module should also handle 
taint-checking where appropriate, though this can cause difficulties in
many cases.</p>
<p>
</p>
<h2><a name="backwards_compatibility">Backwards compatibility</a></h2>
<p>Modules which are &quot;stable&quot; should not break backwards compatibility
without at least a long transition phase and a major change in version
number.</p>
<p>
</p>
<h2><a name="error_handling_and_messages">Error handling and messages</a></h2>
<p>When your module encounters an error it should do one or more of:</p>
<ul>
<li>
<p>Return an undefined value.</p>
</li>
<li>
<p>set <code>$Module::errstr</code> or similar (<code>errstr</code> is a common name used by
DBI and other popular modules; if you choose something else, be sure to
document it clearly).</p>
</li>
<li>
<p><a href="../../lib/Pod/perlfunc.html#item_warn"><code>warn()</code></a> or <code>carp()</code> a message to STDERR.</p>
</li>
<li>
<p><code>croak()</code> only when your module absolutely cannot figure out what to
do.  (<code>croak()</code> is a better version of <a href="../../lib/Pod/perlfunc.html#item_die"><code>die()</code></a> for use within 
modules, which reports its errors from the perspective of the caller.  
See <a href="../../lib/Carp.html">the Carp manpage</a> for details of <code>croak()</code>, <code>carp()</code> and other useful
routines.)</p>
</li>
<li>
<p>As an alternative to the above, you may prefer to throw exceptions using 
the Error module.</p>
</li>
</ul>
<p>Configurable error handling can be very useful to your users.  Consider
offering a choice of levels for warning and debug messages, an option to
send messages to a separate file, a way to specify an error-handling
routine, or other such features.  Be sure to default all these options
to the commonest use.</p>
<p>
</p>
<hr />
<h1><a name="documenting_your_module">DOCUMENTING YOUR MODULE</a></h1>
<p>
</p>
<h2><a name="pod">POD</a></h2>
<p>Your module should include documentation aimed at Perl developers.
You should use Perl's &quot;plain old documentation&quot; (POD) for your general 
technical documentation, though you may wish to write additional
documentation (white papers, tutorials, etc) in some other format.  
You need to cover the following subjects:</p>
<ul>
<li>
<p>A synopsis of the common uses of the module</p>
</li>
<li>
<p>The purpose, scope and target applications of your module</p>
</li>
<li>
<p>Use of each publically accessible method or subroutine, including
parameters and return values</p>
</li>
<li>
<p>Examples of use</p>
</li>
<li>
<p>Sources of further information</p>
</li>
<li>

⌨️ 快捷键说明

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