Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | .\" Automatically generated by Pod::Man v1.37, Pod::Parser v1.32 |
2 | .\" | |
3 | .\" Standard preamble: | |
4 | .\" ======================================================================== | |
5 | .de Sh \" Subsection heading | |
6 | .br | |
7 | .if t .Sp | |
8 | .ne 5 | |
9 | .PP | |
10 | \fB\\$1\fR | |
11 | .PP | |
12 | .. | |
13 | .de Sp \" Vertical space (when we can't use .PP) | |
14 | .if t .sp .5v | |
15 | .if n .sp | |
16 | .. | |
17 | .de Vb \" Begin verbatim text | |
18 | .ft CW | |
19 | .nf | |
20 | .ne \\$1 | |
21 | .. | |
22 | .de Ve \" End verbatim text | |
23 | .ft R | |
24 | .fi | |
25 | .. | |
26 | .\" Set up some character translations and predefined strings. \*(-- will | |
27 | .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left | |
28 | .\" double quote, and \*(R" will give a right double quote. | will give a | |
29 | .\" real vertical bar. \*(C+ will give a nicer C++. Capital omega is used to | |
30 | .\" do unbreakable dashes and therefore won't be available. \*(C` and \*(C' | |
31 | .\" expand to `' in nroff, nothing in troff, for use with C<>. | |
32 | .tr \(*W-|\(bv\*(Tr | |
33 | .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' | |
34 | .ie n \{\ | |
35 | . ds -- \(*W- | |
36 | . ds PI pi | |
37 | . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch | |
38 | . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch | |
39 | . ds L" "" | |
40 | . ds R" "" | |
41 | . ds C` "" | |
42 | . ds C' "" | |
43 | 'br\} | |
44 | .el\{\ | |
45 | . ds -- \|\(em\| | |
46 | . ds PI \(*p | |
47 | . ds L" `` | |
48 | . ds R" '' | |
49 | 'br\} | |
50 | .\" | |
51 | .\" If the F register is turned on, we'll generate index entries on stderr for | |
52 | .\" titles (.TH), headers (.SH), subsections (.Sh), items (.Ip), and index | |
53 | .\" entries marked with X<> in POD. Of course, you'll have to process the | |
54 | .\" output yourself in some meaningful fashion. | |
55 | .if \nF \{\ | |
56 | . de IX | |
57 | . tm Index:\\$1\t\\n%\t"\\$2" | |
58 | .. | |
59 | . nr % 0 | |
60 | . rr F | |
61 | .\} | |
62 | .\" | |
63 | .\" For nroff, turn off justification. Always turn off hyphenation; it makes | |
64 | .\" way too many mistakes in technical documents. | |
65 | .hy 0 | |
66 | .if n .na | |
67 | .\" | |
68 | .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). | |
69 | .\" Fear. Run. Save yourself. No user-serviceable parts. | |
70 | . \" fudge factors for nroff and troff | |
71 | .if n \{\ | |
72 | . ds #H 0 | |
73 | . ds #V .8m | |
74 | . ds #F .3m | |
75 | . ds #[ \f1 | |
76 | . ds #] \fP | |
77 | .\} | |
78 | .if t \{\ | |
79 | . ds #H ((1u-(\\\\n(.fu%2u))*.13m) | |
80 | . ds #V .6m | |
81 | . ds #F 0 | |
82 | . ds #[ \& | |
83 | . ds #] \& | |
84 | .\} | |
85 | . \" simple accents for nroff and troff | |
86 | .if n \{\ | |
87 | . ds ' \& | |
88 | . ds ` \& | |
89 | . ds ^ \& | |
90 | . ds , \& | |
91 | . ds ~ ~ | |
92 | . ds / | |
93 | .\} | |
94 | .if t \{\ | |
95 | . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" | |
96 | . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' | |
97 | . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' | |
98 | . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' | |
99 | . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' | |
100 | . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' | |
101 | .\} | |
102 | . \" troff and (daisy-wheel) nroff accents | |
103 | .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' | |
104 | .ds 8 \h'\*(#H'\(*b\h'-\*(#H' | |
105 | .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] | |
106 | .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' | |
107 | .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' | |
108 | .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] | |
109 | .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] | |
110 | .ds ae a\h'-(\w'a'u*4/10)'e | |
111 | .ds Ae A\h'-(\w'A'u*4/10)'E | |
112 | . \" corrections for vroff | |
113 | .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' | |
114 | .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' | |
115 | . \" for low resolution devices (crt and lpr) | |
116 | .if \n(.H>23 .if \n(.V>19 \ | |
117 | \{\ | |
118 | . ds : e | |
119 | . ds 8 ss | |
120 | . ds o a | |
121 | . ds d- d\h'-1'\(ga | |
122 | . ds D- D\h'-1'\(hy | |
123 | . ds th \o'bp' | |
124 | . ds Th \o'LP' | |
125 | . ds ae ae | |
126 | . ds Ae AE | |
127 | .\} | |
128 | .rm #[ #] #H #V #F C | |
129 | .\" ======================================================================== | |
130 | .\" | |
131 | .IX Title "PERLNEWMOD 1" | |
132 | .TH PERLNEWMOD 1 "2006-01-07" "perl v5.8.8" "Perl Programmers Reference Guide" | |
133 | .SH "NAME" | |
134 | perlnewmod \- preparing a new module for distribution | |
135 | .SH "DESCRIPTION" | |
136 | .IX Header "DESCRIPTION" | |
137 | This document gives you some suggestions about how to go about writing | |
138 | Perl modules, preparing them for distribution, and making them available | |
139 | via \s-1CPAN\s0. | |
140 | .PP | |
141 | One of the things that makes Perl really powerful is the fact that Perl | |
142 | hackers tend to want to share the solutions to problems they've faced, | |
143 | so you and I don't have to battle with the same problem again. | |
144 | .PP | |
145 | The main way they do this is by abstracting the solution into a Perl | |
146 | module. If you don't know what one of these is, the rest of this | |
147 | document isn't going to be much use to you. You're also missing out on | |
148 | an awful lot of useful code; consider having a look at perlmod, | |
149 | perlmodlib and perlmodinstall before coming back here. | |
150 | .PP | |
151 | When you've found that there isn't a module available for what you're | |
152 | trying to do, and you've had to write the code yourself, consider | |
153 | packaging up the solution into a module and uploading it to \s-1CPAN\s0 so that | |
154 | others can benefit. | |
155 | .Sh "Warning" | |
156 | .IX Subsection "Warning" | |
157 | We're going to primarily concentrate on Perl-only modules here, rather | |
158 | than \s-1XS\s0 modules. \s-1XS\s0 modules serve a rather different purpose, and | |
159 | you should consider different things before distributing them \- the | |
160 | popularity of the library you are gluing, the portability to other | |
161 | operating systems, and so on. However, the notes on preparing the Perl | |
162 | side of the module and packaging and distributing it will apply equally | |
163 | well to an \s-1XS\s0 module as a pure-Perl one. | |
164 | .Sh "What should I make into a module?" | |
165 | .IX Subsection "What should I make into a module?" | |
166 | You should make a module out of any code that you think is going to be | |
167 | useful to others. Anything that's likely to fill a hole in the communal | |
168 | library and which someone else can slot directly into their program. Any | |
169 | part of your code which you can isolate and extract and plug into | |
170 | something else is a likely candidate. | |
171 | .PP | |
172 | Let's take an example. Suppose you're reading in data from a local | |
173 | format into a hash-of-hashes in Perl, turning that into a tree, walking | |
174 | the tree and then piping each node to an Acme Transmogrifier Server. | |
175 | .PP | |
176 | Now, quite a few people have the Acme Transmogrifier, and you've had to | |
177 | write something to talk the protocol from scratch \- you'd almost | |
178 | certainly want to make that into a module. The level at which you pitch | |
179 | it is up to you: you might want protocol-level modules analogous to | |
180 | Net::SMTP which then talk to higher level modules analogous | |
181 | to Mail::Send. The choice is yours, but you do want to get | |
182 | a module out for that server protocol. | |
183 | .PP | |
184 | Nobody else on the planet is going to talk your local data format, so we | |
185 | can ignore that. But what about the thing in the middle? Building tree | |
186 | structures from Perl variables and then traversing them is a nice, | |
187 | general problem, and if nobody's already written a module that does | |
188 | that, you might want to modularise that code too. | |
189 | .PP | |
190 | So hopefully you've now got a few ideas about what's good to modularise. | |
191 | Let's now see how it's done. | |
192 | .Sh "Step\-by\-step: Preparing the ground" | |
193 | .IX Subsection "Step-by-step: Preparing the ground" | |
194 | Before we even start scraping out the code, there are a few things we'll | |
195 | want to do in advance. | |
196 | .IP "Look around" 3 | |
197 | .IX Item "Look around" | |
198 | Dig into a bunch of modules to see how they're written. I'd suggest | |
199 | starting with Text::Tabs, since it's in the standard | |
200 | library and is nice and simple, and then looking at something a little | |
201 | more complex like File::Copy. For object oriented | |
202 | code, \f(CW\*(C`WWW::Mechanize\*(C'\fR or the \f(CW\*(C`Email::*\*(C'\fR modules provide some good | |
203 | examples. | |
204 | .Sp | |
205 | These should give you an overall feel for how modules are laid out and | |
206 | written. | |
207 | .IP "Check it's new" 3 | |
208 | .IX Item "Check it's new" | |
209 | There are a lot of modules on \s-1CPAN\s0, and it's easy to miss one that's | |
210 | similar to what you're planning on contributing. Have a good plough | |
211 | through the <http://search.cpan.org> and make sure you're not the one | |
212 | reinventing the wheel! | |
213 | .IP "Discuss the need" 3 | |
214 | .IX Item "Discuss the need" | |
215 | You might love it. You might feel that everyone else needs it. But there | |
216 | might not actually be any real demand for it out there. If you're unsure | |
217 | about the demand your module will have, consider sending out feelers | |
218 | on the \f(CW\*(C`comp.lang.perl.modules\*(C'\fR newsgroup, or as a last resort, ask the | |
219 | modules list at \f(CW\*(C`modules@perl.org\*(C'\fR. Remember that this is a closed list | |
220 | with a very long turn-around time \- be prepared to wait a good while for | |
221 | a response from them. | |
222 | .IP "Choose a name" 3 | |
223 | .IX Item "Choose a name" | |
224 | Perl modules included on \s-1CPAN\s0 have a naming hierarchy you should try to | |
225 | fit in with. See perlmodlib for more details on how this works, and | |
226 | browse around \s-1CPAN\s0 and the modules list to get a feel of it. At the very | |
227 | least, remember this: modules should be title capitalised, (This::Thing) | |
228 | fit in with a category, and explain their purpose succinctly. | |
229 | .IP "Check again" 3 | |
230 | .IX Item "Check again" | |
231 | While you're doing that, make really sure you haven't missed a module | |
232 | similar to the one you're about to write. | |
233 | .Sp | |
234 | When you've got your name sorted out and you're sure that your module is | |
235 | wanted and not currently available, it's time to start coding. | |
236 | .Sh "Step\-by\-step: Making the module" | |
237 | .IX Subsection "Step-by-step: Making the module" | |
238 | .IP "Start with \fImodule-starter\fR or \fIh2xs\fR" 3 | |
239 | .IX Item "Start with module-starter or h2xs" | |
240 | The \fImodule-starter\fR utility is distributed as part of the | |
241 | Module::Starter \s-1CPAN\s0 package. It creates a directory | |
242 | with stubs of all the necessary files to start a new module, according | |
243 | to recent \*(L"best practice\*(R" for module development, and is invoked from | |
244 | the command line, thus: | |
245 | .Sp | |
246 | .Vb 2 | |
247 | \& module-starter --module=Foo::Bar \e | |
248 | \& --author="Your Name" --email=yourname@cpan.org | |
249 | .Ve | |
250 | .Sp | |
251 | If you do not wish to install the Module::Starter | |
252 | package from \s-1CPAN\s0, \fIh2xs\fR is an older tool, originally intended for the | |
253 | development of \s-1XS\s0 modules, which comes packaged with the Perl | |
254 | distribution. | |
255 | .Sp | |
256 | A typical invocation of h2xs for a pure Perl module is: | |
257 | .Sp | |
258 | .Vb 1 | |
259 | \& h2xs -AX --skip-exporter --use-new-tests -n Foo::Bar | |
260 | .Ve | |
261 | .Sp | |
262 | The \f(CW\*(C`\-A\*(C'\fR omits the Autoloader code, \f(CW\*(C`\-X\*(C'\fR omits \s-1XS\s0 elements, | |
263 | \&\f(CW\*(C`\-\-skip\-exporter\*(C'\fR omits the Exporter code, \f(CW\*(C`\-\-use\-new\-tests\*(C'\fR sets up a | |
264 | modern testing environment, and \f(CW\*(C`\-n\*(C'\fR specifies the name of the module. | |
265 | .IP "Use strict and warnings" 3 | |
266 | .IX Item "Use strict and warnings" | |
267 | A module's code has to be warning and strict\-clean, since you can't | |
268 | guarantee the conditions that it'll be used under. Besides, you wouldn't | |
269 | want to distribute code that wasn't warning or strict-clean anyway, | |
270 | right? | |
271 | .IP "Use Carp" 3 | |
272 | .IX Item "Use Carp" | |
273 | The Carp module allows you to present your error messages from | |
274 | the caller's perspective; this gives you a way to signal a problem with | |
275 | the caller and not your module. For instance, if you say this: | |
276 | .Sp | |
277 | .Vb 1 | |
278 | \& warn "No hostname given"; | |
279 | .Ve | |
280 | .Sp | |
281 | the user will see something like this: | |
282 | .Sp | |
283 | .Vb 2 | |
284 | \& No hostname given at /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm | |
285 | \& line 123. | |
286 | .Ve | |
287 | .Sp | |
288 | which looks like your module is doing something wrong. Instead, you want | |
289 | to put the blame on the user, and say this: | |
290 | .Sp | |
291 | .Vb 1 | |
292 | \& No hostname given at bad_code, line 10. | |
293 | .Ve | |
294 | .Sp | |
295 | You do this by using Carp and replacing your \f(CW\*(C`warn\*(C'\fRs with | |
296 | \&\f(CW\*(C`carp\*(C'\fRs. If you need to \f(CW\*(C`die\*(C'\fR, say \f(CW\*(C`croak\*(C'\fR instead. However, keep | |
297 | \&\f(CW\*(C`warn\*(C'\fR and \f(CW\*(C`die\*(C'\fR in place for your sanity checks \- where it really is | |
298 | your module at fault. | |
299 | .IP "Use Exporter \- wisely!" 3 | |
300 | .IX Item "Use Exporter - wisely!" | |
301 | Exporter gives you a standard way of exporting symbols and | |
302 | subroutines from your module into the caller's namespace. For instance, | |
303 | saying \f(CW\*(C`use Net::Acme qw(&frob)\*(C'\fR would import the \f(CW\*(C`frob\*(C'\fR subroutine. | |
304 | .Sp | |
305 | The package variable \f(CW@EXPORT\fR will determine which symbols will get | |
306 | exported when the caller simply says \f(CW\*(C`use Net::Acme\*(C'\fR \- you will hardly | |
307 | ever want to put anything in there. \f(CW@EXPORT_OK\fR, on the other hand, | |
308 | specifies which symbols you're willing to export. If you do want to | |
309 | export a bunch of symbols, use the \f(CW%EXPORT_TAGS\fR and define a standard | |
310 | export set \- look at Exporter for more details. | |
311 | .IP "Use plain old documentation" 3 | |
312 | .IX Item "Use plain old documentation" | |
313 | The work isn't over until the paperwork is done, and you're going to | |
314 | need to put in some time writing some documentation for your module. | |
315 | \&\f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR will provide a stub for you to fill in; if | |
316 | you're not sure about the format, look at perlpod for an | |
317 | introduction. Provide a good synopsis of how your module is used in | |
318 | code, a description, and then notes on the syntax and function of the | |
319 | individual subroutines or methods. Use Perl comments for developer notes | |
320 | and \s-1POD\s0 for end-user notes. | |
321 | .IP "Write tests" 3 | |
322 | .IX Item "Write tests" | |
323 | You're encouraged to create self-tests for your module to ensure it's | |
324 | working as intended on the myriad platforms Perl supports; if you upload | |
325 | your module to \s-1CPAN\s0, a host of testers will build your module and send | |
326 | you the results of the tests. Again, \f(CW\*(C`module\-starter\*(C'\fR and \f(CW\*(C`h2xs\*(C'\fR | |
327 | provide a test framework which you can extend \- you should do something | |
328 | more than just checking your module will compile. | |
329 | Test::Simple and Test::More are good | |
330 | places to start when writing a test suite. | |
331 | .IP "Write the \s-1README\s0" 3 | |
332 | .IX Item "Write the README" | |
333 | If you're uploading to \s-1CPAN\s0, the automated gremlins will extract the | |
334 | \&\s-1README\s0 file and place that in your \s-1CPAN\s0 directory. It'll also appear in | |
335 | the main \fIby-module\fR and \fIby-category\fR directories if you make it onto | |
336 | the modules list. It's a good idea to put here what the module actually | |
337 | does in detail, and the user-visible changes since the last release. | |
338 | .Sh "Step\-by\-step: Distributing your module" | |
339 | .IX Subsection "Step-by-step: Distributing your module" | |
340 | .IP "Get a \s-1CPAN\s0 user \s-1ID\s0" 3 | |
341 | .IX Item "Get a CPAN user ID" | |
342 | Every developer publishing modules on \s-1CPAN\s0 needs a \s-1CPAN\s0 \s-1ID\s0. Visit | |
343 | \&\f(CW\*(C`http://pause.perl.org/\*(C'\fR, select \*(L"Request \s-1PAUSE\s0 Account\*(R", and wait for | |
344 | your request to be approved by the \s-1PAUSE\s0 administrators. | |
345 | .ie n .IP """perl Makefile.PL; make test; make dist""" 3 | |
346 | .el .IP "\f(CWperl Makefile.PL; make test; make dist\fR" 3 | |
347 | .IX Item "perl Makefile.PL; make test; make dist" | |
348 | Once again, \f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR has done all the work for you. | |
349 | They produce the standard \f(CW\*(C`Makefile.PL\*(C'\fR you see when you download and | |
350 | install modules, and this produces a Makefile with a \f(CW\*(C`dist\*(C'\fR target. | |
351 | .Sp | |
352 | Once you've ensured that your module passes its own tests \- always a | |
353 | good thing to make sure \- you can \f(CW\*(C`make dist\*(C'\fR, and the Makefile will | |
354 | hopefully produce you a nice tarball of your module, ready for upload. | |
355 | .IP "Upload the tarball" 3 | |
356 | .IX Item "Upload the tarball" | |
357 | The email you got when you received your \s-1CPAN\s0 \s-1ID\s0 will tell you how to | |
358 | log in to \s-1PAUSE\s0, the Perl Authors Upload SErver. From the menus there, | |
359 | you can upload your module to \s-1CPAN\s0. | |
360 | .IP "Announce to the modules list" 3 | |
361 | .IX Item "Announce to the modules list" | |
362 | Once uploaded, it'll sit unnoticed in your author directory. If you want | |
363 | it connected to the rest of the \s-1CPAN\s0, you'll need to go to \*(L"Register | |
364 | Namespace\*(R" on \s-1PAUSE\s0. Once registered, your module will appear in the | |
365 | by-module and by-category listings on \s-1CPAN\s0. | |
366 | .IP "Announce to clpa" 3 | |
367 | .IX Item "Announce to clpa" | |
368 | If you have a burning desire to tell the world about your release, post | |
369 | an announcement to the moderated \f(CW\*(C`comp.lang.perl.announce\*(C'\fR newsgroup. | |
370 | .IP "Fix bugs!" 3 | |
371 | .IX Item "Fix bugs!" | |
372 | Once you start accumulating users, they'll send you bug reports. If | |
373 | you're lucky, they'll even send you patches. Welcome to the joys of | |
374 | maintaining a software project... | |
375 | .SH "AUTHOR" | |
376 | .IX Header "AUTHOR" | |
377 | Simon Cozens, \f(CW\*(C`simon@cpan.org\*(C'\fR | |
378 | .PP | |
379 | Updated by Kirrily \*(L"Skud\*(R" Robert, \f(CW\*(C`skud@cpan.org\*(C'\fR | |
380 | .SH "SEE ALSO" | |
381 | .IX Header "SEE ALSO" | |
382 | perlmod, perlmodlib, perlmodinstall, h2xs, strict, | |
383 | Carp, Exporter, perlpod, Test::Simple, Test::More | |
384 | ExtUtils::MakeMaker, Module::Build, Module::Starter | |
385 | http://www.cpan.org/ , Ken Williams' tutorial on building your own | |
386 | module at http://mathforum.org/~ken/perl_modules.html |