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