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