This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
inline.h: Fix comment
[perl5.git] / pod / perlnewmod.pod
CommitLineData
2e1d04bc
JH
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
69520e41
E
26You should also take a look at L<perlmodstyle> for best practices in
27making a module.
28
2e1d04bc
JH
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
4e9dada0
KR
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, C<WWW::Mechanize> or the C<Email::*> modules provide some good
82examples.
2e1d04bc
JH
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
4e9dada0
KR
91through the L<http://search.cpan.org> and make sure you're not the one
92reinventing the wheel!
2e1d04bc
JH
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
43a36959 98about the demand your module will have, consider sending out feelers
2e1d04bc
JH
99on the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the
100modules list at C<modules@perl.org>. Remember that this is a closed list
101with a very long turn-around time - be prepared to wait a good while for
102a response from them.
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
4e9dada0 126=item Start with F<module-starter> or F<h2xs>
2e1d04bc 127
4e9dada0
KR
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:
2e1d04bc 133
4e9dada0
KR
134 module-starter --module=Foo::Bar \
135 --author="Your Name" --email=yourname@cpan.org
2e1d04bc 136
4e9dada0
KR
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.
2e1d04bc
JH
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 /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm
168 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
4e9dada0
KR
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.
2e1d04bc
JH
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.
4e9dada0
KR
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.
2e1d04bc
JH
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
4e9dada0
KR
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.
2e1d04bc
JH
214
215=item Write the 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, and the user-visible changes since the last release.
222
223=back
224
225=head2 Step-by-step: Distributing your module
226
227=over 3
228
229=item Get a CPAN user ID
230
4e9dada0
KR
231Every developer publishing modules on CPAN needs a CPAN ID. Visit
232C<http://pause.perl.org/>, select "Request PAUSE Account", and wait for
233your request to be approved by the PAUSE administrators.
2e1d04bc
JH
234
235=item C<perl Makefile.PL; make test; make dist>
236
4e9dada0 237Once again, C<module-starter> or C<h2xs> has done all the work for you.
85b35914
RGS
238They produce the standard C<Makefile.PL> you see when you download and
239install modules, and this produces a Makefile with a C<dist> target.
2e1d04bc
JH
240
241Once you've ensured that your module passes its own tests - always a
242good thing to make sure - you can C<make dist>, and the Makefile will
b1866b2d 243hopefully produce you a nice tarball of your module, ready for upload.
2e1d04bc
JH
244
245=item Upload the tarball
246
247The email you got when you received your CPAN ID will tell you how to
248log in to PAUSE, the Perl Authors Upload SErver. From the menus there,
249you can upload your module to CPAN.
250
251=item Announce to the modules list
252
253Once uploaded, it'll sit unnoticed in your author directory. If you want
4e9dada0
KR
254it connected to the rest of the CPAN, you'll need to go to "Register
255Namespace" on PAUSE. Once registered, your module will appear in the
256by-module and by-category listings on CPAN.
2e1d04bc
JH
257
258=item Announce to clpa
259
260If you have a burning desire to tell the world about your release, post
261an announcement to the moderated C<comp.lang.perl.announce> newsgroup.
262
263=item Fix bugs!
264
265Once you start accumulating users, they'll send you bug reports. If
266you're lucky, they'll even send you patches. Welcome to the joys of
267maintaining a software project...
268
269=back
270
271=head1 AUTHOR
272
273Simon Cozens, C<simon@cpan.org>
274
4e9dada0
KR
275Updated by Kirrily "Skud" Robert, C<skud@cpan.org>
276
2e1d04bc
JH
277=head1 SEE ALSO
278
279L<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>,
4e9dada0
KR
280L<Carp>, L<Exporter>, L<perlpod>, L<Test::Simple>, L<Test::More>
281L<ExtUtils::MakeMaker>, L<Module::Build>, L<Module::Starter>
e1020413 282http://www.cpan.org/ , Ken Williams's tutorial on building your own
9dc55af2 283module at http://mathforum.org/~ken/perl_modules.html