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