1N/A=head1 NAME
1N/A
1N/Aperlnewmod - preparing a new module for distribution
1N/A
1N/A=head1 DESCRIPTION
1N/A
1N/AThis document gives you some suggestions about how to go about writing
1N/APerl modules, preparing them for distribution, and making them available
1N/Avia CPAN.
1N/A
1N/AOne of the things that makes Perl really powerful is the fact that Perl
1N/Ahackers tend to want to share the solutions to problems they've faced,
1N/Aso you and I don't have to battle with the same problem again.
1N/A
1N/AThe main way they do this is by abstracting the solution into a Perl
1N/Amodule. If you don't know what one of these is, the rest of this
1N/Adocument isn't going to be much use to you. You're also missing out on
1N/Aan awful lot of useful code; consider having a look at L<perlmod>,
1N/AL<perlmodlib> and L<perlmodinstall> before coming back here.
1N/A
1N/AWhen you've found that there isn't a module available for what you're
1N/Atrying to do, and you've had to write the code yourself, consider
1N/Apackaging up the solution into a module and uploading it to CPAN so that
1N/Aothers can benefit.
1N/A
1N/A=head2 Warning
1N/A
1N/AWe're going to primarily concentrate on Perl-only modules here, rather
1N/Athan XS modules. XS modules serve a rather different purpose, and
1N/Ayou should consider different things before distributing them - the
1N/Apopularity of the library you are gluing, the portability to other
1N/Aoperating systems, and so on. However, the notes on preparing the Perl
1N/Aside of the module and packaging and distributing it will apply equally
1N/Awell to an XS module as a pure-Perl one.
1N/A
1N/A=head2 What should I make into a module?
1N/A
1N/AYou should make a module out of any code that you think is going to be
1N/Auseful to others. Anything that's likely to fill a hole in the communal
1N/Alibrary and which someone else can slot directly into their program. Any
1N/Apart of your code which you can isolate and extract and plug into
1N/Asomething else is a likely candidate.
1N/A
1N/ALet's take an example. Suppose you're reading in data from a local
1N/Aformat into a hash-of-hashes in Perl, turning that into a tree, walking
1N/Athe tree and then piping each node to an Acme Transmogrifier Server.
1N/A
1N/ANow, quite a few people have the Acme Transmogrifier, and you've had to
1N/Awrite something to talk the protocol from scratch - you'd almost
1N/Acertainly want to make that into a module. The level at which you pitch
1N/Ait is up to you: you might want protocol-level modules analogous to
1N/AL<Net::SMTP|Net::SMTP> which then talk to higher level modules analogous
1N/Ato L<Mail::Send|Mail::Send>. The choice is yours, but you do want to get
1N/Aa module out for that server protocol.
1N/A
1N/ANobody else on the planet is going to talk your local data format, so we
1N/Acan ignore that. But what about the thing in the middle? Building tree
1N/Astructures from Perl variables and then traversing them is a nice,
1N/Ageneral problem, and if nobody's already written a module that does
1N/Athat, you might want to modularise that code too.
1N/A
1N/ASo hopefully you've now got a few ideas about what's good to modularise.
1N/ALet's now see how it's done.
1N/A
1N/A=head2 Step-by-step: Preparing the ground
1N/A
1N/ABefore we even start scraping out the code, there are a few things we'll
1N/Awant to do in advance.
1N/A
1N/A=over 3
1N/A
1N/A=item Look around
1N/A
1N/ADig into a bunch of modules to see how they're written. I'd suggest
1N/Astarting with L<Text::Tabs|Text::Tabs>, since it's in the standard
1N/Alibrary and is nice and simple, and then looking at something like
1N/AL<Time::Zone|Time::Zone>, L<File::Copy|File::Copy> and then some of the
1N/AC<Mail::*> modules if you're planning on writing object oriented code.
1N/A
1N/AThese should give you an overall feel for how modules are laid out and
1N/Awritten.
1N/A
1N/A=item Check it's new
1N/A
1N/AThere are a lot of modules on CPAN, and it's easy to miss one that's
1N/Asimilar to what you're planning on contributing. Have a good plough
1N/Athrough the modules list and the F<by-module> directories, and make sure
1N/Ayou're not the one reinventing the wheel!
1N/A
1N/A=item Discuss the need
1N/A
1N/AYou might love it. You might feel that everyone else needs it. But there
1N/Amight not actually be any real demand for it out there. If you're unsure
1N/Aabout the demand your module will have, consider sending out feelers
1N/Aon the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the
1N/Amodules list at C<modules@perl.org>. Remember that this is a closed list
1N/Awith a very long turn-around time - be prepared to wait a good while for
1N/Aa response from them.
1N/A
1N/A=item Choose a name
1N/A
1N/APerl modules included on CPAN have a naming hierarchy you should try to
1N/Afit in with. See L<perlmodlib> for more details on how this works, and
1N/Abrowse around CPAN and the modules list to get a feel of it. At the very
1N/Aleast, remember this: modules should be title capitalised, (This::Thing)
1N/Afit in with a category, and explain their purpose succinctly.
1N/A
1N/A=item Check again
1N/A
1N/AWhile you're doing that, make really sure you haven't missed a module
1N/Asimilar to the one you're about to write.
1N/A
1N/AWhen you've got your name sorted out and you're sure that your module is
1N/Awanted and not currently available, it's time to start coding.
1N/A
1N/A=back
1N/A
1N/A=head2 Step-by-step: Making the module
1N/A
1N/A=over 3
1N/A
1N/A=item Start with F<h2xs>
1N/A
1N/AOriginally a utility to convert C header files into XS modules,
1N/AL<h2xs|h2xs> has become a useful utility for churning out skeletons for
1N/APerl-only modules as well. If you don't want to use the
1N/AL<Autoloader|Autoloader> which splits up big modules into smaller
1N/Asubroutine-sized chunks, you'll say something like this:
1N/A
1N/A h2xs -AX -n Net::Acme
1N/A
1N/AThe C<-A> omits the Autoloader code, C<-X> omits XS elements, and C<-n>
1N/Aspecifies the name of the module.
1N/A
1N/A=item Use L<strict|strict> and L<warnings|warnings>
1N/A
1N/AA module's code has to be warning and strict-clean, since you can't
1N/Aguarantee the conditions that it'll be used under. Besides, you wouldn't
1N/Awant to distribute code that wasn't warning or strict-clean anyway,
1N/Aright?
1N/A
1N/A=item Use L<Carp|Carp>
1N/A
1N/AThe L<Carp|Carp> module allows you to present your error messages from
1N/Athe caller's perspective; this gives you a way to signal a problem with
1N/Athe caller and not your module. For instance, if you say this:
1N/A
1N/A warn "No hostname given";
1N/A
1N/Athe user will see something like this:
1N/A
1N/A No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm
1N/A line 123.
1N/A
1N/Awhich looks like your module is doing something wrong. Instead, you want
1N/Ato put the blame on the user, and say this:
1N/A
1N/A No hostname given at bad_code, line 10.
1N/A
1N/AYou do this by using L<Carp|Carp> and replacing your C<warn>s with
1N/AC<carp>s. If you need to C<die>, say C<croak> instead. However, keep
1N/AC<warn> and C<die> in place for your sanity checks - where it really is
1N/Ayour module at fault.
1N/A
1N/A=item Use L<Exporter|Exporter> - wisely!
1N/A
1N/AC<h2xs> provides stubs for L<Exporter|Exporter>, which gives you a
1N/Astandard way of exporting symbols and subroutines from your module into
1N/Athe caller's namespace. For instance, saying C<use Net::Acme qw(&frob)>
1N/Awould import the C<frob> subroutine.
1N/A
1N/AThe package variable C<@EXPORT> will determine which symbols will get
1N/Aexported when the caller simply says C<use Net::Acme> - you will hardly
1N/Aever want to put anything in there. C<@EXPORT_OK>, on the other hand,
1N/Aspecifies which symbols you're willing to export. If you do want to
1N/Aexport a bunch of symbols, use the C<%EXPORT_TAGS> and define a standard
1N/Aexport set - look at L<Exporter> for more details.
1N/A
1N/A=item Use L<plain old documentation|perlpod>
1N/A
1N/AThe work isn't over until the paperwork is done, and you're going to
1N/Aneed to put in some time writing some documentation for your module.
1N/AC<h2xs> will provide a stub for you to fill in; if you're not sure about
1N/Athe format, look at L<perlpod> for an introduction. Provide a good
1N/Asynopsis of how your module is used in code, a description, and then
1N/Anotes on the syntax and function of the individual subroutines or
1N/Amethods. Use Perl comments for developer notes and POD for end-user
1N/Anotes.
1N/A
1N/A=item Write tests
1N/A
1N/AYou're encouraged to create self-tests for your module to ensure it's
1N/Aworking as intended on the myriad platforms Perl supports; if you upload
1N/Ayour module to CPAN, a host of testers will build your module and send
1N/Ayou the results of the tests. Again, C<h2xs> provides a test framework
1N/Awhich you can extend - you should do something more than just checking
1N/Ayour module will compile.
1N/A
1N/A=item Write the README
1N/A
1N/AIf you're uploading to CPAN, the automated gremlins will extract the
1N/AREADME file and place that in your CPAN directory. It'll also appear in
1N/Athe main F<by-module> and F<by-category> directories if you make it onto
1N/Athe modules list. It's a good idea to put here what the module actually
1N/Adoes in detail, and the user-visible changes since the last release.
1N/A
1N/A=back
1N/A
1N/A=head2 Step-by-step: Distributing your module
1N/A
1N/A=over 3
1N/A
1N/A=item Get a CPAN user ID
1N/A
1N/AEvery developer publishing modules on CPAN needs a CPAN ID. See the
1N/Ainstructions at C<http://www.cpan.org/modules/04pause.html> (or
1N/Aequivalent on your nearest mirror) to find out how to do this.
1N/A
1N/A=item C<perl Makefile.PL; make test; make dist>
1N/A
1N/AOnce again, C<h2xs> has done all the work for you. It produces the
1N/Astandard C<Makefile.PL> you'll have seen when you downloaded and
1N/Ainstalls modules, and this produces a Makefile with a C<dist> target.
1N/A
1N/AOnce you've ensured that your module passes its own tests - always a
1N/Agood thing to make sure - you can C<make dist>, and the Makefile will
1N/Ahopefully produce you a nice tarball of your module, ready for upload.
1N/A
1N/A=item Upload the tarball
1N/A
1N/AThe email you got when you received your CPAN ID will tell you how to
1N/Alog in to PAUSE, the Perl Authors Upload SErver. From the menus there,
1N/Ayou can upload your module to CPAN.
1N/A
1N/A=item Announce to the modules list
1N/A
1N/AOnce uploaded, it'll sit unnoticed in your author directory. If you want
1N/Ait connected to the rest of the CPAN, you'll need to tell the modules
1N/Alist about it. The best way to do this is to email them a line in the
1N/Astyle of the modules list, like this:
1N/A
1N/A Net::Acme bdpOP Interface to Acme Frobnicator servers FOOBAR
1N/A ^ ^^^^^ ^ ^
1N/A | ||||| Module description Your ID
1N/A | |||||
1N/A | ||||\-Public Licence: (p)standard Perl, (g)GPL, (b)BSD,
1N/A | |||| (l)LGPL, (a)rtistic, (o)ther
1N/A | ||||
1N/A | |||\- Interface: (O)OP, (r)eferences, (h)ybrid, (f)unctions
1N/A | |||
1N/A | ||\-- Language: (p)ure Perl, C(+)+, (h)ybrid, (C), (o)ther
1N/A | ||
1N/A Module |\--- Support: (d)eveloper, (m)ailing list, (u)senet, (n)one
1N/A Name |
1N/A \---- Development: (i)dea, (c)onstructions, (a)lpha, (b)eta,
1N/A (R)eleased, (M)ature, (S)tandard
1N/A
1N/Aplus a description of the module and why you think it should be
1N/Aincluded. If you hear nothing back, that means your module will
1N/Aprobably appear on the modules list at the next update. Don't try
1N/Asubscribing to C<modules@perl.org>; it's not another mailing list. Just
1N/Ahave patience.
1N/A
1N/A=item Announce to clpa
1N/A
1N/AIf you have a burning desire to tell the world about your release, post
1N/Aan announcement to the moderated C<comp.lang.perl.announce> newsgroup.
1N/A
1N/A=item Fix bugs!
1N/A
1N/AOnce you start accumulating users, they'll send you bug reports. If
1N/Ayou're lucky, they'll even send you patches. Welcome to the joys of
1N/Amaintaining a software project...
1N/A
1N/A=back
1N/A
1N/A=head1 AUTHOR
1N/A
1N/ASimon Cozens, C<simon@cpan.org>
1N/A
1N/A=head1 SEE ALSO
1N/A
1N/AL<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>,
1N/AL<Carp>, L<Exporter>, L<perlpod>, L<Test>, L<ExtUtils::MakeMaker>,
1N/Ahttp://www.cpan.org/ , Ken Williams' tutorial on building your own
1N/Amodule at http://mathforum.org/~ken/perl_modules.html