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