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