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