Welcome to Linux Knowledge Base and Tutorial
"The place where you learn linux"
IndexSoft

 Create an AccountHome | Submit News | Your Account  

Tutorial Menu
Linux Tutorial Home
Table of Contents

· Introduction to Operating Systems
· Linux Basics
· Working with the System
· Shells and Utilities
· Editing Files
· Basic Administration
· The Operating System
· The X Windowing System
· The Computer Itself
· Networking
· System Monitoring
· Solving Problems
· Security
· Installing and Upgrading
· Linux and Windows

Glossary
MoreInfo
Man Pages
Linux Topics
Test Your Knowledge

Site Menu
Site Map
FAQ
Copyright Info
Terms of Use
Privacy Info
Disclaimer
WorkBoard
Thanks
Donations
Advertising
Masthead / Impressum
Your Account

Communication
Feedback
Forums
Private Messages
Surveys

Features
HOWTOs
News Archive
Submit News
Topics
User Articles
Web Links

Google
Google


The Web
linux-tutorial.info

Who's Online
There are currently, 226 guest(s) and 0 member(s) that are online.

You are an Anonymous user. You can register for free by clicking here

  

perlmodstyle



INTRODUCTION

       This document attempts to describe the Perl Community's
       "best practice" for writing Perl modules.  It extends the
       recommendations found in perlstyle , which should be con­
       sidered 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 perlnewmod in that it is a
       style guide rather than a tutorial on creating CPAN mod­
       ules.  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 mis­
       takes made by CPAN authors.


QUICK CHECKLIST

       For more detail on each item in this checklist, see below.

       Before you start

       ·   Don't re-invent the wheel

       ·   Patch, extend or subclass an existing module where
           possible

       ·   Do one thing and do it well

       ·   Choose an appropriate name

       The API

       ·   Ensure your module works under "use strict" and "-w"

       ·   Stable modules should maintain backwards compatibility

       Documentation

       ·   Write documentation in POD

       ·   Document purpose, scope and target applications

       ·   Document each publically accessible method or subrou­
           tine, including params and return values

       ·   Give examples of use in your documentation

       ·   Provide a README file and perhaps also release notes,
           changelog, etc

       ·   Provide links to further information (URL, email)

       Release considerations

       ·   Specify pre-requisites in Makefile.PL or Build.PL

       ·   Specify Perl version requirements with "use"

       ·   Include tests with your module

       ·   Choose a sensible and consistent version numbering
           scheme (X.YY is the common Perl module numbering
           scheme)

       ·   Increment the version number for every change, no mat­
           ter how small

       ·   Package the module using "make dist"

       ·   Choose an appropriate license (GPL/Artistic is a good
           default)


BEFORE YOU START WRITING A MODULE

       Try not to launch headlong into developing your module
       without spending some time thinking first.  A little fore­
       thought may save you a vast amount of effort later on.

       Has it been done before?

       You may not even need to write the module.  Check whether
       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 bro­
       ken 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.

       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 intu­
       itive.

       When naming your module, consider the following:

       ·   Be descriptive (i.e. accurately describes the purpose
           of the module).

       ·   Be consistent with existing modules.

       ·   Reflect the functionality of the module, not the
           implementation.

       ·   Avoid starting a new top-level hierarchy, especially
           if a suitable hierarchy already exists under which you
           could place your module.

       You should contact modules@perl.org to ask them about your
       module name before publishing your module.  You should
       also try to ask 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.


DESIGNING AND WRITING YOUR MODULE

           will become objects

       ·   When the types of data form a natural hierarchy that
           can make use of inheritance

       ·   When operations on data vary according to data type
           (making polymorphic invocation of methods feasible)

       ·   When it is likely that new data types may be later
           introduced into the system, and will need to be han­
           dled by existing code

       ·   When interactions between data are best represented by
           overloaded operators

       ·   When the implementation of system components is likely
           to change over time (and hence should be encapsulated)

       ·   When the system design is itself object-oriented

       ·   When large amounts of client code will use the soft­
           ware (and should be insulated from changes in its
           implementation)

       ·   When many separate operations will need to be applied
           to the same set of data

       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.

       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:

       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 rou­
           tines.

       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.
           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 argu­
           ments.

       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!)

       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 proba­
           bly 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, undef, 1024);

           Provide sensible defaults for parameters which have

       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 appro­
       priate, though this can cause difficulties in many cases.

       Backwards compatibility

       Modules which are "stable" should not break backwards com­
       patibility without at least a long transition phase and a
       major change in version number.

       Error handling and messages

       When your module encounters an error it should do one or
       more of:

       ·   Return an undefined value.

       ·   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).

       ·   "warn()" or "carp()" a message to STDERR.

       ·   "croak()" only when your module absolutely cannot fig­
           ure 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.)

       ·   As an alternative to the above, you may prefer to
           throw exceptions using the Error module.

       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 sepa­
       rate file, a way to specify an error-handling routine, or
       other such features.  Be sure to default all these options
       to the commonest use.


