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