perlnewmod - preparing a new module for distribution |
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 the perlmod manpage, perlmodlib and the perlmodinstall manpage 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 the perlmodstyle manpage 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.
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.
Before we even start scraping out the code, there are a few things we'll want to do in advance.
Email::*
modules provide some good
examples.
These should give you an overall feel for how modules are laid out and written.
module-authors@perl.org
mailing list (send an email to
module-authors-subscribe@perl.org
to subscribe; see
http://lists.perl.org/list/module-authors.html for more information
and a link to the archives).
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.
module-starter --module=Foo::Bar \ --author="Your Name" --email=yourname@cpan.org
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:
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.
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 warn
s with
carp
s. 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 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 you're 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 the Exporter manpage for more details.
module-starter
or h2xs
will provide a stub for you to fill in; if
you're not sure about the format, look at the perlpod manpage 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.
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.
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 distcheck; make dist
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 you've ensured that your module passes its own tests - always a
good thing to make sure - you can make distcheck
to make sure
everything looks OK, followed by make dist
, and the Makefile will
hopefully produce you a nice tarball of your module, ready for upload.
Alternatively you can use the cpan-upload script, part of the the CPAN::Uploader manpage distribution on CPAN.
Simon Cozens, simon@cpan.org
Updated by Kirrily ``Skud'' Robert, skud@cpan.org
the perlmod manpage, perlmodlib, the perlmodinstall manpage, h2xs, the strict manpage, the Carp manpage, the Exporter manpage, the perlpod manpage, the Test::Simple manpage, the Test::More manpage the ExtUtils::MakeMaker manpage, the Module::Build manpage, the Module::Starter manpage http://www.cpan.org/, Ken Williams' tutorial on building your own module at http://mathforum.org/~ken/perl_modules.html
perlnewmod - preparing a new module for distribution |