Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
11 KiB

  1. =head1 NAME
  2. perlnewmod - preparing a new module for distribution
  3. =head1 DESCRIPTION
  4. This document gives you some suggestions about how to go about writing
  5. Perl modules, preparing them for distribution, and making them available
  6. via CPAN.
  7. One of the things that makes Perl really powerful is the fact that Perl
  8. hackers tend to want to share the solutions to problems they've faced,
  9. so you and I don't have to battle with the same problem again.
  10. The main way they do this is by abstracting the solution into a Perl
  11. module. If you don't know what one of these is, the rest of this
  12. document isn't going to be much use to you. You're also missing out on
  13. an awful lot of useful code; consider having a look at L<perlmod>,
  14. L<perlmodlib> and L<perlmodinstall> before coming back here.
  15. When you've found that there isn't a module available for what you're
  16. trying to do, and you've had to write the code yourself, consider
  17. packaging up the solution into a module and uploading it to CPAN so that
  18. others can benefit.
  19. =head2 Warning
  20. We're going to primarily concentrate on Perl-only modules here, rather
  21. than XS modules. XS modules serve a rather different purpose, and
  22. you should consider different things before distributing them - the
  23. popularity of the library you are gluing, the portability to other
  24. operating systems, and so on. However, the notes on preparing the Perl
  25. side of the module and packaging and distributing it will apply equally
  26. well to an XS module as a pure-Perl one.
  27. =head2 What should I make into a module?
  28. You should make a module out of any code that you think is going to be
  29. useful to others. Anything that's likely to fill a hole in the communal
  30. library and which someone else can slot directly into their program. Any
  31. part of your code which you can isolate and extract and plug into
  32. something else is a likely candidate.
  33. Let's take an example. Suppose you're reading in data from a local
  34. format into a hash-of-hashes in Perl, turning that into a tree, walking
  35. the tree and then piping each node to an Acme Transmogrifier Server.
  36. Now, quite a few people have the Acme Transmogrifier, and you've had to
  37. write something to talk the protocol from scratch - you'd almost
  38. certainly want to make that into a module. The level at which you pitch
  39. it is up to you: you might want protocol-level modules analogous to
  40. L<Net::SMTP|Net::SMTP> which then talk to higher level modules analogous
  41. to L<Mail::Send|Mail::Send>. The choice is yours, but you do want to get
  42. a module out for that server protocol.
  43. Nobody else on the planet is going to talk your local data format, so we
  44. can ignore that. But what about the thing in the middle? Building tree
  45. structures from Perl variables and then traversing them is a nice,
  46. general problem, and if nobody's already written a module that does
  47. that, you might want to modularise that code too.
  48. So hopefully you've now got a few ideas about what's good to modularise.
  49. Let's now see how it's done.
  50. =head2 Step-by-step: Preparing the ground
  51. Before we even start scraping out the code, there are a few things we'll
  52. want to do in advance.
  53. =over 3
  54. =item Look around
  55. Dig into a bunch of modules to see how they're written. I'd suggest
  56. starting with L<Text::Tabs|Text::Tabs>, since it's in the standard
  57. library and is nice and simple, and then looking at something like
  58. L<Time::Zone|Time::Zone>, L<File::Copy|File::Copy> and then some of the
  59. C<Mail::*> modules if you're planning on writing object oriented code.
  60. These should give you an overall feel for how modules are laid out and
  61. written.
  62. =item Check it's new
  63. There are a lot of modules on CPAN, and it's easy to miss one that's
  64. similar to what you're planning on contributing. Have a good plough
  65. through the modules list and the F<by-module> directories, and make sure
  66. you're not the one reinventing the wheel!
  67. =item Discuss the need
  68. You might love it. You might feel that everyone else needs it. But there
  69. might not actually be any real demand for it out there. If you're unsure
  70. about the demand you're module will have, consider sending out feelers
  71. on the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the
  72. modules list at C<[email protected]>. Remember that this is a closed list
  73. with a very long turn-around time - be prepared to wait a good while for
  74. a response from them.
  75. =item Choose a name
  76. Perl modules included on CPAN have a naming hierarchy you should try to
  77. fit in with. See L<perlmodlib> for more details on how this works, and
  78. browse around CPAN and the modules list to get a feel of it. At the very
  79. least, remember this: modules should be title capitalised, (This::Thing)
  80. fit in with a category, and explain their purpose succinctly.
  81. =item Check again
  82. While you're doing that, make really sure you haven't missed a module
  83. similar to the one you're about to write.
  84. When you've got your name sorted out and you're sure that your module is
  85. wanted and not currently available, it's time to start coding.
  86. =back
  87. =head2 Step-by-step: Making the module
  88. =over 3
  89. =item Start with F<h2xs>
  90. Originally a utility to convert C header files into XS modules,
  91. L<h2xs|h2xs> has become a useful utility for churning out skeletons for
  92. Perl-only modules as well. If you don't want to use the
  93. L<Autoloader|Autoloader> which splits up big modules into smaller
  94. subroutine-sized chunks, you'll say something like this:
  95. h2xs -AX -n Net::Acme
  96. The C<-A> omits the Autoloader code, C<-X> omits XS elements, and C<-n>
  97. specifies the name of the module.
  98. =item Use L<strict|strict> and L<warnings|warnings>
  99. A module's code has to be warning and strict-clean, since you can't
  100. guarantee the conditions that it'll be used under. Besides, you wouldn't
  101. want to distribute code that wasn't warning or strict-clean anyway,
  102. right?
  103. =item Use L<Carp|Carp>
  104. The L<Carp|Carp> module allows you to present your error messages from
  105. the caller's perspective; this gives you a way to signal a problem with
  106. the caller and not your module. For instance, if you say this:
  107. warn "No hostname given";
  108. the user will see something like this:
  109. No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm
  110. line 123.
  111. which looks like your module is doing something wrong. Instead, you want
  112. to put the blame on the user, and say this:
  113. No hostname given at bad_code, line 10.
  114. You do this by using L<Carp|Carp> and replacing your C<warn>s with
  115. C<carp>s. If you need to C<die>, say C<croak> instead. However, keep
  116. C<warn> and C<die> in place for your sanity checks - where it really is
  117. your module at fault.
  118. =item Use L<Exporter|Exporter> - wisely!
  119. C<h2xs> provides stubs for L<Exporter|Exporter>, which gives you a
  120. standard way of exporting symbols and subroutines from your module into
  121. the caller's namespace. For instance, saying C<use Net::Acme qw(&frob)>
  122. would import the C<frob> subroutine.
  123. The package variable C<@EXPORT> will determine which symbols will get
  124. exported when the caller simply says C<use Net::Acme> - you will hardly
  125. ever want to put anything in there. C<@EXPORT_OK>, on the other hand,
  126. specifies which symbols you're willing to export. If you do want to
  127. export a bunch of symbols, use the C<%EXPORT_TAGS> and define a standard
  128. export set - look at L<Exporter> for more details.
  129. =item Use L<plain old documentation|perlpod>
  130. The work isn't over until the paperwork is done, and you're going to
  131. need to put in some time writing some documentation for your module.
  132. C<h2xs> will provide a stub for you to fill in; if you're not sure about
  133. the format, look at L<perlpod> for an introduction. Provide a good
  134. synopsis of how your module is used in code, a description, and then
  135. notes on the syntax and function of the individual subroutines or
  136. methods. Use Perl comments for developer notes and POD for end-user
  137. notes.
  138. =item Write tests
  139. You're encouraged to create self-tests for your module to ensure it's
  140. working as intended on the myriad platforms Perl supports; if you upload
  141. your module to CPAN, a host of testers will build your module and send
  142. you the results of the tests. Again, C<h2xs> provides a test framework
  143. which you can extend - you should do something more than just checking
  144. your module will compile.
  145. =item Write the README
  146. If you're uploading to CPAN, the automated gremlins will extract the
  147. README file and place that in your CPAN directory. It'll also appear in
  148. the main F<by-module> and F<by-category> directories if you make it onto
  149. the modules list. It's a good idea to put here what the module actually
  150. does in detail, and the user-visible changes since the last release.
  151. =back
  152. =head2 Step-by-step: Distributing your module
  153. =over 3
  154. =item Get a CPAN user ID
  155. Every developer publishing modules on CPAN needs a CPAN ID. See the
  156. instructions at C<http://www.cpan.org/modules/04pause.html> (or
  157. equivalent on your nearest mirror) to find out how to do this.
  158. =item C<perl Makefile.PL; make test; make dist>
  159. Once again, C<h2xs> has done all the work for you. It produces the
  160. standard C<Makefile.PL> you'll have seen when you downloaded and
  161. installs modules, and this produces a Makefile with a C<dist> target.
  162. Once you've ensured that your module passes its own tests - always a
  163. good thing to make sure - you can C<make dist>, and the Makefile will
  164. hopefully produce you a nice tarball of your module, ready for upload.
  165. =item Upload the tarball
  166. The email you got when you received your CPAN ID will tell you how to
  167. log in to PAUSE, the Perl Authors Upload SErver. From the menus there,
  168. you can upload your module to CPAN.
  169. =item Announce to the modules list
  170. Once uploaded, it'll sit unnoticed in your author directory. If you want
  171. it connected to the rest of the CPAN, you'll need to tell the modules
  172. list about it. The best way to do this is to email them a line in the
  173. style of the modules list, like this:
  174. Net::Acme bdpO Interface to Acme Frobnicator servers FOOBAR
  175. ^ ^^^^ ^ ^
  176. | |||| Module description Your ID
  177. | ||||
  178. | |||\- Interface: (O)OP, (r)eferences, (h)ybrid, (f)unctions
  179. | |||
  180. | ||\-- Language: (p)ure Perl, C(+)+, (h)ybrid, (C), (o)ther
  181. | ||
  182. Module |\--- Support: (d)eveloper, (m)ailing list, (u)senet, (n)one
  183. Name |
  184. \---- Maturity: (i)dea, (c)onstructions, (a)lpha, (b)eta,
  185. (R)eleased, (M)ature, (S)tandard
  186. plus a description of the module and why you think it should be
  187. included. If you hear nothing back, that means your module will
  188. probably appear on the modules list at the next update. Don't try
  189. subscribing to C<[email protected]>; it's not another mailing list. Just
  190. have patience.
  191. =item Announce to clpa
  192. If you have a burning desire to tell the world about your release, post
  193. an announcement to the moderated C<comp.lang.perl.announce> newsgroup.
  194. =item Fix bugs!
  195. Once you start accumulating users, they'll send you bug reports. If
  196. you're lucky, they'll even send you patches. Welcome to the joys of
  197. maintaining a software project...
  198. =back
  199. =head1 AUTHOR
  200. Simon Cozens, C<[email protected]>
  201. =head1 SEE ALSO
  202. L<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>,
  203. L<Carp>, L<Exporter>, L<perlpod>, L<Test>, L<ExtUtils::MakeMaker>,
  204. http://www.cpan.org/