/usr/share/perl5/pod
NameSizeModeActions
perl.pod162700644editdlrm
perl56delta.pod1072010644editdlrm
perl58delta.pod1151650644editdlrm
perl561delta.pod1247130644editdlrm
perl581delta.pod380610644editdlrm
perl582delta.pod44700644editdlrm
perl583delta.pod63350644editdlrm
perl584delta.pod73630644editdlrm
perl585delta.pod58890644editdlrm
perl586delta.pod46510644editdlrm
perl587delta.pod83570644editdlrm
perl588delta.pod252720644editdlrm
perl589delta.pod539000644editdlrm
perl5004delta.pod562400644editdlrm
perl5005delta.pod342830644editdlrm
perl5100delta.pod555350644editdlrm
perl5101delta.pod438880644editdlrm
perl5120delta.pod892720644editdlrm
perl5121delta.pod101410644editdlrm
perl5122delta.pod96030644editdlrm
perl5123delta.pod41000644editdlrm
perl5124delta.pod36720644editdlrm
perl5125delta.pod76830644editdlrm
perl5140delta.pod1443240644editdlrm
perl5141delta.pod79660644editdlrm
perl5142delta.pod68920644editdlrm
perl5143delta.pod77600644editdlrm
perl5144delta.pod63270644editdlrm
perl5160delta.pod1336510644editdlrm
perl5161delta.pod61420644editdlrm
perl5162delta.pod35940644editdlrm
perl5163delta.pod40850644editdlrm
perl5180delta.pod1194310644editdlrm
perl5181delta.pod65950644editdlrm
perl5182delta.pod53350644editdlrm
perl5184delta.pod46420644editdlrm
perl5200delta.pod1156990644editdlrm
perl5201delta.pod108990644editdlrm
perl5202delta.pod125090644editdlrm
perl5203delta.pod93920644editdlrm
perl5220delta.pod1309630644editdlrm
perl5221delta.pod107670644editdlrm
perl5222delta.pod126290644editdlrm
perl5223delta.pod84560644editdlrm
perl5224delta.pod44600644editdlrm
perl5240delta.pod649270644editdlrm
perl5241delta.pod82150644editdlrm
perl5242delta.pod41130644editdlrm
perl5243delta.pod114280644editdlrm
perl5244delta.pod45100644editdlrm
perl5260delta.pod1018360644editdlrm
perl5261delta.pod79270644editdlrm
perl5262delta.pod78800644editdlrm
perl5263delta.pod70630644editdlrm
perl5280delta.pod721130644editdlrm
perlaix.pod204370644editdlrm
perlamiga.pod57490644editdlrm
perlandroid.pod78710644editdlrm
perlapi.pod4435350644editdlrm
perlapio.pod192850644editdlrm
perlartistic.pod70100644editdlrm
perlbook.pod83380644editdlrm
perlboot.pod2940644editdlrm
perlbot.pod3040644editdlrm
perlbs2000.pod80580644editdlrm
perlcall.pod567060644editdlrm
perlce.pod146020644editdlrm
perlcheat.pod44810644editdlrm
perlclib.pod96190644editdlrm
perlcn.pod46910644editdlrm
perlcommunity.pod72170644editdlrm
perlcygwin.pod271990644editdlrm
perldata.pod467430644editdlrm
perldbmfilter.pod49810644editdlrm
perldebguts.pod385350644editdlrm
perldebtut.pod221520644editdlrm
perldebug.pod392580644editdlrm
perldelta.pod70630644editdlrm
perldeprecation.pod181690644editdlrm
perldiag.pod2845720644editdlrm
perldos.pod105220644editdlrm
perldsc.pod256140644editdlrm
perldtrace.pod79580644editdlrm
perlebcdic.pod842330644editdlrm
perlembed.pod371960644editdlrm
perlexperiment.pod71950644editdlrm
perlfork.pod133550644editdlrm
perlform.pod166080644editdlrm
perlfreebsd.pod16100644editdlrm
perlfunc.pod3929570644editdlrm
perlgit.pod335090644editdlrm
perlgpl.pod138150644editdlrm
perlguts.pod1393290644editdlrm
perlhack.pod404450644editdlrm
perlhacktips.pod555090644editdlrm
perlhacktut.pod61530644editdlrm
perlhaiku.pod15080644editdlrm
perlhist.pod535460644editdlrm
perlhpux.pod305090644editdlrm
perlhurd.pod19930644editdlrm
perlintern.pod545720644editdlrm
perlinterp.pod336870644editdlrm
perlintro.pod221190644editdlrm
perliol.pod341850644editdlrm
perlipc.pod708290644editdlrm
perlirix.pod43950644editdlrm
perljp.pod75210644editdlrm
perlko.pod122590644editdlrm
perllexwarn.pod3550644editdlrm
perllinux.pod14880644editdlrm
perllocale.pod686780644editdlrm
perllol.pod95800644editdlrm
perlmacos.pod10010644editdlrm
perlmacosx.pod120600644editdlrm
perlmod.pod262500644editdlrm
perlmodinstall.pod127920644editdlrm
perlmodlib.pod764820644editdlrm
perlmodstyle.pod225750644editdlrm
perlmroapi.pod32120644editdlrm
perlnetware.pod66480644editdlrm
perlnewmod.pod110360644editdlrm
perlnumber.pod83530644editdlrm
perlobj.pod355370644editdlrm
perlootut.pod267830644editdlrm
perlop.pod1362520644editdlrm
perlopenbsd.pod12040644editdlrm
perlopentut.pod94550644editdlrm
perlos2.pod933510644editdlrm
perlos390.pod156740644editdlrm
perlos400.pod47680644editdlrm
perlpacktut.pod512820644editdlrm
perlperf.pod498810644editdlrm
perlplan9.pod51250644editdlrm
perlpod.pod221960644editdlrm
perlpodspec.pod684760644editdlrm
perlpolicy.pod256290644editdlrm
perlport.pod876020644editdlrm
perlpragma.pod51760644editdlrm
perlqnx.pod66730644editdlrm
perlre.pod1209010644editdlrm
perlreapi.pod303340644editdlrm
perlrebackslash.pod318170644editdlrm
perlrecharclass.pod490290644editdlrm
perlref.pod353040644editdlrm
perlreftut.pod187900644editdlrm
perlreguts.pod383280644editdlrm
perlrepository.pod5090644editdlrm
perlrequick.pod184970644editdlrm
perlreref.pod147440644editdlrm
perlretut.pod1212570644editdlrm
perlriscos.pod15290644editdlrm
perlrun.pod535500644editdlrm
perlsec.pod261840644editdlrm
perlsolaris.pod298220644editdlrm
perlsource.pod68760644editdlrm
perlstyle.pod86300644editdlrm
perlsub.pod729670644editdlrm
perlsymbian.pod153590644editdlrm
perlsyn.pod445120644editdlrm
perlsynology.pod77780644editdlrm
perlthrtut.pod464590644editdlrm
perltie.pod386070644editdlrm
perltoc.pod6941710644editdlrm
perltodo.pod3760644editdlrm
perltooc.pod2940644editdlrm
perltoot.pod2940644editdlrm
perltrap.pod106200644editdlrm
perltru64.pod84920644editdlrm
perltw.pod44770644editdlrm
perlunicode.pod824910644editdlrm
perlunicook.pod254880644editdlrm
perlunifaq.pod136470644editdlrm
perluniintro.pod383400644editdlrm
perluniprops.pod2853060644editdlrm
perlunitut.pod79510644editdlrm
perlutil.pod76400644editdlrm
perlvar.pod783640644editdlrm
perlvms.pod508230644editdlrm
perlvos.pod38430644editdlrm
perlwin32.pod392980644editdlrm
perlxs.pod789200644editdlrm
perlxstut.pod500950644editdlrm
perlxstypemap.pod240010644editdlrm
Edit: /usr/share/perl5/pod/perlmodstyle.pod (22575B)
=head1 NAME perlmodstyle - Perl module style guide =head1 INTRODUCTION This document attempts to describe the Perl Community's "best practice" for writing Perl modules. It extends the recommendations found in L , which should be considered required reading before reading this document. While this document is intended to be useful to all module authors, it is particularly aimed at authors who wish to publish their modules on CPAN. The focus is on elements of style which are visible to the users of a module, rather than those parts which are only seen by the module's developers. However, many of the guidelines presented in this document can be extrapolated and applied successfully to a module's internals. This document differs from L in that it is a style guide rather than a tutorial on creating CPAN modules. It provides a checklist against which modules can be compared to determine whether they conform to best practice, without necessarily describing in detail how to achieve this. All the advice contained in this document has been gleaned from extensive conversations with experienced CPAN authors and users. Every piece of advice given here is the result of previous mistakes. This information is here to help you avoid the same mistakes and the extra work that would inevitably be required to fix them. The first section of this document provides an itemized checklist; subsequent sections provide a more detailed discussion of the items on the list. The final section, "Common Pitfalls", describes some of the most popular mistakes made by CPAN authors. =head1 QUICK CHECKLIST For more detail on each item in this checklist, see below. =head2 Before you start =over 4 =item * Don't re-invent the wheel =item * Patch, extend or subclass an existing module where possible =item * Do one thing and do it well =item * Choose an appropriate name =item * Get feedback before publishing =back =head2 The API =over 4 =item * API should be understandable by the average programmer =item * Simple methods for simple tasks =item * Separate functionality from output =item * Consistent naming of subroutines or methods =item * Use named parameters (a hash or hashref) when there are more than two parameters =back =head2 Stability =over 4 =item * Ensure your module works under C and C<-w> =item * Stable modules should maintain backwards compatibility =back =head2 Documentation =over 4 =item * Write documentation in POD =item * Document purpose, scope and target applications =item * Document each publically accessible method or subroutine, including params and return values =item * Give examples of use in your documentation =item * Provide a README file and perhaps also release notes, changelog, etc =item * Provide links to further information (URL, email) =back =head2 Release considerations =over 4 =item * Specify pre-requisites in Makefile.PL or Build.PL =item * Specify Perl version requirements with C =item * Include tests with your module =item * Choose a sensible and consistent version numbering scheme (X.YY is the common Perl module numbering scheme) =item * Increment the version number for every change, no matter how small =item * Package the module using "make dist" =item * Choose an appropriate license (GPL/Artistic is a good default) =back =head1 BEFORE YOU START WRITING A MODULE Try not to launch headlong into developing your module without spending some time thinking first. A little forethought may save you a vast amount of effort later on. =head2 Has it been done before? You may not even need to write the module. Check whether it's already been done in Perl, and avoid re-inventing the wheel unless you have a good reason. Good places to look for pre-existing modules include L and L and asking on C (L). If an existing module B does what you want, consider writing a patch, writing a subclass, or otherwise extending the existing module rather than rewriting it. =head2 Do one thing and do it well At the risk of stating the obvious, modules are intended to be modular. A Perl developer should be able to use modules to put together the building blocks of their application. However, it's important that the blocks are the right shape, and that the developer shouldn't have to use a big block when all they need is a small one. Your module should have a clearly defined scope which is no longer than a single sentence. Can your module be broken down into a family of related modules? Bad example: "FooBar.pm provides an implementation of the FOO protocol and the related BAR standard." Good example: "Foo.pm provides an implementation of the FOO protocol. Bar.pm implements the related BAR protocol." This means that if a developer only needs a module for the BAR standard, they should not be forced to install libraries for FOO as well. =head2 What's in a name? Make sure you choose an appropriate name for your module early on. This will help people find and remember your module, and make programming with your module more intuitive. When naming your module, consider the following: =over 4 =item * Be descriptive (i.e. accurately describes the purpose of the module). =item * Be consistent with existing modules. =item * Reflect the functionality of the module, not the implementation. =item * Avoid starting a new top-level hierarchy, especially if a suitable hierarchy already exists under which you could place your module. =back =head2 Get feedback before publishing If you have never uploaded a module to CPAN before (and even if you have), you are strongly encouraged to get feedback on L. PrePAN is a site dedicated to discussing ideas for CPAN modules with other Perl developers and is a great resource for new (and experienced) Perl developers. You should also try to get feedback from 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, as are community sites like L. =head1 DESIGNING AND WRITING YOUR MODULE Considerations for module design and coding: =head2 To OO or not to OO? 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. In I (copyright 2004, Published by O'Reilly Media, Inc.), Damian Conway provides a list of criteria to use when deciding if OO is the right fit for your problem: =over 4 =item * The system being designed is large, or is likely to become large. =item * The data can be aggregated into obvious structures, especially if there's a large amount of data in each aggregate. =item * The various types of data aggregate form a natural hierarchy that facilitates the use of inheritance and polymorphism. =item * You have a piece of data on which many different operations are applied. =item * You need to perform the same general operations on related types of data, but with slight variations depending on the specific type of data the operations are applied to. =item * It's likely you'll have to add new data types later. =item * The typical interactions between pieces of data are best represented by operators. =item * The implementation of individual components of the system is likely to change over time. =item * The system design is already object-oriented. =item * Large numbers of other programmers will be using your code modules. =back 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. =head2 Designing your API Your interfaces should be understandable by an average Perl programmer. The following guidelines may help you judge whether your API is sufficiently straightforward: =over 4 =item Write simple routines to do simple things. 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. =item Separate functionality from output. 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. 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 C syntax. =item Provide sensible shortcuts and defaults. 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. =item Naming conventions Your naming should be consistent. For instance, it's better to have: display_day(); display_week(); display_year(); than display_day(); week_display(); show_year(); This applies equally to method names, parameter names, and anything else which is visible to the user (and most things that aren't!) =item Parameter passing Use named parameters. It's easier to use a hash like this: $obj->do_something( name => "wibble", type => "text", size => 1024, ); ... than to have a long list of unnamed parameters like this: $obj->do_something("wibble", "text", 1024); 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: $obj->do_something(undef, undef, undef, undef, undef, 1024); Provide sensible defaults for parameters which have them. Don't make your users specify parameters which will almost always be the same. The issue of whether to pass the arguments in a hash or a hashref is largely a matter of personal style. The use of hash keys starting with a hyphen (C<-name>) or entirely in upper case (C) is a relic of older versions of Perl in which ordinary lower case strings were not handled correctly by the C<=E> 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! =back =head2 Strictness and warnings 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. =head2 Backwards compatibility Modules which are "stable" should not break backwards compatibility without at least a long transition phase and a major change in version number. =head2 Error handling and messages When your module encounters an error it should do one or more of: =over 4 =item * Return an undefined value. =item * set C<$Module::errstr> or similar (C is a common name used by DBI and other popular modules; if you choose something else, be sure to document it clearly). =item * C or C a message to STDERR. =item * C only when your module absolutely cannot figure out what to do. (C is a better version of C for use within modules, which reports its errors from the perspective of the caller. See L for details of C, C and other useful routines.) =item * As an alternative to the above, you may prefer to throw exceptions using the Error module. =back 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. =head1 DOCUMENTING YOUR MODULE =head2 POD Your module should include documentation aimed at Perl developers. You should use Perl's "plain old documentation" (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: =over 4 =item * A synopsis of the common uses of the module =item * The purpose, scope and target applications of your module =item * Use of each publically accessible method or subroutine, including parameters and return values =item * Examples of use =item * Sources of further information =item * A contact email address for the author/maintainer =back The level of detail in Perl module documentation generally goes from less detailed to more detailed. Your SYNOPSIS section should contain a minimal example of use (perhaps as little as one line of code; skip the unusual use cases or anything not needed by most users); the DESCRIPTION should describe your module in broad terms, generally in just a few paragraphs; more detail of the module's routines or methods, lengthy code examples, or other in-depth material should be given in subsequent sections. Ideally, someone who's slightly familiar with your module should be able to refresh their memory without hitting "page down". As your reader continues through the document, they should receive a progressively greater amount of knowledge. The recommended order of sections in Perl module documentation is: =over 4 =item * NAME =item * SYNOPSIS =item * DESCRIPTION =item * One or more sections or subsections giving greater detail of available methods and routines and any other relevant information. =item * BUGS/CAVEATS/etc =item * AUTHOR =item * SEE ALSO =item * COPYRIGHT and LICENSE =back Keep your documentation near the code it documents ("inline" documentation). Include POD for a given method right above that method's subroutine. This makes it easier to keep the documentation up to date, and avoids having to document each piece of code twice (once in POD and once in comments). =head2 README, INSTALL, release notes, changelogs Your module should also include a README file describing the module and giving pointers to further information (website, author email). An INSTALL file should be included, and should contain simple installation instructions. When using ExtUtils::MakeMaker this will usually be: =over 4 =item perl Makefile.PL =item make =item make test =item make install =back When using Module::Build, this will usually be: =over 4 =item perl Build.PL =item perl Build =item perl Build test =item perl Build install =back Release notes or changelogs should be produced for each release of your software describing user-visible changes to your module, in terms relevant to the user. Unless you have good reasons for using some other format (for example, a format used within your company), the convention is to name your changelog file C, and to follow the simple format described in L. =head1 RELEASE CONSIDERATIONS =head2 Version numbering Version numbers should indicate at least major and minor releases, and possibly sub-minor releases. A major release is one in which most of the functionality has changed, or in which major new functionality is added. A minor release is one in which a small amount of functionality has been added or changed. Sub-minor version numbers are usually used for changes which do not affect functionality, such as documentation patches. The most common CPAN version numbering scheme looks like this: 1.00, 1.10, 1.11, 1.20, 1.30, 1.31, 1.32 A correct CPAN version number is a floating point number with at least 2 digits after the decimal. You can test whether it conforms to CPAN by using perl -MExtUtils::MakeMaker -le 'print MM->parse_version(shift)' \ 'Foo.pm' If you want to release a 'beta' or 'alpha' version of a module but don't want CPAN.pm to list it as most recent use an '_' after the regular version number followed by at least 2 digits, eg. 1.20_01. If you do this, the following idiom is recommended: our $VERSION = "1.12_01"; # so CPAN distribution will have # right filename our $XS_VERSION = $VERSION; # only needed if you have XS code $VERSION = eval $VERSION; # so "use Module 0.002" won't warn on # underscore With that trick MakeMaker will only read the first line and thus read the underscore, while the perl interpreter will evaluate the $VERSION and convert the string into a number. Later operations that treat $VERSION as a number will then be able to do so without provoking a warning about $VERSION not being a number. Never release anything (even a one-word documentation patch) without incrementing the number. Even a one-word documentation patch should result in a change in version at the sub-minor level. Once picked, it is important to stick to your version scheme, without reducing the number of digits. This is because "downstream" packagers, such as the FreeBSD ports system, interpret the version numbers in various ways. If you change the number of digits in your version scheme, you can confuse these systems so they get the versions of your module out of order, which is obviously bad. =head2 Pre-requisites Module authors should carefully consider whether to rely on other modules, and which modules to rely on. Most importantly, choose modules which are as stable as possible. In order of preference: =over 4 =item * Core Perl modules =item * Stable CPAN modules =item * Unstable CPAN modules =item * Modules not available from CPAN =back Specify version requirements for other Perl modules in the pre-requisites in your Makefile.PL or Build.PL. Be sure to specify Perl version requirements both in Makefile.PL or Build.PL and with C or similar. See the section on C of L for details. =head2 Testing All modules should be tested before distribution (using "make disttest"), and the tests should also be available to people installing the modules (using "make test"). For Module::Build you would use the C equivalent C. The importance of these tests is proportional to the alleged stability of a module. A module which purports to be stable or which hopes to achieve wide use should adhere to as strict a testing regime as possible. Useful modules to help you write tests (with minimum impact on your development process or your time) include Test::Simple, Carp::Assert and Test::Inline. For more sophisticated test suites there are Test::More and Test::MockObject. =head2 Packaging Modules should be packaged using one of the standard packaging tools. Currently you have the choice between ExtUtils::MakeMaker and the more platform independent Module::Build, allowing modules to be installed in a consistent manner. When using ExtUtils::MakeMaker, you can use "make dist" to create your package. Tools exist to help you to build your module in a MakeMaker-friendly style. These include ExtUtils::ModuleMaker and h2xs. See also L. =head2 Licensing Make sure that your module has a license, and that the full text of it is included in the distribution (unless it's a common one and the terms of the license don't require you to include it). If you don't know what license to use, dual licensing under the GPL and Artistic licenses (the same as Perl itself) is a good idea. See L and L. =head1 COMMON PITFALLS =head2 Reinventing the wheel There are certain application spaces which are already very, very well served by CPAN. One example is templating systems, another is date and time modules, and there are many more. While it is a rite of passage to write your own version of these things, please consider carefully whether the Perl world really needs you to publish it. =head2 Trying to do too much Your module will be part of a developer's toolkit. It will not, in itself, form the B toolkit. It's tempting to add extra features until your code is a monolithic system rather than a set of modular building blocks. =head2 Inappropriate documentation Don't fall into the trap of writing for the wrong audience. Your primary audience is a reasonably experienced developer with at least a moderate understanding of your module's application domain, who's just downloaded your module and wants to start using it as quickly as possible. Tutorials, end-user documentation, research papers, FAQs etc are not appropriate in a module's main documentation. If you really want to write these, include them as sub-documents such as C or C and provide a link in the SEE ALSO section of the main documentation. =head1 SEE ALSO =over 4 =item L General Perl style guide =item L How to create a new module =item L POD documentation =item L Verifies your POD's correctness =item Packaging Tools L, L =item Testing tools L, L, L, L, L =item L Perl Authors Upload Server. Contains links to information for module authors. =item Any good book on software engineering =back =head1 AUTHOR Kirrily "Skud" Robert