xref: /onnv-gate/usr/src/cmd/perl/5.8.4/distrib/pod/perlmodstyle.pod (revision 0:68f95e015346)
1*0Sstevel@tonic-gate=head1 NAME
2*0Sstevel@tonic-gate
3*0Sstevel@tonic-gateperlmodstyle - Perl module style guide
4*0Sstevel@tonic-gate
5*0Sstevel@tonic-gate=head1 INTRODUCTION
6*0Sstevel@tonic-gate
7*0Sstevel@tonic-gateThis document attempts to describe the Perl Community's "best practice"
8*0Sstevel@tonic-gatefor writing Perl modules.  It extends the recommendations found in
9*0Sstevel@tonic-gateL<perlstyle> , which should be considered required reading
10*0Sstevel@tonic-gatebefore reading this document.
11*0Sstevel@tonic-gate
12*0Sstevel@tonic-gateWhile this document is intended to be useful to all module authors, it is
13*0Sstevel@tonic-gateparticularly aimed at authors who wish to publish their modules on CPAN.
14*0Sstevel@tonic-gate
15*0Sstevel@tonic-gateThe focus is on elements of style which are visible to the users of a
16*0Sstevel@tonic-gatemodule, rather than those parts which are only seen by the module's
17*0Sstevel@tonic-gatedevelopers.  However, many of the guidelines presented in this document
18*0Sstevel@tonic-gatecan be extrapolated and applied successfully to a module's internals.
19*0Sstevel@tonic-gate
20*0Sstevel@tonic-gateThis document differs from L<perlnewmod> in that it is a style guide
21*0Sstevel@tonic-gaterather than a tutorial on creating CPAN modules.  It provides a
22*0Sstevel@tonic-gatechecklist against which modules can be compared to determine whether
23*0Sstevel@tonic-gatethey conform to best practice, without necessarily describing in detail
24*0Sstevel@tonic-gatehow to achieve this.
25*0Sstevel@tonic-gate
26*0Sstevel@tonic-gateAll the advice contained in this document has been gleaned from
27*0Sstevel@tonic-gateextensive conversations with experienced CPAN authors and users.  Every
28*0Sstevel@tonic-gatepiece of advice given here is the result of previous mistakes.  This
29*0Sstevel@tonic-gateinformation is here to help you avoid the same mistakes and the extra
30*0Sstevel@tonic-gatework that would inevitably be required to fix them.
31*0Sstevel@tonic-gate
32*0Sstevel@tonic-gateThe first section of this document provides an itemized checklist;
33*0Sstevel@tonic-gatesubsequent sections provide a more detailed discussion of the items on
34*0Sstevel@tonic-gatethe list.  The final section, "Common Pitfalls", describes some of the
35*0Sstevel@tonic-gatemost popular mistakes made by CPAN authors.
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate=head1 QUICK CHECKLIST
38*0Sstevel@tonic-gate
39*0Sstevel@tonic-gateFor more detail on each item in this checklist, see below.
40*0Sstevel@tonic-gate
41*0Sstevel@tonic-gate=head2 Before you start
42*0Sstevel@tonic-gate
43*0Sstevel@tonic-gate=over 4
44*0Sstevel@tonic-gate
45*0Sstevel@tonic-gate=item *
46*0Sstevel@tonic-gate
47*0Sstevel@tonic-gateDon't re-invent the wheel
48*0Sstevel@tonic-gate
49*0Sstevel@tonic-gate=item *
50*0Sstevel@tonic-gate
51*0Sstevel@tonic-gatePatch, extend or subclass an existing module where possible
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate=item *
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gateDo one thing and do it well
56*0Sstevel@tonic-gate
57*0Sstevel@tonic-gate=item *
58*0Sstevel@tonic-gate
59*0Sstevel@tonic-gateChoose an appropriate name
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate=back
62*0Sstevel@tonic-gate
63*0Sstevel@tonic-gate=head2 The API
64*0Sstevel@tonic-gate
65*0Sstevel@tonic-gate=over 4
66*0Sstevel@tonic-gate
67*0Sstevel@tonic-gate=item *
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gateAPI should be understandable by the average programmer
70*0Sstevel@tonic-gate
71*0Sstevel@tonic-gate=item *
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gateSimple methods for simple tasks
74*0Sstevel@tonic-gate
75*0Sstevel@tonic-gate=item *
76*0Sstevel@tonic-gate
77*0Sstevel@tonic-gateSeparate functionality from output
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate=item *
80*0Sstevel@tonic-gate
81*0Sstevel@tonic-gateConsistent naming of subroutines or methods
82*0Sstevel@tonic-gate
83*0Sstevel@tonic-gate=item *
84*0Sstevel@tonic-gate
85*0Sstevel@tonic-gateUse named parameters (a hash or hashref) when there are more than two
86*0Sstevel@tonic-gateparameters
87*0Sstevel@tonic-gate
88*0Sstevel@tonic-gate=back
89*0Sstevel@tonic-gate
90*0Sstevel@tonic-gate=head2 Stability
91*0Sstevel@tonic-gate
92*0Sstevel@tonic-gate=over 4
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate=item *
95*0Sstevel@tonic-gate
96*0Sstevel@tonic-gateEnsure your module works under C<use strict> and C<-w>
97*0Sstevel@tonic-gate
98*0Sstevel@tonic-gate=item *
99*0Sstevel@tonic-gate
100*0Sstevel@tonic-gateStable modules should maintain backwards compatibility
101*0Sstevel@tonic-gate
102*0Sstevel@tonic-gate=back
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate=head2 Documentation
105*0Sstevel@tonic-gate
106*0Sstevel@tonic-gate=over 4
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate=item *
109*0Sstevel@tonic-gate
110*0Sstevel@tonic-gateWrite documentation in POD
111*0Sstevel@tonic-gate
112*0Sstevel@tonic-gate=item *
113*0Sstevel@tonic-gate
114*0Sstevel@tonic-gateDocument purpose, scope and target applications
115*0Sstevel@tonic-gate
116*0Sstevel@tonic-gate=item *
117*0Sstevel@tonic-gate
118*0Sstevel@tonic-gateDocument each publically accessible method or subroutine, including params and return values
119*0Sstevel@tonic-gate
120*0Sstevel@tonic-gate=item *
121*0Sstevel@tonic-gate
122*0Sstevel@tonic-gateGive examples of use in your documentation
123*0Sstevel@tonic-gate
124*0Sstevel@tonic-gate=item *
125*0Sstevel@tonic-gate
126*0Sstevel@tonic-gateProvide a README file and perhaps also release notes, changelog, etc
127*0Sstevel@tonic-gate
128*0Sstevel@tonic-gate=item *
129*0Sstevel@tonic-gate
130*0Sstevel@tonic-gateProvide links to further information (URL, email)
131*0Sstevel@tonic-gate
132*0Sstevel@tonic-gate=back
133*0Sstevel@tonic-gate
134*0Sstevel@tonic-gate=head2 Release considerations
135*0Sstevel@tonic-gate
136*0Sstevel@tonic-gate=over 4
137*0Sstevel@tonic-gate
138*0Sstevel@tonic-gate=item *
139*0Sstevel@tonic-gate
140*0Sstevel@tonic-gateSpecify pre-requisites in Makefile.PL or Build.PL
141*0Sstevel@tonic-gate
142*0Sstevel@tonic-gate=item *
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gateSpecify Perl version requirements with C<use>
145*0Sstevel@tonic-gate
146*0Sstevel@tonic-gate=item *
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gateInclude tests with your module
149*0Sstevel@tonic-gate
150*0Sstevel@tonic-gate=item *
151*0Sstevel@tonic-gate
152*0Sstevel@tonic-gateChoose a sensible and consistent version numbering scheme (X.YY is the common Perl module numbering scheme)
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate=item *
155*0Sstevel@tonic-gate
156*0Sstevel@tonic-gateIncrement the version number for every change, no matter how small
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate=item *
159*0Sstevel@tonic-gate
160*0Sstevel@tonic-gatePackage the module using "make dist"
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate=item *
163*0Sstevel@tonic-gate
164*0Sstevel@tonic-gateChoose an appropriate license (GPL/Artistic is a good default)
165*0Sstevel@tonic-gate
166*0Sstevel@tonic-gate=back
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate=head1 BEFORE YOU START WRITING A MODULE
169*0Sstevel@tonic-gate
170*0Sstevel@tonic-gateTry not to launch headlong into developing your module without spending
171*0Sstevel@tonic-gatesome time thinking first.  A little forethought may save you a vast
172*0Sstevel@tonic-gateamount of effort later on.
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate=head2 Has it been done before?
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gateYou may not even need to write the module.  Check whether it's already
177*0Sstevel@tonic-gatebeen done in Perl, and avoid re-inventing the wheel unless you have a
178*0Sstevel@tonic-gategood reason.
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gateGood places to look for pre-existing modules include
181*0Sstevel@tonic-gatehttp://search.cpan.org/ and asking on modules@perl.org
182*0Sstevel@tonic-gate
183*0Sstevel@tonic-gateIf an existing module B<almost> does what you want, consider writing a
184*0Sstevel@tonic-gatepatch, writing a subclass, or otherwise extending the existing module
185*0Sstevel@tonic-gaterather than rewriting it.
186*0Sstevel@tonic-gate
187*0Sstevel@tonic-gate=head2 Do one thing and do it well
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gateAt the risk of stating the obvious, modules are intended to be modular.
190*0Sstevel@tonic-gateA Perl developer should be able to use modules to put together the
191*0Sstevel@tonic-gatebuilding blocks of their application.  However, it's important that the
192*0Sstevel@tonic-gateblocks are the right shape, and that the developer shouldn't have to use
193*0Sstevel@tonic-gatea big block when all they need is a small one.
194*0Sstevel@tonic-gate
195*0Sstevel@tonic-gateYour module should have a clearly defined scope which is no longer than
196*0Sstevel@tonic-gatea single sentence.  Can your module be broken down into a family of
197*0Sstevel@tonic-gaterelated modules?
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gateBad example:
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate"FooBar.pm provides an implementation of the FOO protocol and the
202*0Sstevel@tonic-gaterelated BAR standard."
203*0Sstevel@tonic-gate
204*0Sstevel@tonic-gateGood example:
205*0Sstevel@tonic-gate
206*0Sstevel@tonic-gate"Foo.pm provides an implementation of the FOO protocol.  Bar.pm
207*0Sstevel@tonic-gateimplements the related BAR protocol."
208*0Sstevel@tonic-gate
209*0Sstevel@tonic-gateThis means that if a developer only needs a module for the BAR standard,
210*0Sstevel@tonic-gatethey should not be forced to install libraries for FOO as well.
211*0Sstevel@tonic-gate
212*0Sstevel@tonic-gate=head2 What's in a name?
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gateMake sure you choose an appropriate name for your module early on.  This
215*0Sstevel@tonic-gatewill help people find and remember your module, and make programming
216*0Sstevel@tonic-gatewith your module more intuitive.
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gateWhen naming your module, consider the following:
219*0Sstevel@tonic-gate
220*0Sstevel@tonic-gate=over 4
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate=item *
223*0Sstevel@tonic-gate
224*0Sstevel@tonic-gateBe descriptive (i.e. accurately describes the purpose of the module).
225*0Sstevel@tonic-gate
226*0Sstevel@tonic-gate=item *
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gateBe consistent with existing modules.
229*0Sstevel@tonic-gate
230*0Sstevel@tonic-gate=item *
231*0Sstevel@tonic-gate
232*0Sstevel@tonic-gateReflect the functionality of the module, not the implementation.
233*0Sstevel@tonic-gate
234*0Sstevel@tonic-gate=item *
235*0Sstevel@tonic-gate
236*0Sstevel@tonic-gateAvoid starting a new top-level hierarchy, especially if a suitable
237*0Sstevel@tonic-gatehierarchy already exists under which you could place your module.
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate=back
240*0Sstevel@tonic-gate
241*0Sstevel@tonic-gateYou should contact modules@perl.org to ask them about your module name
242*0Sstevel@tonic-gatebefore publishing your module.  You should also try to ask people who
243*0Sstevel@tonic-gateare already familiar with the module's application domain and the CPAN
244*0Sstevel@tonic-gatenaming system.  Authors of similar modules, or modules with similar
245*0Sstevel@tonic-gatenames, may be a good place to start.
246*0Sstevel@tonic-gate
247*0Sstevel@tonic-gate=head1 DESIGNING AND WRITING YOUR MODULE
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gateConsiderations for module design and coding:
250*0Sstevel@tonic-gate
251*0Sstevel@tonic-gate=head2 To OO or not to OO?
252*0Sstevel@tonic-gate
253*0Sstevel@tonic-gateYour module may be object oriented (OO) or not, or it may have both kinds
254*0Sstevel@tonic-gateof interfaces available.  There are pros and cons of each technique, which
255*0Sstevel@tonic-gateshould be considered when you design your API.
256*0Sstevel@tonic-gate
257*0Sstevel@tonic-gateAccording to Damian Conway, you should consider using OO:
258*0Sstevel@tonic-gate
259*0Sstevel@tonic-gate=over 4
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate=item *
262*0Sstevel@tonic-gate
263*0Sstevel@tonic-gateWhen the system is large or likely to become so
264*0Sstevel@tonic-gate
265*0Sstevel@tonic-gate=item *
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gateWhen the data is aggregated in obvious structures that will become objects
268*0Sstevel@tonic-gate
269*0Sstevel@tonic-gate=item *
270*0Sstevel@tonic-gate
271*0Sstevel@tonic-gateWhen the types of data form a natural hierarchy that can make use of inheritance
272*0Sstevel@tonic-gate
273*0Sstevel@tonic-gate=item *
274*0Sstevel@tonic-gate
275*0Sstevel@tonic-gateWhen operations on data vary according to data type (making
276*0Sstevel@tonic-gatepolymorphic invocation of methods feasible)
277*0Sstevel@tonic-gate
278*0Sstevel@tonic-gate=item *
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gateWhen it is likely that new data types may be later introduced
281*0Sstevel@tonic-gateinto the system, and will need to be handled by existing code
282*0Sstevel@tonic-gate
283*0Sstevel@tonic-gate=item *
284*0Sstevel@tonic-gate
285*0Sstevel@tonic-gateWhen interactions between data are best represented by
286*0Sstevel@tonic-gateoverloaded operators
287*0Sstevel@tonic-gate
288*0Sstevel@tonic-gate=item *
289*0Sstevel@tonic-gate
290*0Sstevel@tonic-gateWhen the implementation of system components is likely to
291*0Sstevel@tonic-gatechange over time (and hence should be encapsulated)
292*0Sstevel@tonic-gate
293*0Sstevel@tonic-gate=item *
294*0Sstevel@tonic-gate
295*0Sstevel@tonic-gateWhen the system design is itself object-oriented
296*0Sstevel@tonic-gate
297*0Sstevel@tonic-gate=item *
298*0Sstevel@tonic-gate
299*0Sstevel@tonic-gateWhen large amounts of client code will use the software (and
300*0Sstevel@tonic-gateshould be insulated from changes in its implementation)
301*0Sstevel@tonic-gate
302*0Sstevel@tonic-gate=item *
303*0Sstevel@tonic-gate
304*0Sstevel@tonic-gateWhen many separate operations will need to be applied to the
305*0Sstevel@tonic-gatesame set of data
306*0Sstevel@tonic-gate
307*0Sstevel@tonic-gate=back
308*0Sstevel@tonic-gate
309*0Sstevel@tonic-gateThink carefully about whether OO is appropriate for your module.
310*0Sstevel@tonic-gateGratuitous object orientation results in complex APIs which are
311*0Sstevel@tonic-gatedifficult for the average module user to understand or use.
312*0Sstevel@tonic-gate
313*0Sstevel@tonic-gate=head2 Designing your API
314*0Sstevel@tonic-gate
315*0Sstevel@tonic-gateYour interfaces should be understandable by an average Perl programmer.
316*0Sstevel@tonic-gateThe following guidelines may help you judge whether your API is
317*0Sstevel@tonic-gatesufficiently straightforward:
318*0Sstevel@tonic-gate
319*0Sstevel@tonic-gate=over 4
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate=item Write simple routines to do simple things.
322*0Sstevel@tonic-gate
323*0Sstevel@tonic-gateIt's better to have numerous simple routines than a few monolithic ones.
324*0Sstevel@tonic-gateIf your routine changes its behaviour significantly based on its
325*0Sstevel@tonic-gatearguments, it's a sign that you should have two (or more) separate
326*0Sstevel@tonic-gateroutines.
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate=item Separate functionality from output.
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gateReturn your results in the most generic form possible and allow the user
331*0Sstevel@tonic-gateto choose how to use them.  The most generic form possible is usually a
332*0Sstevel@tonic-gatePerl data structure which can then be used to generate a text report,
333*0Sstevel@tonic-gateHTML, XML, a database query, or whatever else your users require.
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gateIf your routine iterates through some kind of list (such as a list of
336*0Sstevel@tonic-gatefiles, or records in a database) you may consider providing a callback
337*0Sstevel@tonic-gateso that users can manipulate each element of the list in turn.
338*0Sstevel@tonic-gateFile::Find provides an example of this with its
339*0Sstevel@tonic-gateC<find(\&wanted, $dir)> syntax.
340*0Sstevel@tonic-gate
341*0Sstevel@tonic-gate=item Provide sensible shortcuts and defaults.
342*0Sstevel@tonic-gate
343*0Sstevel@tonic-gateDon't require every module user to jump through the same hoops to achieve a
344*0Sstevel@tonic-gatesimple result.  You can always include optional parameters or routines for
345*0Sstevel@tonic-gatemore complex or non-standard behaviour.  If most of your users have to
346*0Sstevel@tonic-gatetype a few almost identical lines of code when they start using your
347*0Sstevel@tonic-gatemodule, it's a sign that you should have made that behaviour a default.
348*0Sstevel@tonic-gateAnother good indicator that you should use defaults is if most of your
349*0Sstevel@tonic-gateusers call your routines with the same arguments.
350*0Sstevel@tonic-gate
351*0Sstevel@tonic-gate=item Naming conventions
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gateYour naming should be consistent.  For instance, it's better to have:
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate	display_day();
356*0Sstevel@tonic-gate	display_week();
357*0Sstevel@tonic-gate	display_year();
358*0Sstevel@tonic-gate
359*0Sstevel@tonic-gatethan
360*0Sstevel@tonic-gate
361*0Sstevel@tonic-gate	display_day();
362*0Sstevel@tonic-gate	week_display();
363*0Sstevel@tonic-gate	show_year();
364*0Sstevel@tonic-gate
365*0Sstevel@tonic-gateThis applies equally to method names, parameter names, and anything else
366*0Sstevel@tonic-gatewhich is visible to the user (and most things that aren't!)
367*0Sstevel@tonic-gate
368*0Sstevel@tonic-gate=item Parameter passing
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gateUse named parameters. It's easier to use a hash like this:
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate    $obj->do_something(
373*0Sstevel@tonic-gate	    name => "wibble",
374*0Sstevel@tonic-gate	    type => "text",
375*0Sstevel@tonic-gate	    size => 1024,
376*0Sstevel@tonic-gate    );
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate... than to have a long list of unnamed parameters like this:
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate    $obj->do_something("wibble", "text", 1024);
381*0Sstevel@tonic-gate
382*0Sstevel@tonic-gateWhile the list of arguments might work fine for one, two or even three
383*0Sstevel@tonic-gatearguments, any more arguments become hard for the module user to
384*0Sstevel@tonic-gateremember, and hard for the module author to manage.  If you want to add
385*0Sstevel@tonic-gatea new parameter you will have to add it to the end of the list for
386*0Sstevel@tonic-gatebackward compatibility, and this will probably make your list order
387*0Sstevel@tonic-gateunintuitive.  Also, if many elements may be undefined you may see the
388*0Sstevel@tonic-gatefollowing unattractive method calls:
389*0Sstevel@tonic-gate
390*0Sstevel@tonic-gate    $obj->do_something(undef, undef, undef, undef, undef, undef, 1024);
391*0Sstevel@tonic-gate
392*0Sstevel@tonic-gateProvide sensible defaults for parameters which have them.  Don't make
393*0Sstevel@tonic-gateyour users specify parameters which will almost always be the same.
394*0Sstevel@tonic-gate
395*0Sstevel@tonic-gateThe issue of whether to pass the arguments in a hash or a hashref is
396*0Sstevel@tonic-gatelargely a matter of personal style.
397*0Sstevel@tonic-gate
398*0Sstevel@tonic-gateThe use of hash keys starting with a hyphen (C<-name>) or entirely in
399*0Sstevel@tonic-gateupper case (C<NAME>) is a relic of older versions of Perl in which
400*0Sstevel@tonic-gateordinary lower case strings were not handled correctly by the C<=E<gt>>
401*0Sstevel@tonic-gateoperator.  While some modules retain uppercase or hyphenated argument
402*0Sstevel@tonic-gatekeys for historical reasons or as a matter of personal style, most new
403*0Sstevel@tonic-gatemodules should use simple lower case keys.  Whatever you choose, be
404*0Sstevel@tonic-gateconsistent!
405*0Sstevel@tonic-gate
406*0Sstevel@tonic-gate=back
407*0Sstevel@tonic-gate
408*0Sstevel@tonic-gate=head2 Strictness and warnings
409*0Sstevel@tonic-gate
410*0Sstevel@tonic-gateYour module should run successfully under the strict pragma and should
411*0Sstevel@tonic-gaterun without generating any warnings.  Your module should also handle
412*0Sstevel@tonic-gatetaint-checking where appropriate, though this can cause difficulties in
413*0Sstevel@tonic-gatemany cases.
414*0Sstevel@tonic-gate
415*0Sstevel@tonic-gate=head2 Backwards compatibility
416*0Sstevel@tonic-gate
417*0Sstevel@tonic-gateModules which are "stable" should not break backwards compatibility
418*0Sstevel@tonic-gatewithout at least a long transition phase and a major change in version
419*0Sstevel@tonic-gatenumber.
420*0Sstevel@tonic-gate
421*0Sstevel@tonic-gate=head2 Error handling and messages
422*0Sstevel@tonic-gate
423*0Sstevel@tonic-gateWhen your module encounters an error it should do one or more of:
424*0Sstevel@tonic-gate
425*0Sstevel@tonic-gate=over 4
426*0Sstevel@tonic-gate
427*0Sstevel@tonic-gate=item *
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gateReturn an undefined value.
430*0Sstevel@tonic-gate
431*0Sstevel@tonic-gate=item *
432*0Sstevel@tonic-gate
433*0Sstevel@tonic-gateset C<$Module::errstr> or similar (C<errstr> is a common name used by
434*0Sstevel@tonic-gateDBI and other popular modules; if you choose something else, be sure to
435*0Sstevel@tonic-gatedocument it clearly).
436*0Sstevel@tonic-gate
437*0Sstevel@tonic-gate=item *
438*0Sstevel@tonic-gate
439*0Sstevel@tonic-gateC<warn()> or C<carp()> a message to STDERR.
440*0Sstevel@tonic-gate
441*0Sstevel@tonic-gate=item *
442*0Sstevel@tonic-gate
443*0Sstevel@tonic-gateC<croak()> only when your module absolutely cannot figure out what to
444*0Sstevel@tonic-gatedo.  (C<croak()> is a better version of C<die()> for use within
445*0Sstevel@tonic-gatemodules, which reports its errors from the perspective of the caller.
446*0Sstevel@tonic-gateSee L<Carp> for details of C<croak()>, C<carp()> and other useful
447*0Sstevel@tonic-gateroutines.)
448*0Sstevel@tonic-gate
449*0Sstevel@tonic-gate=item *
450*0Sstevel@tonic-gate
451*0Sstevel@tonic-gateAs an alternative to the above, you may prefer to throw exceptions using
452*0Sstevel@tonic-gatethe Error module.
453*0Sstevel@tonic-gate
454*0Sstevel@tonic-gate=back
455*0Sstevel@tonic-gate
456*0Sstevel@tonic-gateConfigurable error handling can be very useful to your users.  Consider
457*0Sstevel@tonic-gateoffering a choice of levels for warning and debug messages, an option to
458*0Sstevel@tonic-gatesend messages to a separate file, a way to specify an error-handling
459*0Sstevel@tonic-gateroutine, or other such features.  Be sure to default all these options
460*0Sstevel@tonic-gateto the commonest use.
461*0Sstevel@tonic-gate
462*0Sstevel@tonic-gate=head1 DOCUMENTING YOUR MODULE
463*0Sstevel@tonic-gate
464*0Sstevel@tonic-gate=head2 POD
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gateYour module should include documentation aimed at Perl developers.
467*0Sstevel@tonic-gateYou should use Perl's "plain old documentation" (POD) for your general
468*0Sstevel@tonic-gatetechnical documentation, though you may wish to write additional
469*0Sstevel@tonic-gatedocumentation (white papers, tutorials, etc) in some other format.
470*0Sstevel@tonic-gateYou need to cover the following subjects:
471*0Sstevel@tonic-gate
472*0Sstevel@tonic-gate=over 4
473*0Sstevel@tonic-gate
474*0Sstevel@tonic-gate=item *
475*0Sstevel@tonic-gate
476*0Sstevel@tonic-gateA synopsis of the common uses of the module
477*0Sstevel@tonic-gate
478*0Sstevel@tonic-gate=item *
479*0Sstevel@tonic-gate
480*0Sstevel@tonic-gateThe purpose, scope and target applications of your module
481*0Sstevel@tonic-gate
482*0Sstevel@tonic-gate=item *
483*0Sstevel@tonic-gate
484*0Sstevel@tonic-gateUse of each publically accessible method or subroutine, including
485*0Sstevel@tonic-gateparameters and return values
486*0Sstevel@tonic-gate
487*0Sstevel@tonic-gate=item *
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gateExamples of use
490*0Sstevel@tonic-gate
491*0Sstevel@tonic-gate=item *
492*0Sstevel@tonic-gate
493*0Sstevel@tonic-gateSources of further information
494*0Sstevel@tonic-gate
495*0Sstevel@tonic-gate=item *
496*0Sstevel@tonic-gate
497*0Sstevel@tonic-gateA contact email address for the author/maintainer
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate=back
500*0Sstevel@tonic-gate
501*0Sstevel@tonic-gateThe level of detail in Perl module documentation generally goes from
502*0Sstevel@tonic-gateless detailed to more detailed.  Your SYNOPSIS section should contain a
503*0Sstevel@tonic-gateminimal example of use (perhaps as little as one line of code; skip the
504*0Sstevel@tonic-gateunusual use cases or anything not needed by most users); the
505*0Sstevel@tonic-gateDESCRIPTION should describe your module in broad terms, generally in
506*0Sstevel@tonic-gatejust a few paragraphs; more detail of the module's routines or methods,
507*0Sstevel@tonic-gatelengthy code examples, or other in-depth material should be given in
508*0Sstevel@tonic-gatesubsequent sections.
509*0Sstevel@tonic-gate
510*0Sstevel@tonic-gateIdeally, someone who's slightly familiar with your module should be able
511*0Sstevel@tonic-gateto refresh their memory without hitting "page down".  As your reader
512*0Sstevel@tonic-gatecontinues through the document, they should receive a progressively
513*0Sstevel@tonic-gategreater amount of knowledge.
514*0Sstevel@tonic-gate
515*0Sstevel@tonic-gateThe recommended order of sections in Perl module documentation is:
516*0Sstevel@tonic-gate
517*0Sstevel@tonic-gate=over 4
518*0Sstevel@tonic-gate
519*0Sstevel@tonic-gate=item *
520*0Sstevel@tonic-gate
521*0Sstevel@tonic-gateNAME
522*0Sstevel@tonic-gate
523*0Sstevel@tonic-gate=item *
524*0Sstevel@tonic-gate
525*0Sstevel@tonic-gateSYNOPSIS
526*0Sstevel@tonic-gate
527*0Sstevel@tonic-gate=item *
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gateDESCRIPTION
530*0Sstevel@tonic-gate
531*0Sstevel@tonic-gate=item *
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gateOne or more sections or subsections giving greater detail of available
534*0Sstevel@tonic-gatemethods and routines and any other relevant information.
535*0Sstevel@tonic-gate
536*0Sstevel@tonic-gate=item *
537*0Sstevel@tonic-gate
538*0Sstevel@tonic-gateBUGS/CAVEATS/etc
539*0Sstevel@tonic-gate
540*0Sstevel@tonic-gate=item *
541*0Sstevel@tonic-gate
542*0Sstevel@tonic-gateAUTHOR
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate=item *
545*0Sstevel@tonic-gate
546*0Sstevel@tonic-gateSEE ALSO
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate=item *
549*0Sstevel@tonic-gate
550*0Sstevel@tonic-gateCOPYRIGHT and LICENSE
551*0Sstevel@tonic-gate
552*0Sstevel@tonic-gate=back
553*0Sstevel@tonic-gate
554*0Sstevel@tonic-gateKeep your documentation near the code it documents ("inline"
555*0Sstevel@tonic-gatedocumentation).  Include POD for a given method right above that
556*0Sstevel@tonic-gatemethod's subroutine.  This makes it easier to keep the documentation up
557*0Sstevel@tonic-gateto date, and avoids having to document each piece of code twice (once in
558*0Sstevel@tonic-gatePOD and once in comments).
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate=head2 README, INSTALL, release notes, changelogs
561*0Sstevel@tonic-gate
562*0Sstevel@tonic-gateYour module should also include a README file describing the module and
563*0Sstevel@tonic-gategiving pointers to further information (website, author email).
564*0Sstevel@tonic-gate
565*0Sstevel@tonic-gateAn INSTALL file should be included, and should contain simple installation
566*0Sstevel@tonic-gateinstructions. When using ExtUtils::MakeMaker this will usually be:
567*0Sstevel@tonic-gate
568*0Sstevel@tonic-gate=over 4
569*0Sstevel@tonic-gate
570*0Sstevel@tonic-gate=item perl Makefile.PL
571*0Sstevel@tonic-gate
572*0Sstevel@tonic-gate=item make
573*0Sstevel@tonic-gate
574*0Sstevel@tonic-gate=item make test
575*0Sstevel@tonic-gate
576*0Sstevel@tonic-gate=item make install
577*0Sstevel@tonic-gate
578*0Sstevel@tonic-gate=back
579*0Sstevel@tonic-gate
580*0Sstevel@tonic-gateWhen using Module::Build, this will usually be:
581*0Sstevel@tonic-gate
582*0Sstevel@tonic-gate=over 4
583*0Sstevel@tonic-gate
584*0Sstevel@tonic-gate=item perl Build.PL
585*0Sstevel@tonic-gate
586*0Sstevel@tonic-gate=item perl Build
587*0Sstevel@tonic-gate
588*0Sstevel@tonic-gate=item perl Build test
589*0Sstevel@tonic-gate
590*0Sstevel@tonic-gate=item perl Build install
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate=back
593*0Sstevel@tonic-gate
594*0Sstevel@tonic-gateRelease notes or changelogs should be produced for each release of your
595*0Sstevel@tonic-gatesoftware describing user-visible changes to your module, in terms
596*0Sstevel@tonic-gaterelevant to the user.
597*0Sstevel@tonic-gate
598*0Sstevel@tonic-gate=head1 RELEASE CONSIDERATIONS
599*0Sstevel@tonic-gate
600*0Sstevel@tonic-gate=head2 Version numbering
601*0Sstevel@tonic-gate
602*0Sstevel@tonic-gateVersion numbers should indicate at least major and minor releases, and
603*0Sstevel@tonic-gatepossibly sub-minor releases.  A major release is one in which most of
604*0Sstevel@tonic-gatethe functionality has changed, or in which major new functionality is
605*0Sstevel@tonic-gateadded.  A minor release is one in which a small amount of functionality
606*0Sstevel@tonic-gatehas been added or changed.  Sub-minor version numbers are usually used
607*0Sstevel@tonic-gatefor changes which do not affect functionality, such as documentation
608*0Sstevel@tonic-gatepatches.
609*0Sstevel@tonic-gate
610*0Sstevel@tonic-gateThe most common CPAN version numbering scheme looks like this:
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate    1.00, 1.10, 1.11, 1.20, 1.30, 1.31, 1.32
613*0Sstevel@tonic-gate
614*0Sstevel@tonic-gateA correct CPAN version number is a floating point number with at least
615*0Sstevel@tonic-gate2 digits after the decimal. You can test whether it conforms to CPAN by
616*0Sstevel@tonic-gateusing
617*0Sstevel@tonic-gate
618*0Sstevel@tonic-gate    perl -MExtUtils::MakeMaker -le 'print MM->parse_version(shift)' 'Foo.pm'
619*0Sstevel@tonic-gate
620*0Sstevel@tonic-gateIf you want to release a 'beta' or 'alpha' version of a module but
621*0Sstevel@tonic-gatedon't want CPAN.pm to list it as most recent use an '_' after the
622*0Sstevel@tonic-gateregular version number followed by at least 2 digits, eg. 1.20_01. If
623*0Sstevel@tonic-gateyou do this, the following idiom is recommended:
624*0Sstevel@tonic-gate
625*0Sstevel@tonic-gate  $VERSION = "1.12_01";
626*0Sstevel@tonic-gate  $XS_VERSION = $VERSION; # only needed if you have XS code
627*0Sstevel@tonic-gate  $VERSION = eval $VERSION;
628*0Sstevel@tonic-gate
629*0Sstevel@tonic-gateWith that trick MakeMaker will only read the first line and thus read
630*0Sstevel@tonic-gatethe underscore, while the perl interpreter will evaluate the $VERSION
631*0Sstevel@tonic-gateand convert the string into a number. Later operations that treat
632*0Sstevel@tonic-gate$VERSION as a number will then be able to do so without provoking a
633*0Sstevel@tonic-gatewarning about $VERSION not being a number.
634*0Sstevel@tonic-gate
635*0Sstevel@tonic-gateNever release anything (even a one-word documentation patch) without
636*0Sstevel@tonic-gateincrementing the number.  Even a one-word documentation patch should
637*0Sstevel@tonic-gateresult in a change in version at the sub-minor level.
638*0Sstevel@tonic-gate
639*0Sstevel@tonic-gate=head2 Pre-requisites
640*0Sstevel@tonic-gate
641*0Sstevel@tonic-gateModule authors should carefully consider whether to rely on other
642*0Sstevel@tonic-gatemodules, and which modules to rely on.
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gateMost importantly, choose modules which are as stable as possible.  In
645*0Sstevel@tonic-gateorder of preference:
646*0Sstevel@tonic-gate
647*0Sstevel@tonic-gate=over 4
648*0Sstevel@tonic-gate
649*0Sstevel@tonic-gate=item *
650*0Sstevel@tonic-gate
651*0Sstevel@tonic-gateCore Perl modules
652*0Sstevel@tonic-gate
653*0Sstevel@tonic-gate=item *
654*0Sstevel@tonic-gate
655*0Sstevel@tonic-gateStable CPAN modules
656*0Sstevel@tonic-gate
657*0Sstevel@tonic-gate=item *
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gateUnstable CPAN modules
660*0Sstevel@tonic-gate
661*0Sstevel@tonic-gate=item *
662*0Sstevel@tonic-gate
663*0Sstevel@tonic-gateModules not available from CPAN
664*0Sstevel@tonic-gate
665*0Sstevel@tonic-gate=back
666*0Sstevel@tonic-gate
667*0Sstevel@tonic-gateSpecify version requirements for other Perl modules in the
668*0Sstevel@tonic-gatepre-requisites in your Makefile.PL or Build.PL.
669*0Sstevel@tonic-gate
670*0Sstevel@tonic-gateBe sure to specify Perl version requirements both in Makefile.PL or
671*0Sstevel@tonic-gateBuild.PL and with C<require 5.6.1> or similar. See the section on
672*0Sstevel@tonic-gateC<use VERSION> of L<perlfunc/require> for details.
673*0Sstevel@tonic-gate
674*0Sstevel@tonic-gate=head2 Testing
675*0Sstevel@tonic-gate
676*0Sstevel@tonic-gateAll modules should be tested before distribution (using "make disttest"),
677*0Sstevel@tonic-gateand the tests should also be available to people installing the modules
678*0Sstevel@tonic-gate(using "make test").
679*0Sstevel@tonic-gateFor Module::Build you would use the C<make test> equivalent C<perl Build test>.
680*0Sstevel@tonic-gate
681*0Sstevel@tonic-gateThe importance of these tests is proportional to the alleged stability of a
682*0Sstevel@tonic-gatemodule -- a module which purports to be stable or which hopes to achieve wide
683*0Sstevel@tonic-gateuse should adhere to as strict a testing regime as possible.
684*0Sstevel@tonic-gate
685*0Sstevel@tonic-gateUseful modules to help you write tests (with minimum impact on your
686*0Sstevel@tonic-gatedevelopment process or your time) include Test::Simple, Carp::Assert
687*0Sstevel@tonic-gateand Test::Inline.
688*0Sstevel@tonic-gateFor more sophisticated test suites there are Test::More and Test::MockObject.
689*0Sstevel@tonic-gate
690*0Sstevel@tonic-gate=head2 Packaging
691*0Sstevel@tonic-gate
692*0Sstevel@tonic-gateModules should be packaged using one of the standard packaging tools.
693*0Sstevel@tonic-gateCurrently you have the choice between ExtUtils::MakeMaker and the
694*0Sstevel@tonic-gatemore platform independent Module::Build, allowing modules to be installed in a
695*0Sstevel@tonic-gateconsistent manner.
696*0Sstevel@tonic-gateWhen using ExtUtils::MakeMaker, you can use "make dist" to create your
697*0Sstevel@tonic-gatepackage. Tools exist to help you to build your module in a MakeMaker-friendly
698*0Sstevel@tonic-gatestyle. These include ExtUtils::ModuleMaker and h2xs.  See also L<perlnewmod>.
699*0Sstevel@tonic-gate
700*0Sstevel@tonic-gate=head2 Licensing
701*0Sstevel@tonic-gate
702*0Sstevel@tonic-gateMake sure that your module has a license, and that the full text of it
703*0Sstevel@tonic-gateis included in the distribution (unless it's a common one and the terms
704*0Sstevel@tonic-gateof the license don't require you to include it).
705*0Sstevel@tonic-gate
706*0Sstevel@tonic-gateIf you don't know what license to use, dual licensing under the GPL
707*0Sstevel@tonic-gateand Artistic licenses (the same as Perl itself) is a good idea.
708*0Sstevel@tonic-gateSee L<perlgpl> and L<perlartistic>.
709*0Sstevel@tonic-gate
710*0Sstevel@tonic-gate=head1 COMMON PITFALLS
711*0Sstevel@tonic-gate
712*0Sstevel@tonic-gate=head2 Reinventing the wheel
713*0Sstevel@tonic-gate
714*0Sstevel@tonic-gateThere are certain application spaces which are already very, very well
715*0Sstevel@tonic-gateserved by CPAN.  One example is templating systems, another is date and
716*0Sstevel@tonic-gatetime modules, and there are many more.  While it is a rite of passage to
717*0Sstevel@tonic-gatewrite your own version of these things, please consider carefully
718*0Sstevel@tonic-gatewhether the Perl world really needs you to publish it.
719*0Sstevel@tonic-gate
720*0Sstevel@tonic-gate=head2 Trying to do too much
721*0Sstevel@tonic-gate
722*0Sstevel@tonic-gateYour module will be part of a developer's toolkit.  It will not, in
723*0Sstevel@tonic-gateitself, form the B<entire> toolkit.  It's tempting to add extra features
724*0Sstevel@tonic-gateuntil your code is a monolithic system rather than a set of modular
725*0Sstevel@tonic-gatebuilding blocks.
726*0Sstevel@tonic-gate
727*0Sstevel@tonic-gate=head2 Inappropriate documentation
728*0Sstevel@tonic-gate
729*0Sstevel@tonic-gateDon't fall into the trap of writing for the wrong audience.  Your
730*0Sstevel@tonic-gateprimary audience is a reasonably experienced developer with at least
731*0Sstevel@tonic-gatea moderate understanding of your module's application domain, who's just
732*0Sstevel@tonic-gatedownloaded your module and wants to start using it as quickly as possible.
733*0Sstevel@tonic-gate
734*0Sstevel@tonic-gateTutorials, end-user documentation, research papers, FAQs etc are not
735*0Sstevel@tonic-gateappropriate in a module's main documentation.  If you really want to
736*0Sstevel@tonic-gatewrite these, include them as sub-documents such as C<My::Module::Tutorial> or
737*0Sstevel@tonic-gateC<My::Module::FAQ> and provide a link in the SEE ALSO section of the
738*0Sstevel@tonic-gatemain documentation.
739*0Sstevel@tonic-gate
740*0Sstevel@tonic-gate=head1 SEE ALSO
741*0Sstevel@tonic-gate
742*0Sstevel@tonic-gate=over 4
743*0Sstevel@tonic-gate
744*0Sstevel@tonic-gate=item L<perlstyle>
745*0Sstevel@tonic-gate
746*0Sstevel@tonic-gateGeneral Perl style guide
747*0Sstevel@tonic-gate
748*0Sstevel@tonic-gate=item L<perlnewmod>
749*0Sstevel@tonic-gate
750*0Sstevel@tonic-gateHow to create a new module
751*0Sstevel@tonic-gate
752*0Sstevel@tonic-gate=item L<perlpod>
753*0Sstevel@tonic-gate
754*0Sstevel@tonic-gatePOD documentation
755*0Sstevel@tonic-gate
756*0Sstevel@tonic-gate=item L<podchecker>
757*0Sstevel@tonic-gate
758*0Sstevel@tonic-gateVerifies your POD's correctness
759*0Sstevel@tonic-gate
760*0Sstevel@tonic-gate=item Packaging Tools
761*0Sstevel@tonic-gate
762*0Sstevel@tonic-gateL<ExtUtils::MakeMaker>, L<Module::Build>
763*0Sstevel@tonic-gate
764*0Sstevel@tonic-gate=item Testing tools
765*0Sstevel@tonic-gate
766*0Sstevel@tonic-gateL<Test::Simple>, L<Test::Inline>, L<Carp::Assert>, L<Test::More>, L<Test::MockObject>
767*0Sstevel@tonic-gate
768*0Sstevel@tonic-gate=item http://pause.perl.org/
769*0Sstevel@tonic-gate
770*0Sstevel@tonic-gatePerl Authors Upload Server.  Contains links to information for module
771*0Sstevel@tonic-gateauthors.
772*0Sstevel@tonic-gate
773*0Sstevel@tonic-gate=item Any good book on software engineering
774*0Sstevel@tonic-gate
775*0Sstevel@tonic-gate=back
776*0Sstevel@tonic-gate
777*0Sstevel@tonic-gate=head1 AUTHOR
778*0Sstevel@tonic-gate
779*0Sstevel@tonic-gateKirrily "Skud" Robert <skud@cpan.org>
780*0Sstevel@tonic-gate
781