This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
[PATCH] the the double double word word fix fix
[perl5.git] / pod / perlmodstyle.pod
CommitLineData
f67486be
KR
1=head1 NAME
2
3perlmodstyle - Perl module style guide
4
5=head1 INTRODUCTION
6
7This document attempts to describe the Perl Community's "best practice"
8for writing Perl modules. It extends the recommendations found in
9L<perlstyle> , which should be considered required reading
10before reading this document.
11
12While this document is intended to be useful to all module authors, it is
13particularly aimed at authors who wish to publish their modules on CPAN.
14
15The focus is on elements of style which are visible to the users of a
16module, rather than those parts which are only seen by the module's
17developers. However, many of the guidelines presented in this document
18can be extrapolated and applied successfully to a module's internals.
19
20This document differs from L<perlnewmod> in that it is a style guide
21rather than a tutorial on creating CPAN modules. It provides a
22checklist against which modules can be compared to determine whether
23they conform to best practice, without necessarily describing in detail
24how to achieve this.
25
26All the advice contained in this document has been gleaned from
27extensive conversations with experienced CPAN authors and users. Every
28piece of advice given here is the result of previous mistakes. This
29information is here to help you avoid the same mistakes and the extra
30work that would inevitably be required to fix them.
31
32The first section of this document provides an itemized checklist;
33subsequent sections provide a more detailed discussion of the items on
34the list. The final section, "Common Pitfalls", describes some of the
35most popular mistakes made by CPAN authors.
36
37=head1 QUICK CHECKLIST
38
39For more detail on each item in this checklist, see below.
40
41=head2 Before you start
42
43=over 4
44
45=item *
46
47Don't re-invent the wheel
48
49=item *
50
51Patch, extend or subclass an existing module where possible
52
53=item *
54
55Do one thing and do it well
56
57=item *
58
59Choose an appropriate name
60
61=back
62
63=head2 The API
64
65=over 4
66
67=item *
68
69API should be understandable by the average programmer
70
71=item *
72
73Simple methods for simple tasks
74
75=item *
76
77Separate functionality from output
78
79=item *
80
81Consistent naming of subroutines or methods
82
83=item *
84
85Use named parameters (a hash or hashref) when there are more than two
86parameters
87
88=back
89
90=head2 Stability
91
92=over 4
93
94=item *
95
96Ensure your module works under C<use strict> and C<-w>
97
98=item *
99
100Stable modules should maintain backwards compatibility
101
102=back
103
104=head2 Documentation
105
106=over 4
107
108=item *
109
110Write documentation in POD
111
112=item *
113
114Document purpose, scope and target applications
115
116=item *
117
118Document each publically accessible method or subroutine, including params and return values
119
120=item *
121
122Give examples of use in your documentation
123
124=item *
125
126Provide a README file and perhaps also release notes, changelog, etc
127
128=item *
129
130Provide links to further information (URL, email)
131
132=back
133
134=head2 Release considerations
135
136=over 4
137
138=item *
139
140Specify pre-requisites in Makefile.PL
141
142=item *
143
144Specify Perl version requirements with C<use>
145
146=item *
147
148Include tests with your module
149
150=item *
151
152Choose a sensible and consistent version numbering scheme (X.YY is the common Perl module numbering scheme)
153
154=item *
155
156Increment the version number for every change, no matter how small
157
158=item *
159
160Package the module using "make dist"
161
162=item *
163
164Choose an appropriate license (GPL/Artistic is a good default)
165
166=back
167
168=head1 BEFORE YOU START WRITING A MODULE
169
170Try not to launch headlong into developing your module without spending
171some time thinking first. A little forethought may save you a vast
172amount of effort later on.
173
174=head2 Has it been done before?
175
176You may not even need to write the module. Check whether it's already
177been done in Perl, and avoid re-inventing the wheel unless you have a
178good reason.
179
180If an existing module B<almost> does what you want, consider writing a
181patch, writing a subclass, or otherwise extending the existing module
182rather than rewriting it.
183
184=head2 Do one thing and do it well
185
186At the risk of stating the obvious, modules are intended to be modular.
187A Perl developer should be able to use modules to put together the
188building blocks of their application. However, it's important that the
189blocks are the right shape, and that the developer shouldn't have to use
190a big block when all they need is a small one.
191
192Your module should have a clearly defined scope which is no longer than
193a single sentence. Can your module be broken down into a family of
194related modules?
195
196Bad example:
197
198"FooBar.pm provides an implementation of the FOO protocol and the
199related BAR standard."
200
201Good example:
202
203"Foo.pm provides an implementation of the FOO protocol. Bar.pm
204implements the related BAR protocol."
205
206This means that if a developer only needs a module for the BAR standard,
207they should not be forced to install libraries for FOO as well.
208
209=head2 What's in a name?
210
211Make sure you choose an appropriate name for your module early on. This
212will help people find and remember your module, and make programming
213with your module more intuitive.
214
215When naming your module, consider the following:
216
217=over 4
218
219=item *
220
221Be descriptive (i.e. accurately describes the purpose of the module).
222
223=item *
224
225Be consistent with existing modules.
226
227=item *
228
229Reflect the functionality of the module, not the implementation.
230
231=item *
232
233Avoid starting a new top-level hierarchy, especially if a suitable
234hierarchy already exists under which you could place your module.
235
236=back
237
238You should contact modules@perl.org to ask them about your module name
239before publishing your module. You should also try to ask people who
240are already familiar with the module's application domain and the CPAN
241naming system. Authors of similar modules, or modules with similar
242names, may be a good place to start.
243
244=head1 DESIGNING AND WRITING YOUR MODULE
245
246Considerations for module design and coding:
247
248=head2 To OO or not to OO?
249
250Your module may be object oriented (OO) or not, or it may have both kinds
251of interfaces available. There are pros and cons of each technique, which
252should be considered when you design your API.
253
254According to Damian Conway, you should consider using OO:
255
256=over 4
257
258=item *
259
260When the system is large or likely to become so
261
262=item *
263
264When the data is aggregated in obvious structures that will become objects
265
266=item *
267
268When the types of data form a natural hierarchy that can make use of inheritance
269
270=item *
271
272When operations on data vary according to data type (making
273polymorphic invocation of methods feasible)
274
275=item *
276
277When it is likely that new data types may be later introduced
278into the system, and will need to be handled by existing code
279
280=item *
281
282When interactions between data are best represented by
283overloaded operators
284
285=item *
286
287When the implementation of system components is likely to
288change over time (and hence should be encapsulated)
289
290=item *
291
292When the system design is itself object-oriented
293
294=item *
295
296When large amounts of client code will use the software (and
297should be insulated from changes in its implementation)
298
299=item *
300
301When many separate operations will need to be applied to the
302same set of data
303
304=back
305
306Think carefully about whether OO is appropriate for your module.
307Gratuitous object orientation results in complex APIs which are
308difficult for the average module user to understand or use.
309
310=head2 Designing your API
311
312Your interfaces should be understandable by an average Perl programmer.
313The following guidelines may help you judge whether your API is
314sufficiently straightforward:
315
316=over 4
317
318=item Write simple routines to do simple things.
319
320It's better to have numerous simple routines than a few monolithic ones.
321If your routine changes its behaviour significantly based on its
322arguments, it's a sign that you should have two (or more) separate
323routines.
324
325=item Separate functionality from output.
326
327Return your results in the most generic form possible and allow the user
328to choose how to use them. The most generic form possible is usually a
329Perl data structure which can then be used to generate a text report,
330HTML, XML, a database query, or whatever else your users require.
331
332If your routine iterates through some kind of list (such as a list of
333files, or records in a database) you may consider providing a callback
334so that users can manipulate each element of the list in turn.
335File::Find provides an example of this with its
336C<find(\&wanted, $dir)> syntax.
337
338=item Provide sensible shortcuts and defaults.
339
340Don't require every module user to jump through the same hoops to achieve a
341simple result. You can always include optional parameters or routines for
342more complex or non-standard behaviour. If most of your users have to
343type a few almost identical lines of code when they start using your
344module, it's a sign that you should have made that behaviour a default.
345Another good indicator that you should use defaults is if most of your
346users call your routines with the same arguments.
347
348=item Naming conventions
349
350Your naming should be consistent. For instance, it's better to have:
351
352 display_day();
353 display_week();
354 display_year();
355
356than
357
358 display_day();
359 week_display();
360 show_year();
361
362This applies equally to method names, parameter names, and anything else
363which is visible to the user (and most things that aren't!)
364
365=item Parameter passing
366
367Use named parameters. It's easier to use a hash like this:
368
369 $obj->do_something(
370 name => "wibble",
371 type => "text",
372 size => 1024,
373 );
374
375... than to have a long list of unnamed parameters like this:
376
377 $obj->do_something("wibble", "text", 1024);
378
379While the list of arguments might work fine for one, two or even three
380arguments, any more arguments become hard for the module user to
381remember, and hard for the module author to manage. If you want to add
382a new parameter you will have to add it to the end of the list for
383backward compatibility, and this will probably make your list order
384unintuitive. Also, if many elements may be undefined you may see the
385following unattractive method calls:
386
387 $obj->do_something(undef, undef, undef, undef, undef, undef, 1024);
388
389Provide sensible defaults for parameters which have them. Don't make
390your users specify parameters which will almost always be the same.
391
392The issue of whether to pass the arguments in a hash or a hashref is
393largely a matter of personal style.
394
395The use of hash keys starting with a hyphen (C<-name>) or entirely in
396upper case (C<NAME>) is a relic of older versions of Perl in which
397ordinary lower case strings were not handled correctly by the C<=E<gt>>
398operator. While some modules retain uppercase or hyphenated argument
399keys for historical reasons or as a matter of personal style, most new
400modules should use simple lower case keys. Whatever you choose, be
401consistent!
402
403=back
404
405=head2 Strictness and warnings
406
407Your module should run successfully under the strict pragma and should
408run without generating any warnings. Your module should also handle
409taint-checking where appropriate, though this can cause difficulties in
410many cases.
411
412=head2 Backwards compatibility
413
414Modules which are "stable" should not break backwards compatibility
415without at least a long transition phase and a major change in version
416number.
417
418=head2 Error handling and messages
419
420When your module encounters an error it should do one or more of:
421
422=over 4
423
424=item *
425
426Return an undefined value.
427
428=item *
429
430set C<$Module::errstr> or similar (C<errstr> is a common name used by
431DBI and other popular modules; if you choose something else, be sure to
432document it clearly).
433
434=item *
435
436C<warn()> or C<carp()> a message to STDERR.
437
438=item *
439
440C<croak()> only when your module absolutely cannot figure out what to
441do. (C<croak()> is a better version of C<die()> for use within
442modules, which reports its errors from the perspective of the caller.
443See L<Carp> for details of C<croak()>, C<carp()> and other useful
444routines.)
445
446=item *
447
448As an alternative to the above, you may prefer to throw exceptions using
449the Error module.
450
451=back
452
453Configurable error handling can be very useful to your users. Consider
454offering a choice of levels for warning and debug messages, an option to
455send messages to a separate file, a way to specify an error-handling
456routine, or other such features. Be sure to default all these options
457to the commonest use.
458
459=head1 DOCUMENTING YOUR MODULE
460
461=head2 POD
462
463Your module should include documentation aimed at Perl developers.
464You should use Perl's "plain old documentation" (POD) for your general
465technical documentation, though you may wish to write additional
466documentation (white papers, tutorials, etc) in some other format.
467You need to cover the following subjects:
468
469=over 4
470
471=item *
472
473A synopsis of the common uses of the module
474
475=item *
476
477The purpose, scope and target applications of your module
478
479=item *
480
481Use of each publically accessible method or subroutine, including
482parameters and return values
483
484=item *
485
486Examples of use
487
488=item *
489
490Sources of further information
491
492=item *
493
494A contact email address for the author/maintainer
495
496=back
497
498The level of detail in Perl module documentation generally goes from
499less detailed to more detailed. Your SYNOPSIS section should contain a
500minimal example of use (perhaps as little as one line of code; skip the
da75cd15 501unusual use cases or anything not needed by most users); the
f67486be
KR
502DESCRIPTION should describe your module in broad terms, generally in
503just a few paragraphs; more detail of the module's routines or methods,
504lengthy code examples, or other in-depth material should be given in
505subsequent sections.
506
507Ideally, someone who's slightly familiar with your module should be able
508to refresh their memory without hitting "page down". As your reader
509continues through the document, they should receive a progressively
510greater amount of knowledge.
511
512The recommended order of sections in Perl module documentation is:
513
514=over 4
515
516=item *
517
518NAME
519
520=item *
521
522SYNOPSIS
523
524=item *
525
526DESCRIPTION
527
528=item *
529
530One or more sections or subsections giving greater detail of available
531methods and routines and any other relevant information.
532
533=item *
534
535BUGS/CAVEATS/etc
536
537=item *
538
539AUTHOR
540
541=item *
542
543SEE ALSO
544
545=item *
546
547COPYRIGHT and LICENSE
548
549=back
550
551Keep your documentation near the code it documents ("inline"
552documentation). Include POD for a given method right above that
553method's subroutine. This makes it easier to keep the documentation up
554to date, and avoids having to document each piece of code twice (once in
555POD and once in comments).
556
557=head2 README, INSTALL, release notes, changelogs
558
559Your module should also include a README file describing the module and
560giving pointers to further information (website, author email).
561
562An INSTALL file should be included, and should contain simple installation
563instructions (usually "perl Makefile.PL; make; make install").
564
565Release notes or changelogs should be produced for each release of your
566software describing user-visible changes to your module, in terms
567relevant to the user.
568
569=head1 RELEASE CONSIDERATIONS
570
571=head2 Version numbering
572
573Version numbers should indicate at least major and minor releases, and
574possibly sub-minor releases. A major release is one in which most of
575the functionality has changed, or in which major new functionality is
576added. A minor release is one in which a small amount of functionality
577has been added or changed. Sub-minor version numbers are usually used
578for changes which do not affect functionality, such as documentation
579patches.
580
581The most common CPAN version numbering scheme looks like this:
582
583 1.00, 1.10, 1.11, 1.20, 1.30, 1.31, 1.32
584
585A correct CPAN version number is a floating point number with at least
5862 digits after the decimal. You can test whether it conforms to CPAN by
587using
588
589 perl -MExtUtils::MakeMaker -le 'print MM->parse_version(shift)' 'Foo.pm'
590
591If you want to release a 'beta' or 'alpha' version of a module but don't
592want CPAN.pm to list it as most recent use an '_' after the regular
593version number followed by at least 2 digits, eg. 1.20_01
594
595Never release anything (even a one-word documentation patch) without
596incrementing the number. Even a one-word documentation patch should
597result in a change in version at the sub-minor level.
598
599=head2 Pre-requisites
600
601Module authors should carefully consider whether to rely on other
602modules, and which modules to rely on.
603
604Most importantly, choose modules which are as stable as possible. In
605order of preference:
606
607=over 4
608
609=item *
610
611Core Perl modules
612
613=item *
614
615Stable CPAN modules
616
617=item *
618
619Unstable CPAN modules
620
621=item *
622
623Modules not available from CPAN
624
625=back
626
627Specify version requirements for other Perl modules in the
628pre-requisites in your Makefile.PL.
629
630Be sure to specify Perl version requirements both in Makefile.PL and
631with C<require 5.6.1> or similar.
632
633=head2 Testing
634
635All modules should be tested before distribution (using "make disttest",
636and the tests should also be available to people installing the modules
637(using "make test").
638
639The importance of these tests is proportional to the alleged stability of a
640module -- a module which purports to be stable or which hopes to achieve wide
641use should adhere to as strict a testing regime as possible.
642
643Useful modules to help you write tests (with minimum impact on your
644development process or your time) include Test::Simple, Carp::Assert
645and Test::Inline.
646
647=head2 Packaging
648
649Modules should be packaged using the standard MakeMaker tools, allowing
650them to be installed in a consistent manner. Use "make dist" to create
651your package.
652
653Tools exist to help you build your module in a MakeMaker-friendly style.
654These include ExtUtils::ModuleMaker and h2xs. See also L<perlnewmod>.
655
656=head2 Licensing
657
658Make sure that your module has a license, and that the full text of it
659is included in the distribution (unless it's a common one and the terms
660of the license don't require you to include it).
661
662If you don't know what license to use, dual licensing under the GPL
663and Artistic licenses (the same as Perl itself) is a good idea.
664
665=head1 COMMON PITFALLS
666
667=head2 Reinventing the wheel
668
669There are certain application spaces which are already very, very well
670served by CPAN. One example is templating systems, another is date and
671time modules, and there are many more. While it is a rite of passage to
672write your own version of these things, please consider carefully
673whether the Perl world really needs you to publish it.
674
675=head2 Trying to do too much
676
677Your module will be part of a developer's toolkit. It will not, in
678itself, form the B<entire> toolkit. It's tempting to add extra features
679until your code is a monolithic system rather than a set of modular
680building blocks.
681
682=head2 Inappropriate documentation
683
684Don't fall into the trap of writing for the wrong audience. Your
685primary audience is a reasonably experienced developer with at least
686a moderate understanding of your module's application domain, who's just
687downloaded your module and wants to start using it as quickly as possible.
688
689Tutorials, end-user documentation, research papers, FAQs etc are not
690appropriate in a module's main documentation. If you really want to
691write these, include them as sub-documents such as C<My::Module::Tutorial> or
692C<My::Module::FAQ> and provide a link in the SEE ALSO section of the
693main documentation.
694
695=head1 SEE ALSO
696
697=over 4
698
699=item L<perlstyle>
700
701General Perl style guide
702
703=item L<perlnewmod>
704
705How to create a new module
706
707=item L<perlpod>
708
709POD documentation
710
711=item L<podchecker>
712
713Verifies your POD's correctness
714
715=item Testing tools
716
717L<Test::Simple>, L<Test::Inline>, L<Carp::Assert>
718
719=item http://pause.perl.org/
720
721Perl Authors Upload Server. Contains links to information for module
722authors.
723
724=item Any good book on software engineering
725
726=back
727
728=head1 AUTHOR
729
730Kirrily "Skud" Robert <skud@cpan.org>
731