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