1=head1 NAME 2 3perlnewmod - preparing a new module for distribution 4 5=head1 DESCRIPTION 6 7This document gives you some suggestions about how to go about writing 8Perl modules, preparing them for distribution, and making them available 9via CPAN. 10 11One of the things that makes Perl really powerful is the fact that Perl 12hackers tend to want to share the solutions to problems they've faced, 13so you and I don't have to battle with the same problem again. 14 15The main way they do this is by abstracting the solution into a Perl 16module. If you don't know what one of these is, the rest of this 17document isn't going to be much use to you. You're also missing out on 18an awful lot of useful code; consider having a look at L<perlmod>, 19L<perlmodlib> and L<perlmodinstall> before coming back here. 20 21When you've found that there isn't a module available for what you're 22trying to do, and you've had to write the code yourself, consider 23packaging up the solution into a module and uploading it to CPAN so that 24others can benefit. 25 26=head2 Warning 27 28We're going to primarily concentrate on Perl-only modules here, rather 29than XS modules. XS modules serve a rather different purpose, and 30you should consider different things before distributing them - the 31popularity of the library you are gluing, the portability to other 32operating systems, and so on. However, the notes on preparing the Perl 33side of the module and packaging and distributing it will apply equally 34well to an XS module as a pure-Perl one. 35 36=head2 What should I make into a module? 37 38You should make a module out of any code that you think is going to be 39useful to others. Anything that's likely to fill a hole in the communal 40library and which someone else can slot directly into their program. Any 41part of your code which you can isolate and extract and plug into 42something else is a likely candidate. 43 44Let's take an example. Suppose you're reading in data from a local 45format into a hash-of-hashes in Perl, turning that into a tree, walking 46the tree and then piping each node to an Acme Transmogrifier Server. 47 48Now, quite a few people have the Acme Transmogrifier, and you've had to 49write something to talk the protocol from scratch - you'd almost 50certainly want to make that into a module. The level at which you pitch 51it is up to you: you might want protocol-level modules analogous to 52L<Net::SMTP|Net::SMTP> which then talk to higher level modules analogous 53to L<Mail::Send|Mail::Send>. The choice is yours, but you do want to get 54a module out for that server protocol. 55 56Nobody else on the planet is going to talk your local data format, so we 57can ignore that. But what about the thing in the middle? Building tree 58structures from Perl variables and then traversing them is a nice, 59general problem, and if nobody's already written a module that does 60that, you might want to modularise that code too. 61 62So hopefully you've now got a few ideas about what's good to modularise. 63Let's now see how it's done. 64 65=head2 Step-by-step: Preparing the ground 66 67Before we even start scraping out the code, there are a few things we'll 68want to do in advance. 69 70=over 3 71 72=item Look around 73 74Dig into a bunch of modules to see how they're written. I'd suggest 75starting with L<Text::Tabs|Text::Tabs>, since it's in the standard 76library and is nice and simple, and then looking at something a little 77more complex like L<File::Copy|File::Copy>. For object oriented 78code, C<WWW::Mechanize> or the C<Email::*> modules provide some good 79examples. 80 81These should give you an overall feel for how modules are laid out and 82written. 83 84=item Check it's new 85 86There are a lot of modules on CPAN, and it's easy to miss one that's 87similar to what you're planning on contributing. Have a good plough 88through the L<http://search.cpan.org> and make sure you're not the one 89reinventing the wheel! 90 91=item Discuss the need 92 93You might love it. You might feel that everyone else needs it. But there 94might not actually be any real demand for it out there. If you're unsure 95about the demand your module will have, consider sending out feelers 96on the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the 97modules list at C<modules@perl.org>. Remember that this is a closed list 98with a very long turn-around time - be prepared to wait a good while for 99a response from them. 100 101=item Choose a name 102 103Perl modules included on CPAN have a naming hierarchy you should try to 104fit in with. See L<perlmodlib> for more details on how this works, and 105browse around CPAN and the modules list to get a feel of it. At the very 106least, remember this: modules should be title capitalised, (This::Thing) 107fit in with a category, and explain their purpose succinctly. 108 109=item Check again 110 111While you're doing that, make really sure you haven't missed a module 112similar to the one you're about to write. 113 114When you've got your name sorted out and you're sure that your module is 115wanted and not currently available, it's time to start coding. 116 117=back 118 119=head2 Step-by-step: Making the module 120 121=over 3 122 123=item Start with F<module-starter> or F<h2xs> 124 125The F<module-starter> utility is distributed as part of the 126L<Module::Starter|Module::Starter> CPAN package. It creates a directory 127with stubs of all the necessary files to start a new module, according 128to recent "best practice" for module development, and is invoked from 129the command line, thus: 130 131 module-starter --module=Foo::Bar \ 132 --author="Your Name" --email=yourname@cpan.org 133 134If you do not wish to install the L<Module::Starter|Module::Starter> 135package from CPAN, F<h2xs> is an older tool, originally intended for the 136development of XS modules, which comes packaged with the Perl 137distribution. 138 139A typical invocation of L<h2xs|h2xs> for a pure Perl module is: 140 141 h2xs -AX --skip-exporter --use-new-tests -n Foo::Bar 142 143The C<-A> omits the Autoloader code, C<-X> omits XS elements, 144C<--skip-exporter> omits the Exporter code, C<--use-new-tests> sets up a 145modern testing environment, and C<-n> specifies the name of the module. 146 147=item Use L<strict|strict> and L<warnings|warnings> 148 149A module's code has to be warning and strict-clean, since you can't 150guarantee the conditions that it'll be used under. Besides, you wouldn't 151want to distribute code that wasn't warning or strict-clean anyway, 152right? 153 154=item Use L<Carp|Carp> 155 156The L<Carp|Carp> module allows you to present your error messages from 157the caller's perspective; this gives you a way to signal a problem with 158the caller and not your module. For instance, if you say this: 159 160 warn "No hostname given"; 161 162the user will see something like this: 163 164 No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm 165 line 123. 166 167which looks like your module is doing something wrong. Instead, you want 168to put the blame on the user, and say this: 169 170 No hostname given at bad_code, line 10. 171 172You do this by using L<Carp|Carp> and replacing your C<warn>s with 173C<carp>s. If you need to C<die>, say C<croak> instead. However, keep 174C<warn> and C<die> in place for your sanity checks - where it really is 175your module at fault. 176 177=item Use L<Exporter|Exporter> - wisely! 178 179L<Exporter|Exporter> gives you a standard way of exporting symbols and 180subroutines from your module into the caller's namespace. For instance, 181saying C<use Net::Acme qw(&frob)> would import the C<frob> subroutine. 182 183The package variable C<@EXPORT> will determine which symbols will get 184exported when the caller simply says C<use Net::Acme> - you will hardly 185ever want to put anything in there. C<@EXPORT_OK>, on the other hand, 186specifies which symbols you're willing to export. If you do want to 187export a bunch of symbols, use the C<%EXPORT_TAGS> and define a standard 188export set - look at L<Exporter> for more details. 189 190=item Use L<plain old documentation|perlpod> 191 192The work isn't over until the paperwork is done, and you're going to 193need to put in some time writing some documentation for your module. 194C<module-starter> or C<h2xs> will provide a stub for you to fill in; if 195you're not sure about the format, look at L<perlpod> for an 196introduction. Provide a good synopsis of how your module is used in 197code, a description, and then notes on the syntax and function of the 198individual subroutines or methods. Use Perl comments for developer notes 199and POD for end-user notes. 200 201=item Write tests 202 203You're encouraged to create self-tests for your module to ensure it's 204working as intended on the myriad platforms Perl supports; if you upload 205your module to CPAN, a host of testers will build your module and send 206you the results of the tests. Again, C<module-starter> and C<h2xs> 207provide a test framework which you can extend - you should do something 208more than just checking your module will compile. 209L<Test::Simple|Test::Simple> and L<Test::More|Test::More> are good 210places to start when writing a test suite. 211 212=item Write the README 213 214If you're uploading to CPAN, the automated gremlins will extract the 215README file and place that in your CPAN directory. It'll also appear in 216the main F<by-module> and F<by-category> directories if you make it onto 217the modules list. It's a good idea to put here what the module actually 218does in detail, and the user-visible changes since the last release. 219 220=back 221 222=head2 Step-by-step: Distributing your module 223 224=over 3 225 226=item Get a CPAN user ID 227 228Every developer publishing modules on CPAN needs a CPAN ID. Visit 229C<http://pause.perl.org/>, select "Request PAUSE Account", and wait for 230your request to be approved by the PAUSE administrators. 231 232=item C<perl Makefile.PL; make test; make dist> 233 234Once again, C<module-starter> or C<h2xs> has done all the work for you. 235They produce the standard C<Makefile.PL> you see when you download and 236install modules, and this produces a Makefile with a C<dist> target. 237 238Once you've ensured that your module passes its own tests - always a 239good thing to make sure - you can C<make dist>, and the Makefile will 240hopefully produce you a nice tarball of your module, ready for upload. 241 242=item Upload the tarball 243 244The email you got when you received your CPAN ID will tell you how to 245log in to PAUSE, the Perl Authors Upload SErver. From the menus there, 246you can upload your module to CPAN. 247 248=item Announce to the modules list 249 250Once uploaded, it'll sit unnoticed in your author directory. If you want 251it connected to the rest of the CPAN, you'll need to go to "Register 252Namespace" on PAUSE. Once registered, your module will appear in the 253by-module and by-category listings on CPAN. 254 255=item Announce to clpa 256 257If you have a burning desire to tell the world about your release, post 258an announcement to the moderated C<comp.lang.perl.announce> newsgroup. 259 260=item Fix bugs! 261 262Once you start accumulating users, they'll send you bug reports. If 263you're lucky, they'll even send you patches. Welcome to the joys of 264maintaining a software project... 265 266=back 267 268=head1 AUTHOR 269 270Simon Cozens, C<simon@cpan.org> 271 272Updated by Kirrily "Skud" Robert, C<skud@cpan.org> 273 274=head1 SEE ALSO 275 276L<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>, 277L<Carp>, L<Exporter>, L<perlpod>, L<Test::Simple>, L<Test::More> 278L<ExtUtils::MakeMaker>, L<Module::Build>, L<Module::Starter> 279http://www.cpan.org/ , Ken Williams' tutorial on building your own 280module at http://mathforum.org/~ken/perl_modules.html 281