1*0Sstevel@tonic-gate=head1 NAME 2*0Sstevel@tonic-gate 3*0Sstevel@tonic-gateperlnewmod - preparing a new module for distribution 4*0Sstevel@tonic-gate 5*0Sstevel@tonic-gate=head1 DESCRIPTION 6*0Sstevel@tonic-gate 7*0Sstevel@tonic-gateThis document gives you some suggestions about how to go about writing 8*0Sstevel@tonic-gatePerl modules, preparing them for distribution, and making them available 9*0Sstevel@tonic-gatevia CPAN. 10*0Sstevel@tonic-gate 11*0Sstevel@tonic-gateOne of the things that makes Perl really powerful is the fact that Perl 12*0Sstevel@tonic-gatehackers tend to want to share the solutions to problems they've faced, 13*0Sstevel@tonic-gateso you and I don't have to battle with the same problem again. 14*0Sstevel@tonic-gate 15*0Sstevel@tonic-gateThe main way they do this is by abstracting the solution into a Perl 16*0Sstevel@tonic-gatemodule. If you don't know what one of these is, the rest of this 17*0Sstevel@tonic-gatedocument isn't going to be much use to you. You're also missing out on 18*0Sstevel@tonic-gatean awful lot of useful code; consider having a look at L<perlmod>, 19*0Sstevel@tonic-gateL<perlmodlib> and L<perlmodinstall> before coming back here. 20*0Sstevel@tonic-gate 21*0Sstevel@tonic-gateWhen you've found that there isn't a module available for what you're 22*0Sstevel@tonic-gatetrying to do, and you've had to write the code yourself, consider 23*0Sstevel@tonic-gatepackaging up the solution into a module and uploading it to CPAN so that 24*0Sstevel@tonic-gateothers can benefit. 25*0Sstevel@tonic-gate 26*0Sstevel@tonic-gate=head2 Warning 27*0Sstevel@tonic-gate 28*0Sstevel@tonic-gateWe're going to primarily concentrate on Perl-only modules here, rather 29*0Sstevel@tonic-gatethan XS modules. XS modules serve a rather different purpose, and 30*0Sstevel@tonic-gateyou should consider different things before distributing them - the 31*0Sstevel@tonic-gatepopularity of the library you are gluing, the portability to other 32*0Sstevel@tonic-gateoperating systems, and so on. However, the notes on preparing the Perl 33*0Sstevel@tonic-gateside of the module and packaging and distributing it will apply equally 34*0Sstevel@tonic-gatewell to an XS module as a pure-Perl one. 35*0Sstevel@tonic-gate 36*0Sstevel@tonic-gate=head2 What should I make into a module? 37*0Sstevel@tonic-gate 38*0Sstevel@tonic-gateYou should make a module out of any code that you think is going to be 39*0Sstevel@tonic-gateuseful to others. Anything that's likely to fill a hole in the communal 40*0Sstevel@tonic-gatelibrary and which someone else can slot directly into their program. Any 41*0Sstevel@tonic-gatepart of your code which you can isolate and extract and plug into 42*0Sstevel@tonic-gatesomething else is a likely candidate. 43*0Sstevel@tonic-gate 44*0Sstevel@tonic-gateLet's take an example. Suppose you're reading in data from a local 45*0Sstevel@tonic-gateformat into a hash-of-hashes in Perl, turning that into a tree, walking 46*0Sstevel@tonic-gatethe tree and then piping each node to an Acme Transmogrifier Server. 47*0Sstevel@tonic-gate 48*0Sstevel@tonic-gateNow, quite a few people have the Acme Transmogrifier, and you've had to 49*0Sstevel@tonic-gatewrite something to talk the protocol from scratch - you'd almost 50*0Sstevel@tonic-gatecertainly want to make that into a module. The level at which you pitch 51*0Sstevel@tonic-gateit is up to you: you might want protocol-level modules analogous to 52*0Sstevel@tonic-gateL<Net::SMTP|Net::SMTP> which then talk to higher level modules analogous 53*0Sstevel@tonic-gateto L<Mail::Send|Mail::Send>. The choice is yours, but you do want to get 54*0Sstevel@tonic-gatea module out for that server protocol. 55*0Sstevel@tonic-gate 56*0Sstevel@tonic-gateNobody else on the planet is going to talk your local data format, so we 57*0Sstevel@tonic-gatecan ignore that. But what about the thing in the middle? Building tree 58*0Sstevel@tonic-gatestructures from Perl variables and then traversing them is a nice, 59*0Sstevel@tonic-gategeneral problem, and if nobody's already written a module that does 60*0Sstevel@tonic-gatethat, you might want to modularise that code too. 61*0Sstevel@tonic-gate 62*0Sstevel@tonic-gateSo hopefully you've now got a few ideas about what's good to modularise. 63*0Sstevel@tonic-gateLet's now see how it's done. 64*0Sstevel@tonic-gate 65*0Sstevel@tonic-gate=head2 Step-by-step: Preparing the ground 66*0Sstevel@tonic-gate 67*0Sstevel@tonic-gateBefore we even start scraping out the code, there are a few things we'll 68*0Sstevel@tonic-gatewant to do in advance. 69*0Sstevel@tonic-gate 70*0Sstevel@tonic-gate=over 3 71*0Sstevel@tonic-gate 72*0Sstevel@tonic-gate=item Look around 73*0Sstevel@tonic-gate 74*0Sstevel@tonic-gateDig into a bunch of modules to see how they're written. I'd suggest 75*0Sstevel@tonic-gatestarting with L<Text::Tabs|Text::Tabs>, since it's in the standard 76*0Sstevel@tonic-gatelibrary and is nice and simple, and then looking at something like 77*0Sstevel@tonic-gateL<Time::Zone|Time::Zone>, L<File::Copy|File::Copy> and then some of the 78*0Sstevel@tonic-gateC<Mail::*> modules if you're planning on writing object oriented code. 79*0Sstevel@tonic-gate 80*0Sstevel@tonic-gateThese should give you an overall feel for how modules are laid out and 81*0Sstevel@tonic-gatewritten. 82*0Sstevel@tonic-gate 83*0Sstevel@tonic-gate=item Check it's new 84*0Sstevel@tonic-gate 85*0Sstevel@tonic-gateThere are a lot of modules on CPAN, and it's easy to miss one that's 86*0Sstevel@tonic-gatesimilar to what you're planning on contributing. Have a good plough 87*0Sstevel@tonic-gatethrough the modules list and the F<by-module> directories, and make sure 88*0Sstevel@tonic-gateyou're not the one reinventing the wheel! 89*0Sstevel@tonic-gate 90*0Sstevel@tonic-gate=item Discuss the need 91*0Sstevel@tonic-gate 92*0Sstevel@tonic-gateYou might love it. You might feel that everyone else needs it. But there 93*0Sstevel@tonic-gatemight not actually be any real demand for it out there. If you're unsure 94*0Sstevel@tonic-gateabout the demand your module will have, consider sending out feelers 95*0Sstevel@tonic-gateon the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the 96*0Sstevel@tonic-gatemodules list at C<modules@perl.org>. Remember that this is a closed list 97*0Sstevel@tonic-gatewith a very long turn-around time - be prepared to wait a good while for 98*0Sstevel@tonic-gatea response from them. 99*0Sstevel@tonic-gate 100*0Sstevel@tonic-gate=item Choose a name 101*0Sstevel@tonic-gate 102*0Sstevel@tonic-gatePerl modules included on CPAN have a naming hierarchy you should try to 103*0Sstevel@tonic-gatefit in with. See L<perlmodlib> for more details on how this works, and 104*0Sstevel@tonic-gatebrowse around CPAN and the modules list to get a feel of it. At the very 105*0Sstevel@tonic-gateleast, remember this: modules should be title capitalised, (This::Thing) 106*0Sstevel@tonic-gatefit in with a category, and explain their purpose succinctly. 107*0Sstevel@tonic-gate 108*0Sstevel@tonic-gate=item Check again 109*0Sstevel@tonic-gate 110*0Sstevel@tonic-gateWhile you're doing that, make really sure you haven't missed a module 111*0Sstevel@tonic-gatesimilar to the one you're about to write. 112*0Sstevel@tonic-gate 113*0Sstevel@tonic-gateWhen you've got your name sorted out and you're sure that your module is 114*0Sstevel@tonic-gatewanted and not currently available, it's time to start coding. 115*0Sstevel@tonic-gate 116*0Sstevel@tonic-gate=back 117*0Sstevel@tonic-gate 118*0Sstevel@tonic-gate=head2 Step-by-step: Making the module 119*0Sstevel@tonic-gate 120*0Sstevel@tonic-gate=over 3 121*0Sstevel@tonic-gate 122*0Sstevel@tonic-gate=item Start with F<h2xs> 123*0Sstevel@tonic-gate 124*0Sstevel@tonic-gateOriginally a utility to convert C header files into XS modules, 125*0Sstevel@tonic-gateL<h2xs|h2xs> has become a useful utility for churning out skeletons for 126*0Sstevel@tonic-gatePerl-only modules as well. If you don't want to use the 127*0Sstevel@tonic-gateL<Autoloader|Autoloader> which splits up big modules into smaller 128*0Sstevel@tonic-gatesubroutine-sized chunks, you'll say something like this: 129*0Sstevel@tonic-gate 130*0Sstevel@tonic-gate h2xs -AX -n Net::Acme 131*0Sstevel@tonic-gate 132*0Sstevel@tonic-gateThe C<-A> omits the Autoloader code, C<-X> omits XS elements, and C<-n> 133*0Sstevel@tonic-gatespecifies the name of the module. 134*0Sstevel@tonic-gate 135*0Sstevel@tonic-gate=item Use L<strict|strict> and L<warnings|warnings> 136*0Sstevel@tonic-gate 137*0Sstevel@tonic-gateA module's code has to be warning and strict-clean, since you can't 138*0Sstevel@tonic-gateguarantee the conditions that it'll be used under. Besides, you wouldn't 139*0Sstevel@tonic-gatewant to distribute code that wasn't warning or strict-clean anyway, 140*0Sstevel@tonic-gateright? 141*0Sstevel@tonic-gate 142*0Sstevel@tonic-gate=item Use L<Carp|Carp> 143*0Sstevel@tonic-gate 144*0Sstevel@tonic-gateThe L<Carp|Carp> module allows you to present your error messages from 145*0Sstevel@tonic-gatethe caller's perspective; this gives you a way to signal a problem with 146*0Sstevel@tonic-gatethe caller and not your module. For instance, if you say this: 147*0Sstevel@tonic-gate 148*0Sstevel@tonic-gate warn "No hostname given"; 149*0Sstevel@tonic-gate 150*0Sstevel@tonic-gatethe user will see something like this: 151*0Sstevel@tonic-gate 152*0Sstevel@tonic-gate No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm 153*0Sstevel@tonic-gate line 123. 154*0Sstevel@tonic-gate 155*0Sstevel@tonic-gatewhich looks like your module is doing something wrong. Instead, you want 156*0Sstevel@tonic-gateto put the blame on the user, and say this: 157*0Sstevel@tonic-gate 158*0Sstevel@tonic-gate No hostname given at bad_code, line 10. 159*0Sstevel@tonic-gate 160*0Sstevel@tonic-gateYou do this by using L<Carp|Carp> and replacing your C<warn>s with 161*0Sstevel@tonic-gateC<carp>s. If you need to C<die>, say C<croak> instead. However, keep 162*0Sstevel@tonic-gateC<warn> and C<die> in place for your sanity checks - where it really is 163*0Sstevel@tonic-gateyour module at fault. 164*0Sstevel@tonic-gate 165*0Sstevel@tonic-gate=item Use L<Exporter|Exporter> - wisely! 166*0Sstevel@tonic-gate 167*0Sstevel@tonic-gateC<h2xs> provides stubs for L<Exporter|Exporter>, which gives you a 168*0Sstevel@tonic-gatestandard way of exporting symbols and subroutines from your module into 169*0Sstevel@tonic-gatethe caller's namespace. For instance, saying C<use Net::Acme qw(&frob)> 170*0Sstevel@tonic-gatewould import the C<frob> subroutine. 171*0Sstevel@tonic-gate 172*0Sstevel@tonic-gateThe package variable C<@EXPORT> will determine which symbols will get 173*0Sstevel@tonic-gateexported when the caller simply says C<use Net::Acme> - you will hardly 174*0Sstevel@tonic-gateever want to put anything in there. C<@EXPORT_OK>, on the other hand, 175*0Sstevel@tonic-gatespecifies which symbols you're willing to export. If you do want to 176*0Sstevel@tonic-gateexport a bunch of symbols, use the C<%EXPORT_TAGS> and define a standard 177*0Sstevel@tonic-gateexport set - look at L<Exporter> for more details. 178*0Sstevel@tonic-gate 179*0Sstevel@tonic-gate=item Use L<plain old documentation|perlpod> 180*0Sstevel@tonic-gate 181*0Sstevel@tonic-gateThe work isn't over until the paperwork is done, and you're going to 182*0Sstevel@tonic-gateneed to put in some time writing some documentation for your module. 183*0Sstevel@tonic-gateC<h2xs> will provide a stub for you to fill in; if you're not sure about 184*0Sstevel@tonic-gatethe format, look at L<perlpod> for an introduction. Provide a good 185*0Sstevel@tonic-gatesynopsis of how your module is used in code, a description, and then 186*0Sstevel@tonic-gatenotes on the syntax and function of the individual subroutines or 187*0Sstevel@tonic-gatemethods. Use Perl comments for developer notes and POD for end-user 188*0Sstevel@tonic-gatenotes. 189*0Sstevel@tonic-gate 190*0Sstevel@tonic-gate=item Write tests 191*0Sstevel@tonic-gate 192*0Sstevel@tonic-gateYou're encouraged to create self-tests for your module to ensure it's 193*0Sstevel@tonic-gateworking as intended on the myriad platforms Perl supports; if you upload 194*0Sstevel@tonic-gateyour module to CPAN, a host of testers will build your module and send 195*0Sstevel@tonic-gateyou the results of the tests. Again, C<h2xs> provides a test framework 196*0Sstevel@tonic-gatewhich you can extend - you should do something more than just checking 197*0Sstevel@tonic-gateyour module will compile. 198*0Sstevel@tonic-gate 199*0Sstevel@tonic-gate=item Write the README 200*0Sstevel@tonic-gate 201*0Sstevel@tonic-gateIf you're uploading to CPAN, the automated gremlins will extract the 202*0Sstevel@tonic-gateREADME file and place that in your CPAN directory. It'll also appear in 203*0Sstevel@tonic-gatethe main F<by-module> and F<by-category> directories if you make it onto 204*0Sstevel@tonic-gatethe modules list. It's a good idea to put here what the module actually 205*0Sstevel@tonic-gatedoes in detail, and the user-visible changes since the last release. 206*0Sstevel@tonic-gate 207*0Sstevel@tonic-gate=back 208*0Sstevel@tonic-gate 209*0Sstevel@tonic-gate=head2 Step-by-step: Distributing your module 210*0Sstevel@tonic-gate 211*0Sstevel@tonic-gate=over 3 212*0Sstevel@tonic-gate 213*0Sstevel@tonic-gate=item Get a CPAN user ID 214*0Sstevel@tonic-gate 215*0Sstevel@tonic-gateEvery developer publishing modules on CPAN needs a CPAN ID. See the 216*0Sstevel@tonic-gateinstructions at C<http://www.cpan.org/modules/04pause.html> (or 217*0Sstevel@tonic-gateequivalent on your nearest mirror) to find out how to do this. 218*0Sstevel@tonic-gate 219*0Sstevel@tonic-gate=item C<perl Makefile.PL; make test; make dist> 220*0Sstevel@tonic-gate 221*0Sstevel@tonic-gateOnce again, C<h2xs> has done all the work for you. It produces the 222*0Sstevel@tonic-gatestandard C<Makefile.PL> you'll have seen when you downloaded and 223*0Sstevel@tonic-gateinstalls modules, and this produces a Makefile with a C<dist> target. 224*0Sstevel@tonic-gate 225*0Sstevel@tonic-gateOnce you've ensured that your module passes its own tests - always a 226*0Sstevel@tonic-gategood thing to make sure - you can C<make dist>, and the Makefile will 227*0Sstevel@tonic-gatehopefully produce you a nice tarball of your module, ready for upload. 228*0Sstevel@tonic-gate 229*0Sstevel@tonic-gate=item Upload the tarball 230*0Sstevel@tonic-gate 231*0Sstevel@tonic-gateThe email you got when you received your CPAN ID will tell you how to 232*0Sstevel@tonic-gatelog in to PAUSE, the Perl Authors Upload SErver. From the menus there, 233*0Sstevel@tonic-gateyou can upload your module to CPAN. 234*0Sstevel@tonic-gate 235*0Sstevel@tonic-gate=item Announce to the modules list 236*0Sstevel@tonic-gate 237*0Sstevel@tonic-gateOnce uploaded, it'll sit unnoticed in your author directory. If you want 238*0Sstevel@tonic-gateit connected to the rest of the CPAN, you'll need to tell the modules 239*0Sstevel@tonic-gatelist about it. The best way to do this is to email them a line in the 240*0Sstevel@tonic-gatestyle of the modules list, like this: 241*0Sstevel@tonic-gate 242*0Sstevel@tonic-gate Net::Acme bdpOP Interface to Acme Frobnicator servers FOOBAR 243*0Sstevel@tonic-gate ^ ^^^^^ ^ ^ 244*0Sstevel@tonic-gate | ||||| Module description Your ID 245*0Sstevel@tonic-gate | ||||| 246*0Sstevel@tonic-gate | ||||\-Public Licence: (p)standard Perl, (g)GPL, (b)BSD, 247*0Sstevel@tonic-gate | |||| (l)LGPL, (a)rtistic, (o)ther 248*0Sstevel@tonic-gate | |||| 249*0Sstevel@tonic-gate | |||\- Interface: (O)OP, (r)eferences, (h)ybrid, (f)unctions 250*0Sstevel@tonic-gate | ||| 251*0Sstevel@tonic-gate | ||\-- Language: (p)ure Perl, C(+)+, (h)ybrid, (C), (o)ther 252*0Sstevel@tonic-gate | || 253*0Sstevel@tonic-gate Module |\--- Support: (d)eveloper, (m)ailing list, (u)senet, (n)one 254*0Sstevel@tonic-gate Name | 255*0Sstevel@tonic-gate \---- Development: (i)dea, (c)onstructions, (a)lpha, (b)eta, 256*0Sstevel@tonic-gate (R)eleased, (M)ature, (S)tandard 257*0Sstevel@tonic-gate 258*0Sstevel@tonic-gateplus a description of the module and why you think it should be 259*0Sstevel@tonic-gateincluded. If you hear nothing back, that means your module will 260*0Sstevel@tonic-gateprobably appear on the modules list at the next update. Don't try 261*0Sstevel@tonic-gatesubscribing to C<modules@perl.org>; it's not another mailing list. Just 262*0Sstevel@tonic-gatehave patience. 263*0Sstevel@tonic-gate 264*0Sstevel@tonic-gate=item Announce to clpa 265*0Sstevel@tonic-gate 266*0Sstevel@tonic-gateIf you have a burning desire to tell the world about your release, post 267*0Sstevel@tonic-gatean announcement to the moderated C<comp.lang.perl.announce> newsgroup. 268*0Sstevel@tonic-gate 269*0Sstevel@tonic-gate=item Fix bugs! 270*0Sstevel@tonic-gate 271*0Sstevel@tonic-gateOnce you start accumulating users, they'll send you bug reports. If 272*0Sstevel@tonic-gateyou're lucky, they'll even send you patches. Welcome to the joys of 273*0Sstevel@tonic-gatemaintaining a software project... 274*0Sstevel@tonic-gate 275*0Sstevel@tonic-gate=back 276*0Sstevel@tonic-gate 277*0Sstevel@tonic-gate=head1 AUTHOR 278*0Sstevel@tonic-gate 279*0Sstevel@tonic-gateSimon Cozens, C<simon@cpan.org> 280*0Sstevel@tonic-gate 281*0Sstevel@tonic-gate=head1 SEE ALSO 282*0Sstevel@tonic-gate 283*0Sstevel@tonic-gateL<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>, 284*0Sstevel@tonic-gateL<Carp>, L<Exporter>, L<perlpod>, L<Test>, L<ExtUtils::MakeMaker>, 285*0Sstevel@tonic-gatehttp://www.cpan.org/ , Ken Williams' tutorial on building your own 286*0Sstevel@tonic-gatemodule at http://mathforum.org/~ken/perl_modules.html 287