|o||Dont re-invent the wheel|
|o||Patch, extend or subclass an existing module where possible|
|o||Do one thing and do it well|
|o||Choose an appropriate name|
|o||Get feedback before publishing|
o API should be understandable by the average programmer o Simple methods for simple tasks o Separate functionality from output o Consistent naming of subroutines or methods o Use named parameters (a hash or hashref) when there are more than two parameters
o Ensure your module works under use strict and -w o Stable modules should maintain backwards compatibility
o Write documentation in POD o Document purpose, scope and target applications o Document each publically accessible method or subroutine, including params and return values o Give examples of use in your documentation o Provide a README file and perhaps also release notes, changelog, etc o Provide links to further information (URL, email)
o Specify pre-requisites in Makefile.PL or Build.PL o Specify Perl version requirements with use o Include tests with your module o Choose a sensible and consistent version numbering scheme (X.YY is the common Perl module numbering scheme) o Increment the version number for every change, no matter how small o Package the module using make dist o Choose an appropriate license (GPL/Artistic is a good default)
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.
You may not even need to write the module. Check whether its already been done in Perl, and avoid re-inventing the wheel unless you have a good reason.
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, its important that the blocks are the right shape, and that the developer shouldnt 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?
FooBar.pm provides an implementation of the FOO protocol and the related BAR standard.
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.
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:
o Be descriptive (i.e. accurately describes the purpose of the module). o Be consistent with existing modules. o Reflect the functionality of the module, not the implementation. o Avoid starting a new top-level hierarchy, especially if a suitable hierarchy already exists under which you could place your module.
If you have never uploaded a module to CPAN before (and even if you have), you are strongly encouraged to get feedback on PrePAN <http://prepan.org>. 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 modules 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 Perl Monks <http://www.perlmonks.org>.
Considerations for module design and coding:
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.
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.
o The system being designed is large, or is likely to become large. o The data can be aggregated into obvious structures, especially if theres a large amount of data in each aggregate. o The various types of data aggregate form a natural hierarchy that facilitates the use of inheritance and polymorphism. o You have a piece of data on which many different operations are applied. o 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. o Its likely youll have to add new data types later. o The typical interactions between pieces of data are best represented by operators. o The implementation of individual components of the system is likely to change over time. o The system design is already object-oriented. o Large numbers of other programmers will be using your code modules.
Your interfaces should be understandable by an average Perl programmer. The following guidelines may help you judge whether your API is sufficiently straightforward:
Write simple routines to do simple things. Its better to have numerous simple routines than a few monolithic ones. If your routine changes its behaviour significantly based on its arguments, its a sign that you should have two (or more) separate routines. 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 find(\&wanted, $dir) syntax.
Provide sensible shortcuts and defaults. Dont 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, its 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. Naming conventions Your naming should be consistent. For instance, its better to have:
display_day(); display_week(); display_year();
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 arent!)
Parameter passing Use named parameters. Its 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. Dont 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 (-name) or entirely in upper case (NAME) is a relic of older versions of Perl in which ordinary lower case strings were not handled correctly by the => 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!
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.
Modules which are stable should not break backwards compatibility without at least a long transition phase and a major change in version number.
When your module encounters an error it should do one or more of:
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.
o Return an undefined value. o set $Module::errstr or similar (errstr is a common name used by DBI and other popular modules; if you choose something else, be sure to document it clearly). o warn() or carp() a message to STDERR. o croak() only when your module absolutely cannot figure out what to do. (croak() is a better version of die() for use within modules, which reports its errors from the perspective of the caller. See Carp for details of croak(), carp() and other useful routines.) o As an alternative to the above, you may prefer to throw exceptions using the Error module.
Your module should include documentation aimed at Perl developers. You should use Perls 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:
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 modules routines or methods, lengthy code examples, or other in-depth material should be given in subsequent sections.
o A synopsis of the common uses of the module o The purpose, scope and target applications of your module o Use of each publically accessible method or subroutine, including parameters and return values o Examples of use o Sources of further information o A contact email address for the author/maintainer
Ideally, someone whos 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:
Keep your documentation near the code it documents (inline documentation). Include POD for a given method right above that methods 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).
o NAME o SYNOPSIS o DESCRIPTION o One or more sections or subsections giving greater detail of available methods and routines and any other relevant information. o BUGS/CAVEATS/etc o AUTHOR o SEE ALSO o COPYRIGHT and LICENSE
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:
When using Module::Build, this will usually be:
perl Makefile.PL make make test make install
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.
perl Build.PL perl Build perl Build test perl Build install
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 Changes, and to follow the simple format described in CPAN::Changes::Spec.
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.
1.00, 1.10, 1.11, 1.20, 1.30, 1.31, 1.32
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 dont 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:
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.
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.
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:
Specify version requirements for other Perl modules in the pre-requisites in your Makefile.PL or Build.PL.
o Core Perl modules o Stable CPAN modules o Unstable CPAN modules o Modules not available from CPAN
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 make test equivalent perl Build test.
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.
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 perlnewmod.
Make sure that your module has a license, and that the full text of it is included in the distribution (unless its a common one and the terms of the license dont require you to include it).
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.
Your module will be part of a developers toolkit. It will not, in itself, form the <B>entireB> toolkit. Its tempting to add extra features until your code is a monolithic system rather than a set of modular building blocks.
Dont 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 modules application domain, whos 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 modules main documentation. If you really want to write these, include them as sub-documents such as My::Module::Tutorial or My::Module::FAQ and provide a link in the SEE ALSO section of the main documentation.
perlstyle General Perl style guide perlnewmod How to create a new module perlpod POD documentation podchecker Verifies your PODs correctness Packaging Tools ExtUtils::MakeMaker, Module::Build Testing tools Test::Simple, Test::Inline, Carp::Assert, Test::More, Test::MockObject http://pause.perl.org/ Perl Authors Upload Server. Contains links to information for module authors. Any good book on software engineering
Kirrily Skud Robert <email@example.com>
|perl v5.22.1||PERLMODSTYLE (1)||2015-10-17|