DOCUMENTING YOUR MODULE

       POD

       Your module should include documentation aimed at Perl
       developers.  You should use Perl's "plain old documenta­
       tion" (POD) for your general technical documentation,
       though you may wish to write additional documentation

       ·   A contact email address for the author/maintainer

       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 rou­
       tines 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 docu­
       ment, they should receive a progressively greater amount
       of knowledge.

       The recommended order of sections in Perl module documen­
       tation is:

       ·   NAME

       ·   SYNOPSIS

       ·   DESCRIPTION

       ·   One or more sections or subsections giving greater
           detail of available methods and routines and any other
           relevant information.

       ·   BUGS/CAVEATS/etc

       ·   AUTHOR

       ·   SEE ALSO

       ·   COPYRIGHT and LICENSE

       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 eas­
       ier to keep the documentation up to date, and avoids hav­
       ing to document each piece of code twice (once in POD and
       once in comments).

       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).
       perl Build test
       perl Build 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.


RELEASE CONSIDERATIONS

       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 function­
       ality has been added or changed.  Sub-minor version num­
       bers 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:

         $VERSION = "1.12_01";
         $XS_VERSION = $VERSION; # only needed if you have XS code
         $VERSION = eval $VERSION;

       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.

       ·   Modules not available from CPAN

       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 Make­
       file.PL or Build.PL and with "require 5.6.1" or similar.
       See the section on "use VERSION" of "require" in perlfunc
       for details.

       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 "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.

       Packaging

       Modules should be packaged using one of the standard pack­
       aging tools.  Currently you have the choice between ExtU­
       tils::MakeMaker and the more platform independent Mod­
       ule::Build, allowing modules to be installed in a consis­
       tent 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.

       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 perlgpl and perlartistic.

       ing to add extra features until your code is a monolithic
       system rather than a set of modular building blocks.

       Inappropriate documentation

       Don't fall into the trap of writing for the wrong audi­
       ence.  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 "My::Module::Tutorial" or "My::Mod­
       ule::FAQ" and provide a link in the SEE ALSO section of
       the main documentation.


SEE ALSO

       perlstyle
           General Perl style guide

       perlnewmod
           How to create a new module

       perlpod
           POD documentation

       podchecker
           Verifies your POD's 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 infor­
           mation for module authors.

       Any good book on software engineering


AUTHOR

       Kirrily "Skud" Robert <skud@cpan.org>

perl v5.8.1                 2003-09-02            PERLMODSTYLE(1)
  

The Linux Tutorial is always looking for new contributors.


Login
Nickname

Password

Security Code
Security Code
Type Security Code


Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.

Help if you can!


Amazon Wish List

Did You Know?
You can choose larger fonts by selecting a different themes.


Friends



Tell a Friend About Us

Bookmark and Share



Web site powered by PHP-Nuke

Is this information useful? At the very least you can help by spreading the word to your favorite newsgroups, mailing lists and forums.
All logos and trademarks in this site are property of their respective owner. The comments are property of their posters. Articles are the property of their respective owners. Unless otherwise stated in the body of the article, article content (C) 1994-2013 by James Mohr. All rights reserved. The stylized page/paper, as well as the terms "The Linux Tutorial", "The Linux Server Tutorial", "The Linux Knowledge Base and Tutorial" and "The place where you learn Linux" are service marks of James Mohr. All rights reserved.
The Linux Knowledge Base and Tutorial may contain links to sites on the Internet, which are owned and operated by third parties. The Linux Tutorial is not responsible for the content of any such third-party site. By viewing/utilizing this web site, you have agreed to our disclaimer, terms of use and privacy policy. Use of automated download software ("harvesters") such as wget, httrack, etc. causes the site to quickly exceed its bandwidth limitation and are therefore expressly prohibited. For more details on this, take a look here

PHP-Nuke Copyright © 2004 by Francisco Burzi. This is free software, and you may redistribute it under the GPL. PHP-Nuke comes with absolutely no warranty, for details, see the license.
Page Generation: 0.08 Seconds