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