Dig into a bunch of modules to see how theyre written. Id suggest
starting with Text::Tabs, since its in the standard
library and is nice and simple, and then looking at something a little
more complex like File::Copy. For object oriented
code, WWW::Mechanize or the Email::* modules provide some good
These should give you an overall feel for how modules are laid out and written.
|Check its new||There are a lot of modules on CPAN, and its easy to miss one thats similar to what youre planning on contributing. Have a good plough through the <http://search.cpan.org> and make sure youre not the one reinventing the wheel!|
|Discuss the need||You might love it. You might feel that everyone else needs it. But there might not actually be any real demand for it out there. If youre unsure about the demand your module will have, consider sending out feelers on the comp.lang.perl.modules newsgroup, or as a last resort, ask the modules list at firstname.lastname@example.org. Remember that this is a closed list with a very long turn-around time - be prepared to wait a good while for a response from them.|
|Choose a name||Perl modules included on CPAN have a naming hierarchy you should try to fit in with. See perlmodlib for more details on how this works, and browse around CPAN and the modules list to get a feel of it. At the very least, remember this: modules should be title capitalised, (This::Thing) fit in with a category, and explain their purpose succinctly.|
|Check again||While youre doing that, make really sure you havent missed a module similar to the one youre about to write.|
Start with module-starter or h2xs The module-starter utility is distributed as part of the Module::Starter CPAN package. It creates a directory with stubs of all the necessary files to start a new module, according to recent best practice for module development, and is invoked from the command line, thus:
A typical invocation of h2xs for a pure Perl module is:
h2xs -AX --skip-exporter --use-new-tests -n Foo::Bar
The -A omits the Autoloader code, -X omits XS elements, --skip-exporter omits the Exporter code, --use-new-tests sets up a modern testing environment, and -n specifies the name of the module.
Use strict and warnings A modules code has to be warning and strict-clean, since you cant guarantee the conditions that itll be used under. Besides, you wouldnt want to distribute code that wasnt warning or strict-clean anyway, right? Use Carp The Carp module allows you to present your error messages from the callers perspective; this gives you a way to signal a problem with the caller and not your module. For instance, if you say this:
warn "No hostname given";
the user will see something like this:
No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm line 123.
which looks like your module is doing something wrong. Instead, you want to put the blame on the user, and say this:
No hostname given at bad_code, line 10.
You do this by using Carp and replacing your warns with carps. If you need to die, say croak instead. However, keep warn and die in place for your sanity checks - where it really is your module at fault.
Use Exporter - wisely! Exporter gives you a standard way of exporting symbols and subroutines from your module into the callers namespace. For instance, saying use Net::Acme qw(&frob) would import the frob subroutine.
The package variable @EXPORT will determine which symbols will get exported when the caller simply says use Net::Acme - you will hardly ever want to put anything in there. @EXPORT_OK, on the other hand, specifies which symbols youre willing to export. If you do want to export a bunch of symbols, use the %EXPORT_TAGS and define a standard export set - look at Exporter for more details.
Use plain old documentation The work isnt over until the paperwork is done, and youre going to need to put in some time writing some documentation for your module. module-starter or h2xs will provide a stub for you to fill in; if youre not sure about the format, look at perlpod for an introduction. Provide a good synopsis of how your module is used in code, a description, and then notes on the syntax and function of the individual subroutines or methods. Use Perl comments for developer notes and POD for end-user notes. Write tests Youre encouraged to create self-tests for your module to ensure its working as intended on the myriad platforms Perl supports; if you upload your module to CPAN, a host of testers will build your module and send you the results of the tests. Again, module-starter and h2xs provide a test framework which you can extend - you should do something more than just checking your module will compile. Test::Simple and Test::More are good places to start when writing a test suite. Write the README If youre uploading to CPAN, the automated gremlins will extract the README file and place that in your CPAN directory. Itll also appear in the main by-module and by-category directories if you make it onto the modules list. Its a good idea to put here what the module actually does in detail, and the user-visible changes since the last release.
Get a CPAN user ID Every developer publishing modules on CPAN needs a CPAN ID. Visit http://pause.perl.org/, select Request PAUSE Account, and wait for your request to be approved by the PAUSE administrators. perl Makefile.PL; make test; make dist Once again, module-starter or h2xs has done all the work for you. They produce the standard Makefile.PL you see when you download and install modules, and this produces a Makefile with a dist target.
Once youve ensured that your module passes its own tests - always a good thing to make sure - you can make dist, and the Makefile will hopefully produce you a nice tarball of your module, ready for upload.
Upload the tarball The email you got when you received your CPAN ID will tell you how to log in to PAUSE, the Perl Authors Upload SErver. From the menus there, you can upload your module to CPAN. Announce to the modules list Once uploaded, itll sit unnoticed in your author directory. If you want it connected to the rest of the CPAN, youll need to go to Register Namespace on PAUSE. Once registered, your module will appear in the by-module and by-category listings on CPAN. Announce to clpa If you have a burning desire to tell the world about your release, post an announcement to the moderated comp.lang.perl.announce newsgroup. Fix bugs! Once you start accumulating users, theyll send you bug reports. If youre lucky, theyll even send you patches. Welcome to the joys of maintaining a software project...
Simon Cozens, email@example.com
Updated by Kirrily Skud Robert, firstname.lastname@example.org
perlmod, perlmodlib, perlmodinstall, h2xs, strict, Carp, Exporter, perlpod, Test::Simple, Test::More ExtUtils::MakeMaker, Module::Build, Module::Starter http://www.cpan.org/ , Ken Williamss tutorial on building your own module at http://mathforum.org/~ken/perl_modules.html
|perl v5.22.1||PERLNEWMOD (1)||2015-10-17|