| 1 |
.\" -*- mode: troff; coding: utf-8 -*- |
| 2 |
.\" Automatically generated by Pod::Man 5.0102 (Pod::Simple 3.45) |
| 3 |
.\" |
| 4 |
.\" Standard preamble: |
| 5 |
.\" ======================================================================== |
| 6 |
.de Sp \" Vertical space (when we can't use .PP) |
| 7 |
.if t .sp .5v |
| 8 |
.if n .sp |
| 9 |
.. |
| 10 |
.de Vb \" Begin verbatim text |
| 11 |
.ft CW |
| 12 |
.nf |
| 13 |
.ne \\$1 |
| 14 |
.. |
| 15 |
.de Ve \" End verbatim text |
| 16 |
.ft R |
| 17 |
.fi |
| 18 |
.. |
| 19 |
.\" \*(C` and \*(C' are quotes in nroff, nothing in troff, for use with C<>. |
| 20 |
.ie n \{\ |
| 21 |
. ds C` "" |
| 22 |
. ds C' "" |
| 23 |
'br\} |
| 24 |
.el\{\ |
| 25 |
. ds C` |
| 26 |
. ds C' |
| 27 |
'br\} |
| 28 |
.\" |
| 29 |
.\" Escape single quotes in literal strings from groff's Unicode transform. |
| 30 |
.ie \n(.g .ds Aq \(aq |
| 31 |
.el .ds Aq ' |
| 32 |
.\" |
| 33 |
.\" If the F register is >0, we'll generate index entries on stderr for |
| 34 |
.\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index |
| 35 |
.\" entries marked with X<> in POD. Of course, you'll have to process the |
| 36 |
.\" output yourself in some meaningful fashion. |
| 37 |
.\" |
| 38 |
.\" Avoid warning from groff about undefined register 'F'. |
| 39 |
.de IX |
| 40 |
.. |
| 41 |
.nr rF 0 |
| 42 |
.if \n(.g .if rF .nr rF 1 |
| 43 |
.if (\n(rF:(\n(.g==0)) \{\ |
| 44 |
. if \nF \{\ |
| 45 |
. de IX |
| 46 |
. tm Index:\\$1\t\\n%\t"\\$2" |
| 47 |
.. |
| 48 |
. if !\nF==2 \{\ |
| 49 |
. nr % 0 |
| 50 |
. nr F 2 |
| 51 |
. \} |
| 52 |
. \} |
| 53 |
.\} |
| 54 |
.rr rF |
| 55 |
.\" ======================================================================== |
| 56 |
.\" |
| 57 |
.IX Title "PERLNEWMOD 1" |
| 58 |
.TH PERLNEWMOD 1 2025-11-16 "perl v5.40.1" "Perl Programmers Reference Guide" |
| 59 |
.\" For nroff, turn off justification. Always turn off hyphenation; it makes |
| 60 |
.\" way too many mistakes in technical documents. |
| 61 |
.if n .ad l |
| 62 |
.nh |
| 63 |
.SH NAME |
| 64 |
perlnewmod \- preparing a new module for distribution |
| 65 |
.SH DESCRIPTION |
| 66 |
.IX Header "DESCRIPTION" |
| 67 |
This document gives you some suggestions about how to go about writing |
| 68 |
Perl modules, preparing them for distribution, and making them available |
| 69 |
via CPAN. |
| 70 |
.PP |
| 71 |
One of the things that makes Perl really powerful is the fact that Perl |
| 72 |
hackers tend to want to share the solutions to problems they've faced, |
| 73 |
so you and I don't have to battle with the same problem again. |
| 74 |
.PP |
| 75 |
The main way they do this is by abstracting the solution into a Perl |
| 76 |
module. If you don't know what one of these is, the rest of this |
| 77 |
document isn't going to be much use to you. You're also missing out on |
| 78 |
an awful lot of useful code; consider having a look at perlmod, |
| 79 |
perlmodlib and perlmodinstall before coming back here. |
| 80 |
.PP |
| 81 |
When you've found that there isn't a module available for what you're |
| 82 |
trying to do, and you've had to write the code yourself, consider |
| 83 |
packaging up the solution into a module and uploading it to CPAN so that |
| 84 |
others can benefit. |
| 85 |
.PP |
| 86 |
You should also take a look at perlmodstyle for best practices in |
| 87 |
making a module. |
| 88 |
.SS Warning |
| 89 |
.IX Subsection "Warning" |
| 90 |
We're going to primarily concentrate on Perl-only modules here, rather |
| 91 |
than XS modules. XS modules serve a rather different purpose, and |
| 92 |
you should consider different things before distributing them \- the |
| 93 |
popularity of the library you are gluing, the portability to other |
| 94 |
operating systems, and so on. However, the notes on preparing the Perl |
| 95 |
side of the module and packaging and distributing it will apply equally |
| 96 |
well to an XS module as a pure-Perl one. |
| 97 |
.SS "What should I make into a module?" |
| 98 |
.IX Subsection "What should I make into a module?" |
| 99 |
You should make a module out of any code that you think is going to be |
| 100 |
useful to others. Anything that's likely to fill a hole in the communal |
| 101 |
library and which someone else can slot directly into their program. Any |
| 102 |
part of your code which you can isolate and extract and plug into |
| 103 |
something else is a likely candidate. |
| 104 |
.PP |
| 105 |
Let's take an example. Suppose you're reading in data from a local |
| 106 |
format into a hash-of-hashes in Perl, turning that into a tree, walking |
| 107 |
the tree and then piping each node to an Acme Transmogrifier Server. |
| 108 |
.PP |
| 109 |
Now, quite a few people have the Acme Transmogrifier, and you've had to |
| 110 |
write something to talk the protocol from scratch \- you'd almost |
| 111 |
certainly want to make that into a module. The level at which you pitch |
| 112 |
it is up to you: you might want protocol-level modules analogous to |
| 113 |
Net::SMTP which then talk to higher level modules analogous |
| 114 |
to Mail::Send. The choice is yours, but you do want to get |
| 115 |
a module out for that server protocol. |
| 116 |
.PP |
| 117 |
Nobody else on the planet is going to talk your local data format, so we |
| 118 |
can ignore that. But what about the thing in the middle? Building tree |
| 119 |
structures from Perl variables and then traversing them is a nice, |
| 120 |
general problem, and if nobody's already written a module that does |
| 121 |
that, you might want to modularise that code too. |
| 122 |
.PP |
| 123 |
So hopefully you've now got a few ideas about what's good to modularise. |
| 124 |
Let's now see how it's done. |
| 125 |
.SS "Step-by-step: Preparing the ground" |
| 126 |
.IX Subsection "Step-by-step: Preparing the ground" |
| 127 |
Before we even start scraping out the code, there are a few things we'll |
| 128 |
want to do in advance. |
| 129 |
.IP "Look around" 3 |
| 130 |
.IX Item "Look around" |
| 131 |
Dig into a bunch of modules to see how they're written. I'd suggest |
| 132 |
starting with Text::Tabs, since it's in the standard |
| 133 |
library and is nice and simple, and then looking at something a little |
| 134 |
more complex like File::Copy. For object oriented |
| 135 |
code, WWW::Mechanize or the \f(CW\*(C`Email::*\*(C'\fR modules provide some good |
| 136 |
examples. |
| 137 |
.Sp |
| 138 |
These should give you an overall feel for how modules are laid out and |
| 139 |
written. |
| 140 |
.IP "Check it's new" 3 |
| 141 |
.IX Item "Check it's new" |
| 142 |
There are a lot of modules on CPAN, and it's easy to miss one that's |
| 143 |
similar to what you're planning on contributing. Have a good plough |
| 144 |
through <https://metacpan.org> and make sure you're not the one |
| 145 |
reinventing the wheel! |
| 146 |
.IP "Discuss the need" 3 |
| 147 |
.IX Item "Discuss the need" |
| 148 |
You might love it. You might feel that everyone else needs it. But there |
| 149 |
might not actually be any real demand for it out there. If you're unsure |
| 150 |
about the demand your module will have, consider asking the |
| 151 |
\&\f(CW\*(C`module\[email protected]\*(C'\fR mailing list (send an email to |
| 152 |
\&\f(CW\*(C`module\-authors\[email protected]\*(C'\fR to subscribe; see |
| 153 |
<https://lists.perl.org/list/module\-authors.html> for more information |
| 154 |
and a link to the archives). |
| 155 |
.IP "Choose a name" 3 |
| 156 |
.IX Item "Choose a name" |
| 157 |
Perl modules included on CPAN have a naming hierarchy you should try to |
| 158 |
fit in with. See perlmodlib for more details on how this works, and |
| 159 |
browse around CPAN and the modules list to get a feel of it. At the very |
| 160 |
least, remember this: modules should be title capitalised, (This::Thing) |
| 161 |
fit in with a category, and explain their purpose succinctly. |
| 162 |
.IP "Check again" 3 |
| 163 |
.IX Item "Check again" |
| 164 |
While you're doing that, make really sure you haven't missed a module |
| 165 |
similar to the one you're about to write. |
| 166 |
.Sp |
| 167 |
When you've got your name sorted out and you're sure that your module is |
| 168 |
wanted and not currently available, it's time to start coding. |
| 169 |
.SS "Step-by-step: Making the module" |
| 170 |
.IX Subsection "Step-by-step: Making the module" |
| 171 |
.IP "Start with \fImodule-starter\fR or \fIh2xs\fR" 3 |
| 172 |
.IX Item "Start with module-starter or h2xs" |
| 173 |
The \fImodule-starter\fR utility is distributed as part of the |
| 174 |
Module::Starter CPAN package. It creates a directory |
| 175 |
with stubs of all the necessary files to start a new module, according |
| 176 |
to recent "best practice" for module development, and is invoked from |
| 177 |
the command line, thus: |
| 178 |
.Sp |
| 179 |
.Vb 2 |
| 180 |
\& module\-starter \-\-module=Foo::Bar \e |
| 181 |
\& \-\-author="Your Name" \-\[email protected] |
| 182 |
.Ve |
| 183 |
.Sp |
| 184 |
If you do not wish to install the Module::Starter |
| 185 |
package from CPAN, \fIh2xs\fR is an older tool, originally intended for the |
| 186 |
development of XS modules, which comes packaged with the Perl |
| 187 |
distribution. |
| 188 |
.Sp |
| 189 |
A typical invocation of h2xs for a pure Perl module is: |
| 190 |
.Sp |
| 191 |
.Vb 1 |
| 192 |
\& h2xs \-AX \-\-skip\-exporter \-\-use\-new\-tests \-n Foo::Bar |
| 193 |
.Ve |
| 194 |
.Sp |
| 195 |
The \f(CW\*(C`\-A\*(C'\fR omits the Autoloader code, \f(CW\*(C`\-X\*(C'\fR omits XS elements, |
| 196 |
\&\f(CW\*(C`\-\-skip\-exporter\*(C'\fR omits the Exporter code, \f(CW\*(C`\-\-use\-new\-tests\*(C'\fR sets up a |
| 197 |
modern testing environment, and \f(CW\*(C`\-n\*(C'\fR specifies the name of the module. |
| 198 |
.IP "Use strict and warnings" 3 |
| 199 |
.IX Item "Use strict and warnings" |
| 200 |
A module's code has to be warning and strict-clean, since you can't |
| 201 |
guarantee the conditions that it'll be used under. Besides, you wouldn't |
| 202 |
want to distribute code that wasn't warning or strict-clean anyway, |
| 203 |
right? |
| 204 |
.IP "Use Carp" 3 |
| 205 |
.IX Item "Use Carp" |
| 206 |
The Carp module allows you to present your error messages from |
| 207 |
the caller's perspective; this gives you a way to signal a problem with |
| 208 |
the caller and not your module. For instance, if you say this: |
| 209 |
.Sp |
| 210 |
.Vb 1 |
| 211 |
\& warn "No hostname given"; |
| 212 |
.Ve |
| 213 |
.Sp |
| 214 |
the user will see something like this: |
| 215 |
.Sp |
| 216 |
.Vb 2 |
| 217 |
\& No hostname given at |
| 218 |
\& /usr/local/lib/perl5/site_perl/5.6.0/Net/Acme.pm line 123. |
| 219 |
.Ve |
| 220 |
.Sp |
| 221 |
which looks like your module is doing something wrong. Instead, you want |
| 222 |
to put the blame on the user, and say this: |
| 223 |
.Sp |
| 224 |
.Vb 1 |
| 225 |
\& No hostname given at bad_code, line 10. |
| 226 |
.Ve |
| 227 |
.Sp |
| 228 |
You do this by using Carp and replacing your \f(CW\*(C`warn\*(C'\fRs with |
| 229 |
\&\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 |
| 230 |
\&\f(CW\*(C`warn\*(C'\fR and \f(CW\*(C`die\*(C'\fR in place for your sanity checks \- where it really is |
| 231 |
your module at fault. |
| 232 |
.IP "Use Exporter \- wisely!" 3 |
| 233 |
.IX Item "Use Exporter - wisely!" |
| 234 |
Exporter gives you a standard way of exporting symbols and |
| 235 |
subroutines from your module into the caller's namespace. For instance, |
| 236 |
saying \f(CW\*(C`use Net::Acme qw(&frob)\*(C'\fR would import the \f(CW\*(C`frob\*(C'\fR subroutine. |
| 237 |
.Sp |
| 238 |
The package variable \f(CW@EXPORT\fR will determine which symbols will get |
| 239 |
exported when the caller simply says \f(CW\*(C`use Net::Acme\*(C'\fR \- you will hardly |
| 240 |
ever want to put anything in there. \f(CW@EXPORT_OK\fR, on the other hand, |
| 241 |
specifies which symbols you're willing to export. If you do want to |
| 242 |
export a bunch of symbols, use the \f(CW%EXPORT_TAGS\fR and define a standard |
| 243 |
export set \- look at Exporter for more details. |
| 244 |
.IP "Use plain old documentation" 3 |
| 245 |
.IX Item "Use plain old documentation" |
| 246 |
The work isn't over until the paperwork is done, and you're going to |
| 247 |
need to put in some time writing some documentation for your module. |
| 248 |
\&\f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR will provide a stub for you to fill in; if |
| 249 |
you're not sure about the format, look at perlpod for an |
| 250 |
introduction. Provide a good synopsis of how your module is used in |
| 251 |
code, a description, and then notes on the syntax and function of the |
| 252 |
individual subroutines or methods. Use Perl comments for developer notes |
| 253 |
and POD for end-user notes. |
| 254 |
.IP "Write tests" 3 |
| 255 |
.IX Item "Write tests" |
| 256 |
You're encouraged to create self-tests for your module to ensure it's |
| 257 |
working as intended on the myriad platforms Perl supports; if you upload |
| 258 |
your module to CPAN, a host of testers will build your module and send |
| 259 |
you the results of the tests. Again, \f(CW\*(C`module\-starter\*(C'\fR and \f(CW\*(C`h2xs\*(C'\fR |
| 260 |
provide a test framework which you can extend \- you should do something |
| 261 |
more than just checking your module will compile. |
| 262 |
Test::Simple and Test::More are good |
| 263 |
places to start when writing a test suite. |
| 264 |
.IP "Write the \fIREADME\fR" 3 |
| 265 |
.IX Item "Write the README" |
| 266 |
If you're uploading to CPAN, the automated gremlins will extract the |
| 267 |
README file and place that in your CPAN directory. It'll also appear in |
| 268 |
the main \fIby-module\fR and \fIby-category\fR directories if you make it onto |
| 269 |
the modules list. It's a good idea to put here what the module actually |
| 270 |
does in detail. |
| 271 |
.IP "Write \fIChanges\fR" 3 |
| 272 |
.IX Item "Write Changes" |
| 273 |
Add any user-visible changes since the last release to your \fIChanges\fR |
| 274 |
file. |
| 275 |
.SS "Step-by-step: Distributing your module" |
| 276 |
.IX Subsection "Step-by-step: Distributing your module" |
| 277 |
.IP "Get a CPAN user ID" 3 |
| 278 |
.IX Item "Get a CPAN user ID" |
| 279 |
Every developer publishing modules on CPAN needs a CPAN ID. Visit |
| 280 |
\&\f(CW\*(C`<https://pause.perl.org/>\*(C'\fR, select "Request PAUSE Account", and wait for |
| 281 |
your request to be approved by the PAUSE administrators. |
| 282 |
.IP "Make the tarball" 3 |
| 283 |
.IX Item "Make the tarball" |
| 284 |
Once again, \f(CW\*(C`module\-starter\*(C'\fR or \f(CW\*(C`h2xs\*(C'\fR has done all the work for you. |
| 285 |
They produce the standard \f(CW\*(C`Makefile.PL\*(C'\fR you see when you download and |
| 286 |
install modules, and this produces a Makefile with a \f(CW\*(C`dist\*(C'\fR target. |
| 287 |
.Sp |
| 288 |
.Vb 1 |
| 289 |
\& perl Makefile.PL && make test && make distcheck && make dist |
| 290 |
.Ve |
| 291 |
.Sp |
| 292 |
Once you've ensured that your module passes its own tests \- always a |
| 293 |
good thing to make sure \- you can \f(CW\*(C`make distcheck\*(C'\fR to make sure |
| 294 |
everything looks OK, followed by \f(CW\*(C`make dist\*(C'\fR, and the Makefile will |
| 295 |
hopefully produce you a nice tarball of your module, ready for upload. |
| 296 |
.IP "Upload the tarball" 3 |
| 297 |
.IX Item "Upload the tarball" |
| 298 |
The email you got when you received your CPAN ID will tell you how to |
| 299 |
log in to PAUSE, the Perl Authors Upload SErver. From the menus there, |
| 300 |
you can upload your module to CPAN. |
| 301 |
.Sp |
| 302 |
Alternatively you can use the \fIcpan-upload\fR script, part of the |
| 303 |
CPAN::Uploader distribution on CPAN. |
| 304 |
.IP "Fix bugs!" 3 |
| 305 |
.IX Item "Fix bugs!" |
| 306 |
Once you start accumulating users, they'll send you bug reports. If |
| 307 |
you're lucky, they'll even send you patches. Welcome to the joys of |
| 308 |
maintaining a software project... |
| 309 |
.SH AUTHOR |
| 310 |
.IX Header "AUTHOR" |
| 311 |
Simon Cozens, \f(CW\*(C`[email protected]\*(C'\fR |
| 312 |
.PP |
| 313 |
Updated by Kirrily "Skud" Robert, \f(CW\*(C`[email protected]\*(C'\fR |
| 314 |
.SH "SEE ALSO" |
| 315 |
.IX Header "SEE ALSO" |
| 316 |
perlmod, perlmodlib, perlmodinstall, h2xs, strict, |
| 317 |
Carp, Exporter, perlpod, Test::Simple, Test::More |
| 318 |
ExtUtils::MakeMaker, Module::Build, Module::Starter |
| 319 |
<https://www.cpan.org/> |