Commit | Line | Data |
---|---|---|
a0d0e21e LW |
1 | =head1 NAME |
2 | ||
3 | perlmod - Perl modules (packages) | |
4 | ||
5 | =head1 DESCRIPTION | |
6 | ||
7 | =head2 Packages | |
8 | ||
748a9306 | 9 | Perl provides a mechanism for alternative namespaces to protect packages |
d0c42abe | 10 | from stomping on each other's variables. In fact, apart from certain |
cb1a09d0 AD |
11 | magical variables, there's really no such thing as a global variable in |
12 | Perl. The package statement declares the compilation unit as being in the | |
13 | given namespace. The scope of the package declaration is from the | |
14 | declaration itself through the end of the enclosing block (the same scope | |
15 | as the local() operator). All further unqualified dynamic identifiers | |
5f05dabc | 16 | will be in this namespace. A package statement affects only dynamic |
cb1a09d0 AD |
17 | variables--including those you've used local() on--but I<not> lexical |
18 | variables created with my(). Typically it would be the first declaration | |
19 | in a file to be included by the C<require> or C<use> operator. You can | |
5f05dabc | 20 | switch into a package in more than one place; it influences merely which |
a0d0e21e LW |
21 | symbol table is used by the compiler for the rest of that block. You can |
22 | refer to variables and filehandles in other packages by prefixing the | |
23 | identifier with the package name and a double colon: | |
24 | C<$Package::Variable>. If the package name is null, the C<main> package | |
d0c42abe | 25 | is assumed. That is, C<$::sail> is equivalent to C<$main::sail>. |
a0d0e21e LW |
26 | |
27 | (The old package delimiter was a single quote, but double colon | |
28 | is now the preferred delimiter, in part because it's more readable | |
29 | to humans, and in part because it's more readable to B<emacs> macros. | |
30 | It also makes C++ programmers feel like they know what's going on.) | |
31 | ||
32 | Packages may be nested inside other packages: C<$OUTER::INNER::var>. This | |
33 | implies nothing about the order of name lookups, however. All symbols | |
34 | are either local to the current package, or must be fully qualified | |
35 | from the outer package name down. For instance, there is nowhere | |
36 | within package C<OUTER> that C<$INNER::var> refers to C<$OUTER::INNER::var>. | |
37 | It would treat package C<INNER> as a totally separate global package. | |
38 | ||
39 | Only identifiers starting with letters (or underscore) are stored in a | |
cb1a09d0 AD |
40 | package's symbol table. All other symbols are kept in package C<main>, |
41 | including all of the punctuation variables like $_. In addition, the | |
5f05dabc | 42 | identifiers STDIN, STDOUT, STDERR, ARGV, ARGVOUT, ENV, INC, and SIG are |
cb1a09d0 AD |
43 | forced to be in package C<main>, even when used for other purposes than |
44 | their built-in one. Note also that, if you have a package called C<m>, | |
5f05dabc | 45 | C<s>, or C<y>, then you can't use the qualified form of an identifier |
cb1a09d0 AD |
46 | because it will be interpreted instead as a pattern match, a substitution, |
47 | or a translation. | |
a0d0e21e LW |
48 | |
49 | (Variables beginning with underscore used to be forced into package | |
50 | main, but we decided it was more useful for package writers to be able | |
cb1a09d0 AD |
51 | to use leading underscore to indicate private variables and method names. |
52 | $_ is still global though.) | |
a0d0e21e LW |
53 | |
54 | Eval()ed strings are compiled in the package in which the eval() was | |
55 | compiled. (Assignments to C<$SIG{}>, however, assume the signal | |
748a9306 | 56 | handler specified is in the C<main> package. Qualify the signal handler |
a0d0e21e LW |
57 | name if you wish to have a signal handler in a package.) For an |
58 | example, examine F<perldb.pl> in the Perl library. It initially switches | |
59 | to the C<DB> package so that the debugger doesn't interfere with variables | |
60 | in the script you are trying to debug. At various points, however, it | |
61 | temporarily switches back to the C<main> package to evaluate various | |
62 | expressions in the context of the C<main> package (or wherever you came | |
63 | from). See L<perldebug>. | |
64 | ||
5f05dabc | 65 | See L<perlsub> for other scoping issues related to my() and local(), |
cb1a09d0 AD |
66 | or L<perlref> regarding closures. |
67 | ||
a0d0e21e LW |
68 | =head2 Symbol Tables |
69 | ||
aa689395 | 70 | The symbol table for a package happens to be stored in the hash of that |
71 | name with two colons appended. The main symbol table's name is thus | |
72 | C<%main::>, or C<%::> for short. Likewise symbol table for the nested | |
73 | package mentioned earlier is named C<%OUTER::INNER::>. | |
74 | ||
75 | The value in each entry of the hash is what you are referring to when you | |
76 | use the C<*name> typeglob notation. In fact, the following have the same | |
77 | effect, though the first is more efficient because it does the symbol | |
78 | table lookups at compile time: | |
a0d0e21e LW |
79 | |
80 | local(*main::foo) = *main::bar; local($main::{'foo'}) = | |
81 | $main::{'bar'}; | |
82 | ||
83 | You can use this to print out all the variables in a package, for | |
84 | instance. Here is F<dumpvar.pl> from the Perl library: | |
85 | ||
86 | package dumpvar; | |
87 | sub main::dumpvar { | |
88 | ($package) = @_; | |
89 | local(*stab) = eval("*${package}::"); | |
90 | while (($key,$val) = each(%stab)) { | |
91 | local(*entry) = $val; | |
92 | if (defined $entry) { | |
93 | print "\$$key = '$entry'\n"; | |
94 | } | |
95 | ||
96 | if (defined @entry) { | |
97 | print "\@$key = (\n"; | |
98 | foreach $num ($[ .. $#entry) { | |
99 | print " $num\t'",$entry[$num],"'\n"; | |
100 | } | |
101 | print ")\n"; | |
102 | } | |
103 | ||
104 | if ($key ne "${package}::" && defined %entry) { | |
105 | print "\%$key = (\n"; | |
106 | foreach $key (sort keys(%entry)) { | |
107 | print " $key\t'",$entry{$key},"'\n"; | |
108 | } | |
109 | print ")\n"; | |
110 | } | |
111 | } | |
112 | } | |
113 | ||
114 | Note that even though the subroutine is compiled in package C<dumpvar>, | |
115 | the name of the subroutine is qualified so that its name is inserted | |
116 | into package C<main>. | |
117 | ||
cb1a09d0 | 118 | Assignment to a typeglob performs an aliasing operation, i.e., |
a0d0e21e LW |
119 | |
120 | *dick = *richard; | |
121 | ||
5f05dabc | 122 | causes variables, subroutines, and file handles accessible via the |
d0c42abe | 123 | identifier C<richard> to also be accessible via the identifier C<dick>. If |
5f05dabc | 124 | you want to alias only a particular variable or subroutine, you can |
a0d0e21e LW |
125 | assign a reference instead: |
126 | ||
127 | *dick = \$richard; | |
128 | ||
129 | makes $richard and $dick the same variable, but leaves | |
130 | @richard and @dick as separate arrays. Tricky, eh? | |
131 | ||
cb1a09d0 AD |
132 | This mechanism may be used to pass and return cheap references |
133 | into or from subroutines if you won't want to copy the whole | |
134 | thing. | |
135 | ||
136 | %some_hash = (); | |
137 | *some_hash = fn( \%another_hash ); | |
138 | sub fn { | |
139 | local *hashsym = shift; | |
140 | # now use %hashsym normally, and you | |
141 | # will affect the caller's %another_hash | |
142 | my %nhash = (); # do what you want | |
5f05dabc | 143 | return \%nhash; |
cb1a09d0 AD |
144 | } |
145 | ||
5f05dabc | 146 | On return, the reference will overwrite the hash slot in the |
cb1a09d0 | 147 | symbol table specified by the *some_hash typeglob. This |
c36e9b62 | 148 | is a somewhat tricky way of passing around references cheaply |
cb1a09d0 AD |
149 | when you won't want to have to remember to dereference variables |
150 | explicitly. | |
151 | ||
152 | Another use of symbol tables is for making "constant" scalars. | |
153 | ||
154 | *PI = \3.14159265358979; | |
155 | ||
156 | Now you cannot alter $PI, which is probably a good thing all in all. | |
157 | ||
55497cff | 158 | You can say C<*foo{PACKAGE}> and C<*foo{NAME}> to find out what name and |
159 | package the *foo symbol table entry comes from. This may be useful | |
160 | in a subroutine which is passed typeglobs as arguments | |
161 | ||
162 | sub identify_typeglob { | |
163 | my $glob = shift; | |
164 | print 'You gave me ', *{$glob}{PACKAGE}, '::', *{$glob}{NAME}, "\n"; | |
165 | } | |
166 | identify_typeglob *foo; | |
167 | identify_typeglob *bar::baz; | |
168 | ||
169 | This prints | |
170 | ||
171 | You gave me main::foo | |
172 | You gave me bar::baz | |
173 | ||
174 | The *foo{THING} notation can also be used to obtain references to the | |
175 | individual elements of *foo, see L<perlref>. | |
176 | ||
a0d0e21e LW |
177 | =head2 Package Constructors and Destructors |
178 | ||
179 | There are two special subroutine definitions that function as package | |
180 | constructors and destructors. These are the C<BEGIN> and C<END> | |
181 | routines. The C<sub> is optional for these routines. | |
182 | ||
183 | A C<BEGIN> subroutine is executed as soon as possible, that is, the | |
184 | moment it is completely defined, even before the rest of the containing | |
185 | file is parsed. You may have multiple C<BEGIN> blocks within a | |
186 | file--they will execute in order of definition. Because a C<BEGIN> | |
187 | block executes immediately, it can pull in definitions of subroutines | |
188 | and such from other files in time to be visible to the rest of the | |
189 | file. | |
190 | ||
191 | An C<END> subroutine is executed as late as possible, that is, when the | |
192 | interpreter is being exited, even if it is exiting as a result of a | |
193 | die() function. (But not if it's is being blown out of the water by a | |
194 | signal--you have to trap that yourself (if you can).) You may have | |
748a9306 | 195 | multiple C<END> blocks within a file--they will execute in reverse |
a0d0e21e LW |
196 | order of definition; that is: last in, first out (LIFO). |
197 | ||
c36e9b62 | 198 | Inside an C<END> subroutine C<$?> contains the value that the script is |
199 | going to pass to C<exit()>. You can modify C<$?> to change the exit | |
5f05dabc | 200 | value of the script. Beware of changing C<$?> by accident (e.g.,, by |
c36e9b62 | 201 | running something via C<system>). |
202 | ||
a0d0e21e LW |
203 | Note that when you use the B<-n> and B<-p> switches to Perl, C<BEGIN> |
204 | and C<END> work just as they do in B<awk>, as a degenerate case. | |
205 | ||
206 | =head2 Perl Classes | |
207 | ||
4633a7c4 | 208 | There is no special class syntax in Perl, but a package may function |
a0d0e21e LW |
209 | as a class if it provides subroutines that function as methods. Such a |
210 | package may also derive some of its methods from another class package | |
5f05dabc | 211 | by listing the other package name in its @ISA array. |
4633a7c4 LW |
212 | |
213 | For more on this, see L<perlobj>. | |
a0d0e21e LW |
214 | |
215 | =head2 Perl Modules | |
216 | ||
c07a80fd | 217 | A module is just a package that is defined in a library file of |
a0d0e21e LW |
218 | the same name, and is designed to be reusable. It may do this by |
219 | providing a mechanism for exporting some of its symbols into the symbol | |
220 | table of any package using it. Or it may function as a class | |
221 | definition and make its semantics available implicitly through method | |
222 | calls on the class and its objects, without explicit exportation of any | |
223 | symbols. Or it can do a little of both. | |
224 | ||
4633a7c4 LW |
225 | For example, to start a normal module called Fred, create |
226 | a file called Fred.pm and put this at the start of it: | |
227 | ||
5f05dabc | 228 | package Fred; |
229 | use strict; | |
230 | use Exporter (); | |
231 | use vars qw(@ISA @EXPORT @EXPORT_OK); | |
4633a7c4 | 232 | @ISA = qw(Exporter); |
5f05dabc | 233 | @EXPORT = qw(&func1 &func2); |
234 | @EXPORT_OK = qw($sally @listabob %harry &func3); | |
235 | use vars qw($sally @listabob %harry); | |
4633a7c4 LW |
236 | |
237 | Then go on to declare and use your variables in functions | |
238 | without any qualifications. | |
5f05dabc | 239 | See L<Exporter> and the I<Perl Modules File> for details on |
4633a7c4 LW |
240 | mechanics and style issues in module creation. |
241 | ||
242 | Perl modules are included into your program by saying | |
a0d0e21e LW |
243 | |
244 | use Module; | |
245 | ||
246 | or | |
247 | ||
248 | use Module LIST; | |
249 | ||
250 | This is exactly equivalent to | |
251 | ||
252 | BEGIN { require "Module.pm"; import Module; } | |
253 | ||
254 | or | |
255 | ||
256 | BEGIN { require "Module.pm"; import Module LIST; } | |
257 | ||
cb1a09d0 AD |
258 | As a special case |
259 | ||
260 | use Module (); | |
261 | ||
262 | is exactly equivalent to | |
263 | ||
264 | BEGIN { require "Module.pm"; } | |
265 | ||
a0d0e21e LW |
266 | All Perl module files have the extension F<.pm>. C<use> assumes this so |
267 | that you don't have to spell out "F<Module.pm>" in quotes. This also | |
268 | helps to differentiate new modules from old F<.pl> and F<.ph> files. | |
269 | Module names are also capitalized unless they're functioning as pragmas, | |
270 | "Pragmas" are in effect compiler directives, and are sometimes called | |
271 | "pragmatic modules" (or even "pragmata" if you're a classicist). | |
272 | ||
273 | Because the C<use> statement implies a C<BEGIN> block, the importation | |
274 | of semantics happens at the moment the C<use> statement is compiled, | |
275 | before the rest of the file is compiled. This is how it is able | |
276 | to function as a pragma mechanism, and also how modules are able to | |
277 | declare subroutines that are then visible as list operators for | |
278 | the rest of the current file. This will not work if you use C<require> | |
cb1a09d0 | 279 | instead of C<use>. With require you can get into this problem: |
a0d0e21e LW |
280 | |
281 | require Cwd; # make Cwd:: accessible | |
282 | $here = Cwd::getcwd(); | |
283 | ||
5f05dabc | 284 | use Cwd; # import names from Cwd:: |
a0d0e21e LW |
285 | $here = getcwd(); |
286 | ||
287 | require Cwd; # make Cwd:: accessible | |
288 | $here = getcwd(); # oops! no main::getcwd() | |
289 | ||
cb1a09d0 AD |
290 | In general C<use Module ();> is recommended over C<require Module;>. |
291 | ||
a0d0e21e LW |
292 | Perl packages may be nested inside other package names, so we can have |
293 | package names containing C<::>. But if we used that package name | |
294 | directly as a filename it would makes for unwieldy or impossible | |
295 | filenames on some systems. Therefore, if a module's name is, say, | |
296 | C<Text::Soundex>, then its definition is actually found in the library | |
297 | file F<Text/Soundex.pm>. | |
298 | ||
299 | Perl modules always have a F<.pm> file, but there may also be dynamically | |
300 | linked executables or autoloaded subroutine definitions associated with | |
301 | the module. If so, these will be entirely transparent to the user of | |
302 | the module. It is the responsibility of the F<.pm> file to load (or | |
303 | arrange to autoload) any additional functionality. The POSIX module | |
304 | happens to do both dynamic loading and autoloading, but the user can | |
5f05dabc | 305 | say just C<use POSIX> to get it all. |
a0d0e21e | 306 | |
8e07c86e | 307 | For more information on writing extension modules, see L<perlxs> |
a0d0e21e LW |
308 | and L<perlguts>. |
309 | ||
310 | =head1 NOTE | |
311 | ||
312 | Perl does not enforce private and public parts of its modules as you may | |
313 | have been used to in other languages like C++, Ada, or Modula-17. Perl | |
314 | doesn't have an infatuation with enforced privacy. It would prefer | |
315 | that you stayed out of its living room because you weren't invited, not | |
316 | because it has a shotgun. | |
317 | ||
318 | The module and its user have a contract, part of which is common law, | |
319 | and part of which is "written". Part of the common law contract is | |
320 | that a module doesn't pollute any namespace it wasn't asked to. The | |
5f05dabc | 321 | written contract for the module (A.K.A. documentation) may make other |
a0d0e21e LW |
322 | provisions. But then you know when you C<use RedefineTheWorld> that |
323 | you're redefining the world and willing to take the consequences. | |
324 | ||
325 | =head1 THE PERL MODULE LIBRARY | |
326 | ||
5f05dabc | 327 | A number of modules are included the Perl distribution. These are |
328 | described below, and all end in F<.pm>. You may also discover files in | |
a0d0e21e | 329 | the library directory that end in either F<.pl> or F<.ph>. These are old |
748a9306 | 330 | libraries supplied so that old programs that use them still run. The |
a0d0e21e LW |
331 | F<.pl> files will all eventually be converted into standard modules, and |
332 | the F<.ph> files made by B<h2ph> will probably end up as extension modules | |
333 | made by B<h2xs>. (Some F<.ph> values may already be available through the | |
334 | POSIX module.) The B<pl2pm> file in the distribution may help in your | |
4fdae800 | 335 | conversion, but it's just a mechanical process and therefore far from |
336 | bulletproof. | |
a0d0e21e LW |
337 | |
338 | =head2 Pragmatic Modules | |
339 | ||
340 | They work somewhat like pragmas in that they tend to affect the compilation of | |
5f05dabc | 341 | your program, and thus will usually work well only when used within a |
55497cff | 342 | C<use>, or C<no>. Most of these are locally scoped, so an inner BLOCK |
343 | may countermand any of these by saying: | |
a0d0e21e LW |
344 | |
345 | no integer; | |
346 | no strict 'refs'; | |
347 | ||
348 | which lasts until the end of that BLOCK. | |
349 | ||
5f05dabc | 350 | Unlike the pragmas that effect the C<$^H> hints variable, the C<use |
55497cff | 351 | vars> and C<use subs> declarations are not BLOCK-scoped. They allow |
352 | you to pre-declare a variables or subroutines within a particular | |
4fdae800 | 353 | I<file> rather than just a block. Such declarations are effective |
55497cff | 354 | for the entire file for which they were declared. You cannot rescind |
355 | them with C<no vars> or C<no subs>. | |
356 | ||
357 | The following pragmas are defined (and have their own documentation). | |
a0d0e21e LW |
358 | |
359 | =over 12 | |
360 | ||
5f05dabc | 361 | =item blib |
362 | ||
363 | manipulate @INC at compile time to use MakeMaker's uninstalled version | |
364 | of a package | |
365 | ||
cb1a09d0 | 366 | =item diagnostics |
4633a7c4 | 367 | |
55497cff | 368 | force verbose warning diagnostics |
4633a7c4 | 369 | |
cb1a09d0 | 370 | =item integer |
a0d0e21e | 371 | |
55497cff | 372 | compute arithmetic in integer instead of double |
a0d0e21e | 373 | |
cb1a09d0 | 374 | =item less |
a0d0e21e | 375 | |
55497cff | 376 | request less of something from the compiler |
377 | ||
378 | =item lib | |
379 | ||
380 | manipulate @INC at compile time | |
a0d0e21e | 381 | |
5f05dabc | 382 | =item locale |
383 | ||
71be2cbc | 384 | use or ignore current locale for built-in operations (see L<perllocale>) |
5f05dabc | 385 | |
d0c42abe | 386 | =item ops |
387 | ||
5f05dabc | 388 | restrict named opcodes when compiling or running Perl code |
d0c42abe | 389 | |
cb1a09d0 AD |
390 | =item overload |
391 | ||
5f05dabc | 392 | overload basic Perl operations |
cb1a09d0 AD |
393 | |
394 | =item sigtrap | |
a0d0e21e | 395 | |
55497cff | 396 | enable simple signal handling |
a0d0e21e | 397 | |
cb1a09d0 | 398 | =item strict |
a0d0e21e | 399 | |
55497cff | 400 | restrict unsafe constructs |
a0d0e21e | 401 | |
cb1a09d0 | 402 | =item subs |
a0d0e21e | 403 | |
5f05dabc | 404 | pre-declare sub names |
a0d0e21e | 405 | |
ff0cee69 | 406 | =item vmsish |
407 | ||
408 | adopt certain VMS-specific behaviors | |
409 | ||
d0c42abe | 410 | =item vars |
411 | ||
5f05dabc | 412 | pre-declare global variable names |
d0c42abe | 413 | |
a0d0e21e LW |
414 | =back |
415 | ||
416 | =head2 Standard Modules | |
417 | ||
4633a7c4 | 418 | Standard, bundled modules are all expected to behave in a well-defined |
a0d0e21e | 419 | manner with respect to namespace pollution because they use the |
4633a7c4 | 420 | Exporter module. See their own documentation for details. |
a0d0e21e | 421 | |
cb1a09d0 AD |
422 | =over 12 |
423 | ||
424 | =item AnyDBM_File | |
425 | ||
426 | provide framework for multiple DBMs | |
427 | ||
428 | =item AutoLoader | |
429 | ||
430 | load functions only on demand | |
431 | ||
432 | =item AutoSplit | |
433 | ||
434 | split a package for autoloading | |
435 | ||
436 | =item Benchmark | |
437 | ||
438 | benchmark running times of code | |
439 | ||
71be2cbc | 440 | =item CPAN |
441 | ||
442 | interface to Comprehensive Perl Archive Network | |
443 | ||
444 | =item CPAN::FirstTime | |
445 | ||
446 | create a CPAN configuration file | |
447 | ||
448 | =item CPAN::Nox | |
449 | ||
450 | run CPAN while avoiding compiled extensions | |
451 | ||
cb1a09d0 AD |
452 | =item Carp |
453 | ||
454 | warn of errors (from perspective of caller) | |
455 | ||
5f05dabc | 456 | =item Class::Template |
457 | ||
458 | struct/member template builder | |
459 | ||
cb1a09d0 AD |
460 | =item Config |
461 | ||
55497cff | 462 | access Perl configuration information |
cb1a09d0 AD |
463 | |
464 | =item Cwd | |
465 | ||
466 | get pathname of current working directory | |
467 | ||
468 | =item DB_File | |
469 | ||
55497cff | 470 | access to Berkeley DB |
cb1a09d0 AD |
471 | |
472 | =item Devel::SelfStubber | |
473 | ||
474 | generate stubs for a SelfLoading module | |
475 | ||
55497cff | 476 | =item DirHandle |
477 | ||
478 | supply object methods for directory handles | |
479 | ||
cb1a09d0 AD |
480 | =item DynaLoader |
481 | ||
5f05dabc | 482 | dynamically load C libraries into Perl code |
cb1a09d0 AD |
483 | |
484 | =item English | |
485 | ||
55497cff | 486 | use nice English (or awk) names for ugly punctuation variables |
cb1a09d0 AD |
487 | |
488 | =item Env | |
489 | ||
55497cff | 490 | import environment variables |
cb1a09d0 AD |
491 | |
492 | =item Exporter | |
493 | ||
55497cff | 494 | implements default import method for modules |
495 | ||
496 | =item ExtUtils::Embed | |
497 | ||
5f05dabc | 498 | utilities for embedding Perl in C/C++ applications |
55497cff | 499 | |
500 | =item ExtUtils::Install | |
501 | ||
502 | install files from here to there | |
cb1a09d0 AD |
503 | |
504 | =item ExtUtils::Liblist | |
505 | ||
506 | determine libraries to use and how to use them | |
507 | ||
5f05dabc | 508 | =item ExtUtils::MM_OS2 |
509 | ||
510 | methods to override UN*X behaviour in ExtUtils::MakeMaker | |
511 | ||
512 | =item ExtUtils::MM_Unix | |
513 | ||
514 | methods used by ExtUtils::MakeMaker | |
515 | ||
516 | =item ExtUtils::MM_VMS | |
517 | ||
518 | methods to override UN*X behaviour in ExtUtils::MakeMaker | |
519 | ||
cb1a09d0 AD |
520 | =item ExtUtils::MakeMaker |
521 | ||
522 | create an extension Makefile | |
523 | ||
524 | =item ExtUtils::Manifest | |
525 | ||
526 | utilities to write and check a MANIFEST file | |
527 | ||
528 | =item ExtUtils::Mkbootstrap | |
529 | ||
530 | make a bootstrap file for use by DynaLoader | |
531 | ||
55497cff | 532 | =item ExtUtils::Mksymlists |
533 | ||
534 | write linker options files for dynamic extension | |
535 | ||
5f05dabc | 536 | =item ExtUtils::testlib |
55497cff | 537 | |
5f05dabc | 538 | add blib/* directories to @INC |
55497cff | 539 | |
cb1a09d0 AD |
540 | =item Fcntl |
541 | ||
542 | load the C Fcntl.h defines | |
543 | ||
544 | =item File::Basename | |
545 | ||
5f05dabc | 546 | split a pathname into pieces |
55497cff | 547 | |
cb1a09d0 AD |
548 | =item File::CheckTree |
549 | ||
550 | run many filetest checks on a tree | |
551 | ||
5f05dabc | 552 | =item File::Compare |
553 | ||
554 | compare files or filehandles | |
555 | ||
55497cff | 556 | =item File::Copy |
557 | ||
5f05dabc | 558 | copy files or filehandles |
55497cff | 559 | |
cb1a09d0 AD |
560 | =item File::Find |
561 | ||
562 | traverse a file tree | |
563 | ||
cb1a09d0 AD |
564 | =item File::Path |
565 | ||
566 | create or remove a series of directories | |
567 | ||
5f05dabc | 568 | =item File::stat |
569 | ||
570 | by-name interface to Perl's built-in stat() functions | |
571 | ||
572 | =item FileCache | |
573 | ||
574 | keep more files open than the system permits | |
575 | ||
576 | =item FileHandle | |
577 | ||
578 | supply object methods for filehandles | |
579 | ||
55497cff | 580 | =item FindBin |
581 | ||
582 | locate directory of original perl script | |
583 | ||
584 | =item GDBM_File | |
585 | ||
5f05dabc | 586 | access to the gdbm library |
55497cff | 587 | |
cb1a09d0 AD |
588 | =item Getopt::Long |
589 | ||
55497cff | 590 | extended processing of command line options |
cb1a09d0 AD |
591 | |
592 | =item Getopt::Std | |
593 | ||
55497cff | 594 | process single-character switches with switch clustering |
cb1a09d0 AD |
595 | |
596 | =item I18N::Collate | |
597 | ||
598 | compare 8-bit scalar data according to the current locale | |
599 | ||
55497cff | 600 | =item IO |
601 | ||
602 | load various IO modules | |
603 | ||
604 | =item IO::File | |
605 | ||
606 | supply object methods for filehandles | |
607 | ||
608 | =item IO::Handle | |
609 | ||
610 | supply object methods for I/O handles | |
611 | ||
612 | =item IO::Pipe | |
613 | ||
614 | supply object methods for pipes | |
615 | ||
616 | =item IO::Seekable | |
617 | ||
618 | supply seek based methods for I/O objects | |
619 | ||
620 | =item IO::Select | |
621 | ||
622 | OO interface to the select system call | |
623 | ||
624 | =item IO::Socket | |
625 | ||
626 | object interface to socket communications | |
627 | ||
cb1a09d0 AD |
628 | =item IPC::Open2 |
629 | ||
55497cff | 630 | open a process for both reading and writing |
cb1a09d0 AD |
631 | |
632 | =item IPC::Open3 | |
633 | ||
634 | open a process for reading, writing, and error handling | |
635 | ||
55497cff | 636 | =item Math::BigFloat |
637 | ||
638 | arbitrary length float math package | |
639 | ||
640 | =item Math::BigInt | |
641 | ||
642 | arbitrary size integer math package | |
643 | ||
644 | =item Math::Complex | |
645 | ||
646 | complex numbers and associated mathematical functions | |
647 | ||
648 | =item NDBM_File | |
649 | ||
650 | tied access to ndbm files | |
651 | ||
7e1af8bc | 652 | =item Net::Ping |
653 | ||
654 | Hello, anybody home? | |
655 | ||
5f05dabc | 656 | =item Net::hostent |
657 | ||
658 | by-name interface to Perl's built-in gethost*() functions | |
659 | ||
660 | =item Net::netent | |
661 | ||
662 | by-name interface to Perl's built-in getnet*() functions | |
663 | ||
664 | =item Net::protoent | |
665 | ||
666 | by-name interface to Perl's built-in getproto*() functions | |
667 | ||
668 | =item Net::servent | |
669 | ||
670 | by-name interface to Perl's built-in getserv*() functions | |
671 | ||
55497cff | 672 | =item Opcode |
673 | ||
5f05dabc | 674 | disable named opcodes when compiling or running perl code |
55497cff | 675 | |
676 | =item Pod::Text | |
677 | ||
678 | convert POD data to formatted ASCII text | |
679 | ||
cb1a09d0 AD |
680 | =item POSIX |
681 | ||
5f05dabc | 682 | interface to IEEE Standard 1003.1 |
55497cff | 683 | |
684 | =item SDBM_File | |
685 | ||
686 | tied access to sdbm files | |
687 | ||
5f05dabc | 688 | =item Safe |
689 | ||
690 | compile and execute code in restricted compartments | |
691 | ||
55497cff | 692 | =item Search::Dict |
693 | ||
694 | search for key in dictionary file | |
695 | ||
696 | =item SelectSaver | |
697 | ||
698 | save and restore selected file handle | |
cb1a09d0 AD |
699 | |
700 | =item SelfLoader | |
701 | ||
702 | load functions only on demand | |
703 | ||
55497cff | 704 | =item Shell |
a2927560 | 705 | |
55497cff | 706 | run shell commands transparently within perl |
a2927560 | 707 | |
cb1a09d0 AD |
708 | =item Socket |
709 | ||
710 | load the C socket.h defines and structure manipulators | |
711 | ||
55497cff | 712 | =item Symbol |
713 | ||
714 | manipulate Perl symbols and their names | |
715 | ||
716 | =item Sys::Hostname | |
717 | ||
718 | try every conceivable way to get hostname | |
719 | ||
720 | =item Sys::Syslog | |
721 | ||
722 | interface to the UNIX syslog(3) calls | |
723 | ||
724 | =item Term::Cap | |
725 | ||
5f05dabc | 726 | termcap interface |
55497cff | 727 | |
728 | =item Term::Complete | |
729 | ||
730 | word completion module | |
731 | ||
732 | =item Term::ReadLine | |
733 | ||
5f05dabc | 734 | interface to various C<readline> packages |
55497cff | 735 | |
cb1a09d0 AD |
736 | =item Test::Harness |
737 | ||
738 | run perl standard test scripts with statistics | |
739 | ||
740 | =item Text::Abbrev | |
741 | ||
c36e9b62 | 742 | create an abbreviation table from a list |
cb1a09d0 | 743 | |
55497cff | 744 | =item Text::ParseWords |
745 | ||
746 | parse text into an array of tokens | |
747 | ||
748 | =item Text::Soundex | |
749 | ||
5f05dabc | 750 | implementation of the Soundex Algorithm as described by Knuth |
55497cff | 751 | |
752 | =item Text::Tabs | |
753 | ||
754 | expand and unexpand tabs per the unix expand(1) and unexpand(1) | |
755 | ||
756 | =item Text::Wrap | |
757 | ||
758 | line wrapping to form simple paragraphs | |
759 | ||
760 | =item Tie::Hash | |
761 | ||
762 | base class definitions for tied hashes | |
763 | ||
5f05dabc | 764 | =item Tie::RefHash |
765 | ||
766 | base class definitions for tied hashes with references as keys | |
767 | ||
55497cff | 768 | =item Tie::Scalar |
769 | ||
770 | base class definitions for tied scalars | |
771 | ||
772 | =item Tie::SubstrHash | |
773 | ||
774 | fixed-table-size, fixed-key-length hashing | |
775 | ||
776 | =item Time::Local | |
777 | ||
778 | efficiently compute time from local and GMT time | |
779 | ||
5f05dabc | 780 | =item Time::gmtime |
781 | ||
782 | by-name interface to Perl's built-in gmtime() function | |
783 | ||
784 | =item Time::localtime | |
785 | ||
786 | by-name interface to Perl's built-in localtime() function | |
787 | ||
788 | =item Time::tm | |
789 | ||
790 | internal object used by Time::gmtime and Time::localtime | |
791 | ||
55497cff | 792 | =item UNIVERSAL |
793 | ||
794 | base class for ALL classes (blessed references) | |
795 | ||
5f05dabc | 796 | =item User::grent |
797 | ||
798 | by-name interface to Perl's built-in getgr*() functions | |
799 | ||
800 | =item User::pwent | |
801 | ||
802 | by-name interface to Perl's built-in getpw*() functions | |
803 | ||
cb1a09d0 AD |
804 | =back |
805 | ||
806 | To find out I<all> the modules installed on your system, including | |
807 | those without documentation or outside the standard release, do this: | |
a0d0e21e | 808 | |
4633a7c4 | 809 | find `perl -e 'print "@INC"'` -name '*.pm' -print |
a0d0e21e | 810 | |
4633a7c4 LW |
811 | They should all have their own documentation installed and accessible via |
812 | your system man(1) command. If that fails, try the I<perldoc> program. | |
a0d0e21e | 813 | |
4633a7c4 | 814 | =head2 Extension Modules |
a0d0e21e | 815 | |
4633a7c4 LW |
816 | Extension modules are written in C (or a mix of Perl and C) and get |
817 | dynamically loaded into Perl if and when you need them. Supported | |
818 | extension modules include the Socket, Fcntl, and POSIX modules. | |
a0d0e21e | 819 | |
cb1a09d0 | 820 | Many popular C extension modules do not come bundled (at least, not |
5f05dabc | 821 | completely) due to their sizes, volatility, or simply lack of time for |
cb1a09d0 AD |
822 | adequate testing and configuration across the multitude of platforms on |
823 | which Perl was beta-tested. You are encouraged to look for them in | |
824 | archie(1L), the Perl FAQ or Meta-FAQ, the WWW page, and even with their | |
825 | authors before randomly posting asking for their present condition and | |
826 | disposition. | |
a0d0e21e | 827 | |
cb1a09d0 | 828 | =head1 CPAN |
a0d0e21e | 829 | |
4633a7c4 | 830 | CPAN stands for the Comprehensive Perl Archive Network. This is a globally |
5f05dabc | 831 | replicated collection of all known Perl materials, including hundreds |
c36e9b62 | 832 | of unbundled modules. Here are the major categories of modules: |
a0d0e21e | 833 | |
4633a7c4 | 834 | =over |
a0d0e21e | 835 | |
4633a7c4 | 836 | =item * |
5f05dabc | 837 | Language Extensions and Documentation Tools |
a0d0e21e | 838 | |
4633a7c4 LW |
839 | =item * |
840 | Development Support | |
a0d0e21e | 841 | |
4633a7c4 LW |
842 | =item * |
843 | Operating System Interfaces | |
a0d0e21e | 844 | |
4633a7c4 LW |
845 | =item * |
846 | Networking, Device Control (modems) and InterProcess Communication | |
a0d0e21e | 847 | |
4633a7c4 LW |
848 | =item * |
849 | Data Types and Data Type Utilities | |
a0d0e21e | 850 | |
4633a7c4 LW |
851 | =item * |
852 | Database Interfaces | |
a0d0e21e | 853 | |
4633a7c4 LW |
854 | =item * |
855 | User Interfaces | |
a0d0e21e | 856 | |
4633a7c4 LW |
857 | =item * |
858 | Interfaces to / Emulations of Other Programming Languages | |
a0d0e21e | 859 | |
4633a7c4 LW |
860 | =item * |
861 | File Names, File Systems and File Locking (see also File Handles) | |
a0d0e21e | 862 | |
4633a7c4 | 863 | =item * |
5f05dabc | 864 | String Processing, Language Text Processing, Parsing, and Searching |
a0d0e21e | 865 | |
4633a7c4 | 866 | =item * |
5f05dabc | 867 | Option, Argument, Parameter, and Configuration File Processing |
a0d0e21e | 868 | |
4633a7c4 LW |
869 | =item * |
870 | Internationalization and Locale | |
a0d0e21e | 871 | |
4633a7c4 | 872 | =item * |
5f05dabc | 873 | Authentication, Security, and Encryption |
a0d0e21e | 874 | |
4633a7c4 LW |
875 | =item * |
876 | World Wide Web, HTML, HTTP, CGI, MIME | |
a0d0e21e | 877 | |
4633a7c4 LW |
878 | =item * |
879 | Server and Daemon Utilities | |
a0d0e21e | 880 | |
4633a7c4 LW |
881 | =item * |
882 | Archiving and Compression | |
a0d0e21e | 883 | |
4633a7c4 | 884 | =item * |
5f05dabc | 885 | Images, Pixmap and Bitmap Manipulation, Drawing, and Graphing |
a0d0e21e | 886 | |
4633a7c4 LW |
887 | =item * |
888 | Mail and Usenet News | |
a0d0e21e | 889 | |
4633a7c4 LW |
890 | =item * |
891 | Control Flow Utilities (callbacks and exceptions etc) | |
a0d0e21e | 892 | |
4633a7c4 LW |
893 | =item * |
894 | File Handle and Input/Output Stream Utilities | |
a0d0e21e | 895 | |
4633a7c4 LW |
896 | =item * |
897 | Miscellaneous Modules | |
a0d0e21e | 898 | |
4633a7c4 | 899 | =back |
a0d0e21e | 900 | |
d0c42abe | 901 | The registered CPAN sites as of this writing include the following. |
4633a7c4 | 902 | You should try to choose one close to you: |
a0d0e21e | 903 | |
4633a7c4 | 904 | =over |
a0d0e21e | 905 | |
4633a7c4 LW |
906 | =item * |
907 | ftp://ftp.sterling.com/programming/languages/perl/ | |
a0d0e21e | 908 | |
4633a7c4 LW |
909 | =item * |
910 | ftp://ftp.sedl.org/pub/mirrors/CPAN/ | |
a0d0e21e | 911 | |
4633a7c4 LW |
912 | =item * |
913 | ftp://ftp.uoknor.edu/mirrors/CPAN/ | |
a0d0e21e | 914 | |
4633a7c4 LW |
915 | =item * |
916 | ftp://ftp.delphi.com/pub/mirrors/packages/perl/CPAN/ | |
a0d0e21e | 917 | |
4633a7c4 LW |
918 | =item * |
919 | ftp://uiarchive.cso.uiuc.edu/pub/lang/perl/CPAN/ | |
a0d0e21e | 920 | |
4633a7c4 LW |
921 | =item * |
922 | ftp://ftp.cis.ufl.edu/pub/perl/CPAN/ | |
a0d0e21e | 923 | |
4633a7c4 LW |
924 | =item * |
925 | ftp://ftp.switch.ch/mirror/CPAN/ | |
a0d0e21e | 926 | |
4633a7c4 LW |
927 | =item * |
928 | ftp://ftp.sunet.se/pub/lang/perl/CPAN/ | |
a0d0e21e | 929 | |
4633a7c4 LW |
930 | =item * |
931 | ftp://ftp.ci.uminho.pt/pub/lang/perl/ | |
a0d0e21e | 932 | |
4633a7c4 LW |
933 | =item * |
934 | ftp://ftp.cs.ruu.nl/pub/PERL/CPAN/ | |
a0d0e21e | 935 | |
4633a7c4 LW |
936 | =item * |
937 | ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/ | |
a0d0e21e | 938 | |
4633a7c4 LW |
939 | =item * |
940 | ftp://ftp.rz.ruhr-uni-bochum.de/pub/programming/languages/perl/CPAN/ | |
a0d0e21e | 941 | |
4633a7c4 LW |
942 | =item * |
943 | ftp://ftp.leo.org/pub/comp/programming/languages/perl/CPAN/ | |
a0d0e21e | 944 | |
4633a7c4 LW |
945 | =item * |
946 | ftp://ftp.pasteur.fr/pub/computing/unix/perl/CPAN/ | |
a0d0e21e | 947 | |
4633a7c4 LW |
948 | =item * |
949 | ftp://ftp.ibp.fr/pub/perl/CPAN/ | |
a0d0e21e | 950 | |
4633a7c4 LW |
951 | =item * |
952 | ftp://ftp.funet.fi/pub/languages/perl/CPAN/ | |
a0d0e21e | 953 | |
4633a7c4 LW |
954 | =item * |
955 | ftp://ftp.tekotago.ac.nz/pub/perl/CPAN/ | |
a0d0e21e | 956 | |
4633a7c4 LW |
957 | =item * |
958 | ftp://ftp.mame.mu.oz.au/pub/perl/CPAN/ | |
a0d0e21e | 959 | |
4633a7c4 LW |
960 | =item * |
961 | ftp://coombs.anu.edu.au/pub/perl/ | |
a0d0e21e | 962 | |
4633a7c4 LW |
963 | =item * |
964 | ftp://dongpo.math.ncu.edu.tw/perl/CPAN/ | |
a0d0e21e | 965 | |
4633a7c4 LW |
966 | =item * |
967 | ftp://ftp.lab.kdd.co.jp/lang/perl/CPAN/ | |
a0d0e21e | 968 | |
4633a7c4 LW |
969 | =item * |
970 | ftp://ftp.is.co.za/programming/perl/CPAN/ | |
a0d0e21e LW |
971 | |
972 | =back | |
4633a7c4 | 973 | |
5f05dabc | 974 | For an up-to-date listing of CPAN sites, |
d0c42abe | 975 | see F<http://www.perl.com/perl/CPAN> or F<ftp://ftp.perl.com/perl/>. |
cb1a09d0 | 976 | |
5f05dabc | 977 | =head1 Modules: Creation, Use, and Abuse |
cb1a09d0 AD |
978 | |
979 | (The following section is borrowed directly from Tim Bunce's modules | |
980 | file, available at your nearest CPAN site.) | |
981 | ||
5f05dabc | 982 | Perl implements a class using a package, but the presence of a |
cb1a09d0 AD |
983 | package doesn't imply the presence of a class. A package is just a |
984 | namespace. A class is a package that provides subroutines that can be | |
985 | used as methods. A method is just a subroutine that expects, as its | |
986 | first argument, either the name of a package (for "static" methods), | |
987 | or a reference to something (for "virtual" methods). | |
988 | ||
989 | A module is a file that (by convention) provides a class of the same | |
990 | name (sans the .pm), plus an import method in that class that can be | |
991 | called to fetch exported symbols. This module may implement some of | |
992 | its methods by loading dynamic C or C++ objects, but that should be | |
993 | totally transparent to the user of the module. Likewise, the module | |
994 | might set up an AUTOLOAD function to slurp in subroutine definitions on | |
995 | demand, but this is also transparent. Only the .pm file is required to | |
996 | exist. | |
997 | ||
998 | =head2 Guidelines for Module Creation | |
999 | ||
1000 | =over 4 | |
1001 | ||
1002 | =item Do similar modules already exist in some form? | |
1003 | ||
1004 | If so, please try to reuse the existing modules either in whole or | |
1005 | by inheriting useful features into a new class. If this is not | |
1006 | practical try to get together with the module authors to work on | |
1007 | extending or enhancing the functionality of the existing modules. | |
1008 | A perfect example is the plethora of packages in perl4 for dealing | |
1009 | with command line options. | |
1010 | ||
1011 | If you are writing a module to expand an already existing set of | |
1012 | modules, please coordinate with the author of the package. It | |
1013 | helps if you follow the same naming scheme and module interaction | |
1014 | scheme as the original author. | |
1015 | ||
1016 | =item Try to design the new module to be easy to extend and reuse. | |
1017 | ||
1018 | Use blessed references. Use the two argument form of bless to bless | |
1019 | into the class name given as the first parameter of the constructor, | |
5f05dabc | 1020 | e.g.,: |
cb1a09d0 | 1021 | |
5f05dabc | 1022 | sub new { |
cb1a09d0 AD |
1023 | my $class = shift; |
1024 | return bless {}, $class; | |
1025 | } | |
1026 | ||
1027 | or even this if you'd like it to be used as either a static | |
1028 | or a virtual method. | |
1029 | ||
5f05dabc | 1030 | sub new { |
cb1a09d0 AD |
1031 | my $self = shift; |
1032 | my $class = ref($self) || $self; | |
1033 | return bless {}, $class; | |
1034 | } | |
1035 | ||
1036 | Pass arrays as references so more parameters can be added later | |
1037 | (it's also faster). Convert functions into methods where | |
1038 | appropriate. Split large methods into smaller more flexible ones. | |
1039 | Inherit methods from other modules if appropriate. | |
1040 | ||
c36e9b62 | 1041 | Avoid class name tests like: C<die "Invalid" unless ref $ref eq 'FOO'>. |
1042 | Generally you can delete the "C<eq 'FOO'>" part with no harm at all. | |
cb1a09d0 AD |
1043 | Let the objects look after themselves! Generally, avoid hardwired |
1044 | class names as far as possible. | |
1045 | ||
c36e9b62 | 1046 | Avoid C<$r-E<gt>Class::func()> where using C<@ISA=qw(... Class ...)> and |
1047 | C<$r-E<gt>func()> would work (see L<perlbot> for more details). | |
cb1a09d0 AD |
1048 | |
1049 | Use autosplit so little used or newly added functions won't be a | |
1050 | burden to programs which don't use them. Add test functions to | |
1051 | the module after __END__ either using AutoSplit or by saying: | |
1052 | ||
1053 | eval join('',<main::DATA>) || die $@ unless caller(); | |
1054 | ||
1055 | Does your module pass the 'empty sub-class' test? If you say | |
c36e9b62 | 1056 | "C<@SUBCLASS::ISA = qw(YOURCLASS);>" your applications should be able |
cb1a09d0 | 1057 | to use SUBCLASS in exactly the same way as YOURCLASS. For example, |
c36e9b62 | 1058 | does your application still work if you change: C<$obj = new YOURCLASS;> |
1059 | into: C<$obj = new SUBCLASS;> ? | |
cb1a09d0 AD |
1060 | |
1061 | Avoid keeping any state information in your packages. It makes it | |
1062 | difficult for multiple other packages to use yours. Keep state | |
1063 | information in objects. | |
1064 | ||
c36e9b62 | 1065 | Always use B<-w>. Try to C<use strict;> (or C<use strict qw(...);>). |
cb1a09d0 | 1066 | Remember that you can add C<no strict qw(...);> to individual blocks |
c36e9b62 | 1067 | of code which need less strictness. Always use B<-w>. Always use B<-w>! |
cb1a09d0 AD |
1068 | Follow the guidelines in the perlstyle(1) manual. |
1069 | ||
1070 | =item Some simple style guidelines | |
1071 | ||
1072 | The perlstyle manual supplied with perl has many helpful points. | |
1073 | ||
1074 | Coding style is a matter of personal taste. Many people evolve their | |
1075 | style over several years as they learn what helps them write and | |
1076 | maintain good code. Here's one set of assorted suggestions that | |
1077 | seem to be widely used by experienced developers: | |
1078 | ||
1079 | Use underscores to separate words. It is generally easier to read | |
1080 | $var_names_like_this than $VarNamesLikeThis, especially for | |
1081 | non-native speakers of English. It's also a simple rule that works | |
1082 | consistently with VAR_NAMES_LIKE_THIS. | |
1083 | ||
1084 | Package/Module names are an exception to this rule. Perl informally | |
1085 | reserves lowercase module names for 'pragma' modules like integer | |
1086 | and strict. Other modules normally begin with a capital letter and | |
1087 | use mixed case with no underscores (need to be short and portable). | |
1088 | ||
1089 | You may find it helpful to use letter case to indicate the scope | |
1090 | or nature of a variable. For example: | |
1091 | ||
1092 | $ALL_CAPS_HERE constants only (beware clashes with perl vars) | |
1093 | $Some_Caps_Here package-wide global/static | |
1094 | $no_caps_here function scope my() or local() variables | |
1095 | ||
1096 | Function and method names seem to work best as all lowercase. | |
5f05dabc | 1097 | e.g.,, C<$obj-E<gt>as_string()>. |
cb1a09d0 AD |
1098 | |
1099 | You can use a leading underscore to indicate that a variable or | |
1100 | function should not be used outside the package that defined it. | |
1101 | ||
1102 | =item Select what to export. | |
1103 | ||
1104 | Do NOT export method names! | |
1105 | ||
1106 | Do NOT export anything else by default without a good reason! | |
1107 | ||
1108 | Exports pollute the namespace of the module user. If you must | |
1109 | export try to use @EXPORT_OK in preference to @EXPORT and avoid | |
1110 | short or common names to reduce the risk of name clashes. | |
1111 | ||
1112 | Generally anything not exported is still accessible from outside the | |
c36e9b62 | 1113 | module using the ModuleName::item_name (or C<$blessed_ref-E<gt>method>) |
cb1a09d0 | 1114 | syntax. By convention you can use a leading underscore on names to |
5f05dabc | 1115 | indicate informally that they are 'internal' and not for public use. |
cb1a09d0 AD |
1116 | |
1117 | (It is actually possible to get private functions by saying: | |
c36e9b62 | 1118 | C<my $subref = sub { ... }; &$subref;>. But there's no way to call that |
5f05dabc | 1119 | directly as a method, because a method must have a name in the symbol |
cb1a09d0 AD |
1120 | table.) |
1121 | ||
1122 | As a general rule, if the module is trying to be object oriented | |
1123 | then export nothing. If it's just a collection of functions then | |
1124 | @EXPORT_OK anything but use @EXPORT with caution. | |
1125 | ||
1126 | =item Select a name for the module. | |
1127 | ||
5f05dabc | 1128 | This name should be as descriptive, accurate, and complete as |
cb1a09d0 AD |
1129 | possible. Avoid any risk of ambiguity. Always try to use two or |
1130 | more whole words. Generally the name should reflect what is special | |
1131 | about what the module does rather than how it does it. Please use | |
5f05dabc | 1132 | nested module names to group informally or categorize a module. |
1133 | There should be a very good reason for a module not to have a nested name. | |
cb1a09d0 AD |
1134 | Module names should begin with a capital letter. |
1135 | ||
1136 | Having 57 modules all called Sort will not make life easy for anyone | |
1137 | (though having 23 called Sort::Quick is only marginally better :-). | |
1138 | Imagine someone trying to install your module alongside many others. | |
1139 | If in any doubt ask for suggestions in comp.lang.perl.misc. | |
1140 | ||
1141 | If you are developing a suite of related modules/classes it's good | |
1142 | practice to use nested classes with a common prefix as this will | |
1143 | avoid namespace clashes. For example: Xyz::Control, Xyz::View, | |
1144 | Xyz::Model etc. Use the modules in this list as a naming guide. | |
1145 | ||
1146 | If adding a new module to a set, follow the original author's | |
1147 | standards for naming modules and the interface to methods in | |
1148 | those modules. | |
1149 | ||
1150 | To be portable each component of a module name should be limited to | |
1151 | 11 characters. If it might be used on DOS then try to ensure each is | |
1152 | unique in the first 8 characters. Nested modules make this easier. | |
1153 | ||
1154 | =item Have you got it right? | |
1155 | ||
1156 | How do you know that you've made the right decisions? Have you | |
1157 | picked an interface design that will cause problems later? Have | |
1158 | you picked the most appropriate name? Do you have any questions? | |
1159 | ||
1160 | The best way to know for sure, and pick up many helpful suggestions, | |
1161 | is to ask someone who knows. Comp.lang.perl.misc is read by just about | |
1162 | all the people who develop modules and it's the best place to ask. | |
1163 | ||
1164 | All you need to do is post a short summary of the module, its | |
1165 | purpose and interfaces. A few lines on each of the main methods is | |
1166 | probably enough. (If you post the whole module it might be ignored | |
1167 | by busy people - generally the very people you want to read it!) | |
1168 | ||
1169 | Don't worry about posting if you can't say when the module will be | |
1170 | ready - just say so in the message. It might be worth inviting | |
1171 | others to help you, they may be able to complete it for you! | |
1172 | ||
1173 | =item README and other Additional Files. | |
1174 | ||
1175 | It's well known that software developers usually fully document the | |
1176 | software they write. If, however, the world is in urgent need of | |
1177 | your software and there is not enough time to write the full | |
1178 | documentation please at least provide a README file containing: | |
1179 | ||
1180 | =over 10 | |
1181 | ||
1182 | =item * | |
1183 | A description of the module/package/extension etc. | |
1184 | ||
1185 | =item * | |
1186 | A copyright notice - see below. | |
1187 | ||
1188 | =item * | |
1189 | Prerequisites - what else you may need to have. | |
1190 | ||
1191 | =item * | |
1192 | How to build it - possible changes to Makefile.PL etc. | |
1193 | ||
1194 | =item * | |
1195 | How to install it. | |
1196 | ||
1197 | =item * | |
1198 | Recent changes in this release, especially incompatibilities | |
1199 | ||
1200 | =item * | |
1201 | Changes / enhancements you plan to make in the future. | |
1202 | ||
1203 | =back | |
1204 | ||
1205 | If the README file seems to be getting too large you may wish to | |
1206 | split out some of the sections into separate files: INSTALL, | |
1207 | Copying, ToDo etc. | |
1208 | ||
d0c42abe | 1209 | =over 4 |
1210 | ||
cb1a09d0 AD |
1211 | =item Adding a Copyright Notice. |
1212 | ||
5f05dabc | 1213 | How you choose to license your work is a personal decision. |
cb1a09d0 AD |
1214 | The general mechanism is to assert your Copyright and then make |
1215 | a declaration of how others may copy/use/modify your work. | |
1216 | ||
c36e9b62 | 1217 | Perl, for example, is supplied with two types of license: The GNU |
5f05dabc | 1218 | GPL and The Artistic License (see the files README, Copying, and |
cb1a09d0 AD |
1219 | Artistic). Larry has good reasons for NOT just using the GNU GPL. |
1220 | ||
5f05dabc | 1221 | My personal recommendation, out of respect for Larry, Perl, and the |
1222 | perl community at large is to state something simply like: | |
cb1a09d0 AD |
1223 | |
1224 | Copyright (c) 1995 Your Name. All rights reserved. | |
1225 | This program is free software; you can redistribute it and/or | |
1226 | modify it under the same terms as Perl itself. | |
1227 | ||
1228 | This statement should at least appear in the README file. You may | |
1229 | also wish to include it in a Copying file and your source files. | |
1230 | Remember to include the other words in addition to the Copyright. | |
1231 | ||
1232 | =item Give the module a version/issue/release number. | |
1233 | ||
1234 | To be fully compatible with the Exporter and MakeMaker modules you | |
1235 | should store your module's version number in a non-my package | |
5f05dabc | 1236 | variable called $VERSION. This should be a floating point |
1237 | number with at least two digits after the decimal (i.e., hundredths, | |
c36e9b62 | 1238 | e.g, C<$VERSION = "0.01">). Don't use a "1.3.2" style version. |
cb1a09d0 AD |
1239 | See Exporter.pm in Perl5.001m or later for details. |
1240 | ||
1241 | It may be handy to add a function or method to retrieve the number. | |
1242 | Use the number in announcements and archive file names when | |
1243 | releasing the module (ModuleName-1.02.tar.Z). | |
1244 | See perldoc ExtUtils::MakeMaker.pm for details. | |
1245 | ||
1246 | =item How to release and distribute a module. | |
1247 | ||
1248 | It's good idea to post an announcement of the availability of your | |
1249 | module (or the module itself if small) to the comp.lang.perl.announce | |
1250 | Usenet newsgroup. This will at least ensure very wide once-off | |
1251 | distribution. | |
1252 | ||
1253 | If possible you should place the module into a major ftp archive and | |
5f05dabc | 1254 | include details of its location in your announcement. |
cb1a09d0 AD |
1255 | |
1256 | Some notes about ftp archives: Please use a long descriptive file | |
1257 | name which includes the version number. Most incoming directories | |
1258 | will not be readable/listable, i.e., you won't be able to see your | |
1259 | file after uploading it. Remember to send your email notification | |
1260 | message as soon as possible after uploading else your file may get | |
1261 | deleted automatically. Allow time for the file to be processed | |
1262 | and/or check the file has been processed before announcing its | |
1263 | location. | |
1264 | ||
1265 | FTP Archives for Perl Modules: | |
1266 | ||
1267 | Follow the instructions and links on | |
1268 | ||
1269 | http://franz.ww.tu-berlin.de/modulelist | |
1270 | ||
5f05dabc | 1271 | or upload to one of these sites: |
cb1a09d0 AD |
1272 | |
1273 | ftp://franz.ww.tu-berlin.de/incoming | |
5f05dabc | 1274 | ftp://ftp.cis.ufl.edu/incoming |
cb1a09d0 AD |
1275 | |
1276 | and notify upload@franz.ww.tu-berlin.de. | |
1277 | ||
1278 | By using the WWW interface you can ask the Upload Server to mirror | |
1279 | your modules from your ftp or WWW site into your own directory on | |
1280 | CPAN! | |
1281 | ||
1282 | Please remember to send me an updated entry for the Module list! | |
1283 | ||
1284 | =item Take care when changing a released module. | |
1285 | ||
1286 | Always strive to remain compatible with previous released versions | |
1287 | (see 2.2 above) Otherwise try to add a mechanism to revert to the | |
1288 | old behaviour if people rely on it. Document incompatible changes. | |
1289 | ||
1290 | =back | |
1291 | ||
d0c42abe | 1292 | =back |
1293 | ||
cb1a09d0 AD |
1294 | =head2 Guidelines for Converting Perl 4 Library Scripts into Modules |
1295 | ||
1296 | =over 4 | |
1297 | ||
1298 | =item There is no requirement to convert anything. | |
1299 | ||
1300 | If it ain't broke, don't fix it! Perl 4 library scripts should | |
1301 | continue to work with no problems. You may need to make some minor | |
1302 | changes (like escaping non-array @'s in double quoted strings) but | |
1303 | there is no need to convert a .pl file into a Module for just that. | |
1304 | ||
1305 | =item Consider the implications. | |
1306 | ||
1307 | All the perl applications which make use of the script will need to | |
1308 | be changed (slightly) if the script is converted into a module. Is | |
1309 | it worth it unless you plan to make other changes at the same time? | |
1310 | ||
1311 | =item Make the most of the opportunity. | |
1312 | ||
1313 | If you are going to convert the script to a module you can use the | |
1314 | opportunity to redesign the interface. The 'Guidelines for Module | |
1315 | Creation' above include many of the issues you should consider. | |
1316 | ||
1317 | =item The pl2pm utility will get you started. | |
1318 | ||
1319 | This utility will read *.pl files (given as parameters) and write | |
1320 | corresponding *.pm files. The pl2pm utilities does the following: | |
1321 | ||
1322 | =over 10 | |
1323 | ||
1324 | =item * | |
1325 | Adds the standard Module prologue lines | |
1326 | ||
1327 | =item * | |
1328 | Converts package specifiers from ' to :: | |
1329 | ||
1330 | =item * | |
1331 | Converts die(...) to croak(...) | |
1332 | ||
1333 | =item * | |
1334 | Several other minor changes | |
1335 | ||
1336 | =back | |
1337 | ||
1338 | Being a mechanical process pl2pm is not bullet proof. The converted | |
1339 | code will need careful checking, especially any package statements. | |
1340 | Don't delete the original .pl file till the new .pm one works! | |
1341 | ||
1342 | =back | |
1343 | ||
1344 | =head2 Guidelines for Reusing Application Code | |
1345 | ||
1346 | =over 4 | |
1347 | ||
1348 | =item Complete applications rarely belong in the Perl Module Library. | |
1349 | ||
1350 | =item Many applications contain some perl code which could be reused. | |
1351 | ||
1352 | Help save the world! Share your code in a form that makes it easy | |
1353 | to reuse. | |
1354 | ||
1355 | =item Break-out the reusable code into one or more separate module files. | |
1356 | ||
1357 | =item Take the opportunity to reconsider and redesign the interfaces. | |
1358 | ||
1359 | =item In some cases the 'application' can then be reduced to a small | |
1360 | ||
1361 | fragment of code built on top of the reusable modules. In these cases | |
1362 | the application could invoked as: | |
1363 | ||
1364 | perl -e 'use Module::Name; method(@ARGV)' ... | |
5f05dabc | 1365 | or |
d0c42abe | 1366 | perl -mModule::Name ... (in perl5.002) |
cb1a09d0 AD |
1367 | |
1368 | =back |