perlnewmod - preparing a new module for distribution
This document gives you some suggestions about how to go about writing Perl modules, preparing them for distribution, and making them available via CPAN.
One of the things that makes Perl really powerful is the fact that Perl hackers tend to want to share the solutions to problems they’ve faced, so you and I don’t have to battle with the same problem again.
The main way they do this is by abstracting the solution into a Perl module. If you don’t know what one of these is, the rest of this document isn’t going to be much use to you. You’re also missing out on an awful lot of useful code; consider having a look at perlmod, perlmodlib and perlmodinstall before coming back here.
When you’ve found that there isn’t a module available for what you’re trying to do, and you’ve had to write the code yourself, consider packaging up the solution into a module and uploading it to CPAN so that others can benefit.
You should also take a look at perlmodstyle for best practices in making a module.
We’re going to primarily concentrate on Perl-only modules here, rather than XS modules. XS modules serve a rather different purpose, and you should consider different things before distributing them - the popularity of the library you are gluing, the portability to other operating systems, and so on. However, the notes on preparing the Perl side of the module and packaging and distributing it will apply equally well to an XS module as a pure-Perl one.
What should I make into a module?
You should make a module out of any code that you think is going to be useful to others. Anything that’s likely to fill a hole in the communal library and which someone else can slot directly into their program. Any part of your code which you can isolate and extract and plug into something else is a likely candidate.
Let’s take an example. Suppose you’re reading in data from a local format into a hash-of-hashes in Perl, turning that into a tree, walking the tree and then piping each node to an Acme Transmogrifier Server.
Now, quite a few people have the Acme Transmogrifier, and you’ve had to write something to talk the protocol from scratch - you’d almost certainly want to make that into a module. The level at which you pitch it is up to you: you might want protocol-level modules analogous to Net::SMTP which then talk to higher level modules analogous to Mail::Send. The choice is yours, but you do want to get a module out for that server protocol.
Nobody else on the planet is going to talk your local data format, so we can ignore that. But what about the thing in the middle? Building tree structures from Perl variables and then traversing them is a nice, general problem, and if nobody’s already written a module that does that, you might want to modularise that code too.
So hopefully you’ve now got a few ideas about what’s good to modularise. Let’s now see how it’s done.
Step-by-step: Preparing the ground
Before we even start scraping out the code, there are a few things we’ll want to do in advance.
|Look around||Dig into a bunch of modules to see how they’re written. I’d suggest starting with Text::Tabs, since it’s 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
These should give you an overall feel for how modules are laid out and written.
|Check it’s new||There are a lot of modules on CPAN, and it’s easy to miss one that’s similar to what you’re planning on contributing. Have a good plough through <http://metacpan.org> and make sure you’re 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 you’re unsure about the demand your module will have, consider asking the
|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 you’re doing that, make really sure you haven’t missed a module similar to the one you’re about to write.
When you’ve got your name sorted out and you’re sure that your module is wanted and not currently available, it’s time to start coding.
Step-by-step: Making the module
|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:
If you do not wish to install the Module::Starter package from CPAN, h2xs is an older tool, originally intended for the development of XS modules, which comes packaged with the Perl distribution.
A typical invocation of h2xs for a pure Perl module is:
|Use strict and warnings||A module’s code has to be warning and strict-clean, since you can’t guarantee the conditions that it’ll be used under. Besides, you wouldn’t want to distribute code that wasn’t warning or strict-clean anyway, right?|
|Use Carp||The Carp module allows you to present your error messages from the caller’s perspective; this gives you a way to signal a problem with the caller and not your module. For instance, if you say this:
the user will see something like this:
which looks like your module is doing something wrong. Instead, you want to put the blame on the user, and say this:
You do this by using Carp and replacing your
|Use Exporter - wisely!||Exporter gives you a standard way of exporting symbols and subroutines from your module into the caller’s namespace. For instance, saying
The package variable
|Use plain old documentation||The work isn’t over until the paperwork is done, and you’re going to need to put in some time writing some documentation for your module.
|Write tests||You’re encouraged to create self-tests for your module to ensure it’s 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,
|Write the README||If you’re uploading to CPAN, the automated gremlins will extract the README file and place that in your CPAN directory. It’ll also appear in the main by-module and by-category directories if you make it onto the modules list. It’s a good idea to put here what the module actually does in detail.|
|Write Changes||Add any user-visible changes since the last release to your Changes file.|
Step-by-step: Distributing your module
|Get a CPAN user ID||Every developer publishing modules on CPAN needs a CPAN ID. Visit
Once you’ve ensured that your module passes its own tests - always a good thing to make sure - you can
|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.
Alternatively you can use the cpan-upload script, part of the CPAN::Uploader distribution on CPAN.
|Fix bugs!||Once you start accumulating users, they’ll send you bug reports. If you’re lucky, they’ll even send you patches. Welcome to the joys of maintaining a software project...|