📄 perlmodstyle.1
字号:
.IX Item "Provide sensible shortcuts and defaults."Don't require every module user to jump through the same hoops to achieve asimple result. You can always include optional parameters or routines for more complex or non-standard behaviour. If most of your users have totype a few almost identical lines of code when they start using yourmodule, 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..IP "Naming conventions" 4.IX Item "Naming conventions"Your naming should be consistent. For instance, it's better to have:.Sp.Vb 3\& display_day();\& display_week();\& display_year();.Ve.Spthan.Sp.Vb 3\& display_day();\& week_display();\& show_year();.Ve.SpThis applies equally to method names, parameter names, and anything elsewhich is visible to the user (and most things that aren't!).IP "Parameter passing" 4.IX Item "Parameter passing"Use named parameters. It's easier to use a hash like this:.Sp.Vb 5\& $obj\->do_something(\& name => "wibble",\& type => "text",\& size => 1024,\& );.Ve.Sp\&... than to have a long list of unnamed parameters like this:.Sp.Vb 1\& $obj\->do_something("wibble", "text", 1024);.Ve.SpWhile the list of arguments might work fine for one, two or even threearguments, any more arguments become hard for the module user toremember, and hard for the module author to manage. If you want to adda new parameter you will have to add it to the end of the list forbackward compatibility, and this will probably make your list orderunintuitive. Also, if many elements may be undefined you may see thefollowing unattractive method calls:.Sp.Vb 1\& $obj\->do_something(undef, undef, undef, undef, undef, undef, 1024);.Ve.SpProvide sensible defaults for parameters which have them. Don't makeyour users specify parameters which will almost always be the same..SpThe issue of whether to pass the arguments in a hash or a hashref islargely a matter of personal style..SpThe use of hash keys starting with a hyphen (\f(CW\*(C`\-name\*(C'\fR) or entirely in upper case (\f(CW\*(C`NAME\*(C'\fR) is a relic of older versions of Perl in whichordinary lower case strings were not handled correctly by the \f(CW\*(C`=>\*(C'\fRoperator. While some modules retain uppercase or hyphenated argumentkeys for historical reasons or as a matter of personal style, most newmodules should use simple lower case keys. Whatever you choose, beconsistent!.Sh "Strictness and warnings".IX Subsection "Strictness and warnings"Your module should run successfully under the strict pragma and shouldrun without generating any warnings. Your module should also handle taint-checking where appropriate, though this can cause difficulties inmany cases..Sh "Backwards compatibility".IX Subsection "Backwards compatibility"Modules which are \*(L"stable\*(R" should not break backwards compatibilitywithout at least a long transition phase and a major change in versionnumber..Sh "Error handling and messages".IX Subsection "Error handling and messages"When your module encounters an error it should do one or more of:.IP "\(bu" 4Return an undefined value..IP "\(bu" 4set \f(CW$Module::errstr\fR or similar (\f(CW\*(C`errstr\*(C'\fR is a common name used by\&\s-1DBI\s0 and other popular modules; if you choose something else, be sure todocument it clearly)..IP "\(bu" 4\&\f(CW\*(C`warn()\*(C'\fR or \f(CW\*(C`carp()\*(C'\fR a message to \s-1STDERR\s0..IP "\(bu" 4\&\f(CW\*(C`croak()\*(C'\fR only when your module absolutely cannot figure out what todo. (\f(CW\*(C`croak()\*(C'\fR is a better version of \f(CW\*(C`die()\*(C'\fR for use within modules, which reports its errors from the perspective of the caller. See Carp for details of \f(CW\*(C`croak()\*(C'\fR, \f(CW\*(C`carp()\*(C'\fR and other usefulroutines.).IP "\(bu" 4As an alternative to the above, you may prefer to throw exceptions using the Error module..PPConfigurable error handling can be very useful to your users. Consideroffering a choice of levels for warning and debug messages, an option tosend messages to a separate file, a way to specify an error-handlingroutine, or other such features. Be sure to default all these optionsto the commonest use..SH "DOCUMENTING YOUR MODULE".IX Header "DOCUMENTING YOUR MODULE".Sh "\s-1POD\s0".IX Subsection "POD"Your module should include documentation aimed at Perl developers.You should use Perl's \*(L"plain old documentation\*(R" (\s-1POD\s0) for your general technical documentation, though you may wish to write additionaldocumentation (white papers, tutorials, etc) in some other format. You need to cover the following subjects:.IP "\(bu" 4A synopsis of the common uses of the module.IP "\(bu" 4The purpose, scope and target applications of your module.IP "\(bu" 4Use of each publically accessible method or subroutine, includingparameters and return values.IP "\(bu" 4Examples of use.IP "\(bu" 4Sources of further information.IP "\(bu" 4A contact email address for the author/maintainer.PPThe level of detail in Perl module documentation generally goes fromless detailed to more detailed. Your \s-1SYNOPSIS\s0 section should contain aminimal example of use (perhaps as little as one line of code; skip theunusual use cases or anything not needed by most users); the\&\s-1DESCRIPTION\s0 should describe your module in broad terms, generally injust 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..PPIdeally, someone who's slightly familiar with your module should be ableto refresh their memory without hitting \*(L"page down\*(R". As your readercontinues through the document, they should receive a progressivelygreater amount of knowledge..PPThe recommended order of sections in Perl module documentation is:.IP "\(bu" 4\&\s-1NAME\s0.IP "\(bu" 4\&\s-1SYNOPSIS\s0.IP "\(bu" 4\&\s-1DESCRIPTION\s0.IP "\(bu" 4One or more sections or subsections giving greater detail of available methods and routines and any other relevant information..IP "\(bu" 4BUGS/CAVEATS/etc.IP "\(bu" 4\&\s-1AUTHOR\s0.IP "\(bu" 4\&\s-1SEE\s0 \s-1ALSO\s0.IP "\(bu" 4\&\s-1COPYRIGHT\s0 and \s-1LICENSE\s0.PPKeep your documentation near the code it documents (\*(L"inline\*(R"documentation). Include \s-1POD\s0 for a given method right above that method's subroutine. This makes it easier to keep the documentation upto date, and avoids having to document each piece of code twice (once in\&\s-1POD\s0 and once in comments)..Sh "\s-1README\s0, \s-1INSTALL\s0, release notes, changelogs".IX Subsection "README, INSTALL, release notes, changelogs"Your module should also include a \s-1README\s0 file describing the module andgiving pointers to further information (website, author email)..PPAn \s-1INSTALL\s0 file should be included, and should contain simple installation instructions. When using ExtUtils::MakeMaker this will usually be:.IP "perl Makefile.PL" 4.IX Item "perl Makefile.PL".PD 0.IP "make" 4.IX Item "make".IP "make test" 4.IX Item "make test".IP "make install" 4.IX Item "make install".PD.PPWhen using Module::Build, this will usually be:.IP "perl Build.PL" 4.IX Item "perl Build.PL".PD 0.IP "perl Build" 4.IX Item "perl Build".IP "perl Build test" 4.IX Item "perl Build test".IP "perl Build install" 4.IX Item "perl Build install".PD.PPRelease notes or changelogs should be produced for each release of yoursoftware describing user-visible changes to your module, in termsrelevant to the user..SH "RELEASE CONSIDERATIONS".IX Header "RELEASE CONSIDERATIONS".Sh "Version numbering".IX Subsection "Version numbering"Version numbers should indicate at least major and minor releases, andpossibly sub-minor releases. A major release is one in which most ofthe functionality has changed, or in which major new functionality isadded. A minor release is one in which a small amount of functionalityhas been added or changed. Sub-minor version numbers are usually usedfor changes which do not affect functionality, such as documentationpatches..PPThe most common \s-1CPAN\s0 version numbering scheme looks like this:.PP.Vb 1\& 1.00, 1.10, 1.11, 1.20, 1.30, 1.31, 1.32.Ve.PPA correct \s-1CPAN\s0 version number is a floating point number with at least 2 digits after the decimal. You can test whether it conforms to \s-1CPAN\s0 by using.PP.Vb 1\& perl \-MExtUtils::MakeMaker \-le \*(Aqprint MM\->parse_version(shift)\*(Aq \*(AqFoo.pm\*(Aq.Ve.PPIf you want to release a 'beta' or 'alpha' version of a module butdon't want \s-1CPAN\s0.pm to list it as most recent use an '_' after theregular version number followed by at least 2 digits, eg. 1.20_01. Ifyou do this, the following idiom is recommended:.PP.Vb 3\& $VERSION = "1.12_01";\& $XS_VERSION = $VERSION; # only needed if you have XS code\& $VERSION = eval $VERSION;.Ve.PPWith that trick MakeMaker will only read the first line and thus readthe underscore, while the perl interpreter will evaluate the \f(CW$VERSION\fRand convert the string into a number. Later operations that treat\&\f(CW$VERSION\fR as a number will then be able to do so without provoking awarning about \f(CW$VERSION\fR not being a number..PPNever release anything (even a one-word documentation patch) withoutincrementing the number. Even a one-word documentation patch shouldresult in a change in version at the sub-minor level..Sh "Pre-requisites".IX Subsection "Pre-requisites"Module authors should carefully consider whether to rely on othermodules, and which modules to rely on..PPMost importantly, choose modules which are as stable as possible. Inorder of preference:.IP "\(bu" 4Core Perl modules.IP "\(bu" 4Stable \s-1CPAN\s0 modules.IP "\(bu" 4Unstable \s-1CPAN\s0 modules.IP "\(bu" 4Modules not available from \s-1CPAN\s0.PPSpecify version requirements for other Perl modules in thepre-requisites in your Makefile.PL or Build.PL..PPBe sure to specify Perl version requirements both in Makefile.PL orBuild.PL and with \f(CW\*(C`require 5.6.1\*(C'\fR or similar. See the section on\&\f(CW\*(C`use VERSION\*(C'\fR of \*(L"require\*(R" in perlfunc for details..Sh "Testing".IX Subsection "Testing"All modules should be tested before distribution (using \*(L"make disttest\*(R"),and the tests should also be available to people installing the modules (using \*(L"make test\*(R"). For Module::Build you would use the \f(CW\*(C`make test\*(C'\fR equivalent \f(CW\*(C`perl Build test\*(C'\fR..PPThe 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..PPUseful 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..Sh "Packaging".IX Subsection "Packaging"Modules should be packaged using one of the standard packaging tools.Currently you have the choice between ExtUtils::MakeMaker and themore platform independent Module::Build, allowing modules to be installed in aconsistent manner.When using ExtUtils::MakeMaker, you can use \*(L"make dist\*(R" to create yourpackage. Tools exist to help you to build your module in a MakeMaker-friendlystyle. These include ExtUtils::ModuleMaker and h2xs. See also perlnewmod..Sh "Licensing".IX Subsection "Licensing"Make sure that your module has a license, and that the full text of itis included in the distribution (unless it's a common one and the termsof the license don't require you to include it)..PPIf you don't know what license to use, dual licensing under the \s-1GPL\s0and Artistic licenses (the same as Perl itself) is a good idea.See perlgpl and perlartistic..SH "COMMON PITFALLS".IX Header "COMMON PITFALLS".Sh "Reinventing the wheel".IX Subsection "Reinventing the wheel"There are certain application spaces which are already very, very wellserved by \s-1CPAN\s0. One example is templating systems, another is date andtime modules, and there are many more. While it is a rite of passage towrite your own version of these things, please consider carefullywhether the Perl world really needs you to publish it..Sh "Trying to do too much".IX Subsection "Trying to do too much"Your module will be part of a developer's toolkit. It will not, initself, form the \fBentire\fR toolkit. It's tempting to add extra featuresuntil your code is a monolithic system rather than a set of modularbuilding blocks..Sh "Inappropriate documentation".IX Subsection "Inappropriate documentation"Don't fall into the trap of writing for the wrong audience. Yourprimary 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..PPTutorials, 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 \f(CW\*(C`My::Module::Tutorial\*(C'\fR or\&\f(CW\*(C`My::Module::FAQ\*(C'\fR and provide a link in the \s-1SEE\s0 \s-1ALSO\s0 section of themain documentation..SH "SEE ALSO".IX Header "SEE ALSO".IP "perlstyle" 4.IX Item "perlstyle"General Perl style guide.IP "perlnewmod" 4.IX Item "perlnewmod"How to create a new module.IP "perlpod" 4.IX Item "perlpod"\&\s-1POD\s0 documentation.IP "podchecker" 4.IX Item "podchecker"Verifies your \s-1POD\s0's correctness.IP "Packaging Tools" 4.IX Item "Packaging Tools"ExtUtils::MakeMaker, Module::Build.IP "Testing tools" 4.IX Item "Testing tools"Test::Simple, Test::Inline, Carp::Assert, Test::More, Test::MockObject.IP "http://pause.perl.org/" 4.IX Item "http://pause.perl.org/"Perl Authors Upload Server. Contains links to information for moduleauthors..IP "Any good book on software engineering" 4.IX Item "Any good book on software engineering".SH "AUTHOR".IX Header "AUTHOR"Kirrily \*(L"Skud\*(R" Robert <skud@cpan.org>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -