xref: /openbsd/gnu/usr.bin/perl/pod/perlmodlib.PL (revision fdcd7346)
1#!../miniperl
2
3use strict;
4use warnings;
5
6local $ENV{LC_ALL} = 'C';
7
8my $Quiet;
9@ARGV = grep { not($_ eq '-q' and $Quiet = 1) } @ARGV;
10
11if (@ARGV) {
12    my $workdir = shift;
13    chdir $workdir
14        or die "Couldn't chdir to '$workdir': $!";
15}
16require './regen/regen_lib.pl';
17
18# MANIFEST itself is Unix style filenames, so we have to assume that Unix style
19# filenames will work.
20
21open MANIFEST, '<', 'MANIFEST'
22    or die "Can't open MANIFEST: $!";
23my @files =
24    grep !m#/perl.*\.pod#,
25    grep m#(?:\.pm|\.pod|_pm\.PL)#,
26    map {s/\s.*//s; $_}
27    grep { m#^(lib|ext|dist|cpan)/# && !m#/(?:t|demo|corpus)/# }
28    <MANIFEST>;
29close MANIFEST
30    or die "$0: failed to close MANIFEST: $!";
31
32my $out = open_new('pod/perlmodlib.pod', undef,
33                   {by => "$0 extracting documentation",
34                    from => 'the Perl source files'}, 1);
35
36my %exceptions = (
37    'abbrev' => 'Text::Abbrev',
38    'carp' => 'Carp',
39    'getopt' => 'Getopt::Std',
40    'Encode::MIME::NAME' => 'Encode::MIME::Name',
41    'libnetFAQ' => 'Net::libnetFAQ',
42);
43
44my (@pragma, @mod);
45
46for my $filename (@files) {
47    unless (open MOD, '<', $filename) {
48        warn "Couldn't open $filename: $!";
49	next;
50    }
51
52    my ($name, $thing);
53    my $foundit = 0;
54    {
55	local $/ = "";
56	while (<MOD>) {
57	    next unless /^=head1 NAME/;
58	    $foundit++;
59	    last;
60	}
61    }
62    unless ($foundit) {
63        next if pod_for_module_has_head1_NAME($filename);
64        die "p5p-controlled module $filename missing =head1 NAME\n"
65            if $filename !~ m{^(dist/|cpan/)}n # under our direct control
66            && $filename !~ m{/_[^/]+\z}       # not private
67            && $filename !~ m{/unicore/}       # not unicore
68            && $filename ne 'lib/meta_notation.pm'      # no pod
69            && $filename ne 'lib/overload/numbers.pm';  # no pod
70        warn "$filename missing =head1 NAME\n" unless $Quiet;
71	next;
72    }
73    my $title = <MOD>;
74    chomp $title;
75    close MOD
76        or die "Error closing $filename: $!";
77
78    ($name, $thing) = split /\s+--?\s+/, $title, 2;
79
80    unless ($name and $thing) {
81	warn "$filename missing name\n"  unless $name;
82	warn "$filename missing thing\n" unless $thing or $Quiet;
83	next;
84    }
85
86    $name =~ s/[^A-Za-z0-9_:\$<>].*//;
87    $name = $exceptions{$name} || $name;
88    $thing =~ s/^perl pragma to //i;
89    $thing = ucfirst $thing;
90    $title = "=item $name\n\n$thing\n\n";
91
92    if ($name =~ /[A-Z]/) {
93	push @mod, $title;
94    } else {
95	push @pragma, $title;
96    }
97}
98
99sub pod_for_module_has_head1_NAME {
100    my ($filename) = @_;
101    (my $pod_file = $filename) =~ s/\.pm\z/.pod/ or return 0;
102    return 0 if !-e $pod_file;
103    open my $fh, '<', $pod_file
104        or die "Can't open $pod_file for reading: $!\n";
105    local $/ = '';
106    while (my $para = <$fh>) {
107        return 1 if $para =~ /\A=head1 NAME$/m;
108    }
109    return 0;
110}
111
112# Much easier to special case it like this than special case the depending on
113# and parsing lib/Config.pod, or special case opening configpm and finding its
114# =head1 (which is not found with the $/="" above)
115push @mod, "=item Config\n\nAccess Perl configuration information\n\n";
116
117
118# The intent of using =cut as the heredoc terminator is to make the whole file
119# parse as (reasonably) sane Pod as-is to anything that attempts to
120# brute-force treat it as such. The content is already useful - this just
121# makes it tidier, by stopping anything doing this mistaking the rest of the
122# Perl code for Pod. eg https://metacpan.org/pod/perlmodlib
123
124print $out <<'=cut';
125=head1 NAME
126
127perlmodlib - constructing new Perl modules and finding existing ones
128
129=head1 THE PERL MODULE LIBRARY
130
131Many modules are included in the Perl distribution.  These are described
132below, and all end in F<.pm>.  You may discover compiled library
133files (usually ending in F<.so>) or small pieces of modules to be
134autoloaded (ending in F<.al>); these were automatically generated
135by the installation process.  You may also discover files in the
136library directory that end in either F<.pl> or F<.ph>.  These are
137old libraries supplied so that old programs that use them still
138run.  The F<.pl> files will all eventually be converted into standard
139modules, and the F<.ph> files made by B<h2ph> will probably end up
140as extension modules made by B<h2xs>.  (Some F<.ph> values may
141already be available through the POSIX, Errno, or Fcntl modules.)
142The B<pl2pm> file in the distribution may help in your conversion,
143but it's just a mechanical process and therefore far from bulletproof.
144
145=head2 Pragmatic Modules
146
147They work somewhat like compiler directives (pragmata) in that they
148tend to affect the compilation of your program, and thus will usually
149work well only when used within a C<use>, or C<no>.  Most of these
150are lexically scoped, so an inner BLOCK may countermand them
151by saying:
152
153    no integer;
154    no strict 'refs';
155    no warnings;
156
157which lasts until the end of that BLOCK.
158
159Some pragmas are lexically scoped--typically those that affect the
160C<$^H> hints variable.  Others affect the current package instead,
161like C<use vars> and C<use subs>, which allow you to predeclare a
162variables or subroutines within a particular I<file> rather than
163just a block.  Such declarations are effective for the entire file
164for which they were declared.  You cannot rescind them with C<no
165vars> or C<no subs>.
166
167The following pragmas are defined (and have their own documentation).
168
169=over 12
170
171=cut
172
173print $out $_ for sort @pragma;
174
175print $out <<'=cut';
176
177=back
178
179=head2 Standard Modules
180
181Standard, bundled modules are all expected to behave in a well-defined
182manner with respect to namespace pollution because they use the
183Exporter module.  See their own documentation for details.
184
185It's possible that not all modules listed below are installed on your
186system. For example, the GDBM_File module will not be installed if you
187don't have the gdbm library.
188
189=over 12
190
191=cut
192
193print $out $_ for sort @mod;
194
195print $out <<'=cut', "=cut\n";
196
197=back
198
199To find out I<all> modules installed on your system, including
200those without documentation or outside the standard release,
201just use the following command (under the default win32 shell,
202double quotes should be used instead of single quotes).
203
204    % perl -MFile::Find=find -MFile::Spec::Functions -Tlwe \
205      'find { wanted => sub { print canonpath $_ if /\.pm\z/ },
206      no_chdir => 1 }, @INC'
207
208(The -T is here to prevent @INC from being populated by C<PERL5LIB>,
209C<PERLLIB>, and C<PERL_USE_UNSAFE_INC>.)
210They should all have their own documentation installed and accessible
211via your system man(1) command.  If you do not have a B<find>
212program, you can use the Perl B<find2perl> program instead, which
213generates Perl code as output you can run through perl.  If you
214have a B<man> program but it doesn't find your modules, you'll have
215to fix your manpath.  See L<perl> for details.  If you have no
216system B<man> command, you might try the B<perldoc> program.
217
218Note also that the command C<perldoc perllocal> gives you a (possibly
219incomplete) list of the modules that have been further installed on
220your system. (The perllocal.pod file is updated by the standard MakeMaker
221install process.)
222
223=head2 Extension Modules
224
225Extension modules are written in C (or a mix of Perl and C).  They
226are usually dynamically loaded into Perl if and when you need them,
227but may also be linked in statically.  Supported extension modules
228include Socket, Fcntl, and POSIX.
229
230Many popular C extension modules do not come bundled (at least, not
231completely) due to their sizes, volatility, or simply lack of time
232for adequate testing and configuration across the multitude of
233platforms on which Perl was beta-tested.  You are encouraged to
234look for them on CPAN (described below), or using web search engines
235like Google or DuckDuckGo.
236
237=head1 CPAN
238
239CPAN stands for Comprehensive Perl Archive Network; it's a globally
240replicated trove of Perl materials, including documentation, style
241guides, tricks and traps, alternate ports to non-Unix systems and
242occasional binary distributions for these.   Search engines for
243CPAN can be found at https://www.cpan.org/
244
245Most importantly, CPAN includes around a thousand unbundled modules,
246some of which require a C compiler to build.  Major categories of
247modules are:
248
249=over
250
251=item *
252
253Language Extensions and Documentation Tools
254
255=item *
256
257Development Support
258
259=item *
260
261Operating System Interfaces
262
263=item *
264
265Networking, Device Control (modems) and InterProcess Communication
266
267=item *
268
269Data Types and Data Type Utilities
270
271=item *
272
273Database Interfaces
274
275=item *
276
277User Interfaces
278
279=item *
280
281Interfaces to / Emulations of Other Programming Languages
282
283=item *
284
285File Names, File Systems and File Locking (see also File Handles)
286
287=item *
288
289String Processing, Language Text Processing, Parsing, and Searching
290
291=item *
292
293Option, Argument, Parameter, and Configuration File Processing
294
295=item *
296
297Internationalization and Locale
298
299=item *
300
301Authentication, Security, and Encryption
302
303=item *
304
305World Wide Web, HTML, HTTP, CGI, MIME
306
307=item *
308
309Server and Daemon Utilities
310
311=item *
312
313Archiving and Compression
314
315=item *
316
317Images, Pixmap and Bitmap Manipulation, Drawing, and Graphing
318
319=item *
320
321Mail and Usenet News
322
323=item *
324
325Control Flow Utilities (callbacks and exceptions etc)
326
327=item *
328
329File Handle and Input/Output Stream Utilities
330
331=item *
332
333Miscellaneous Modules
334
335=back
336
337You can find the CPAN online at L<https://www.cpan.org/>
338
339=head1 Modules: Creation, Use, and Abuse
340
341(The following section is borrowed directly from Tim Bunce's modules
342file, available at your nearest CPAN site.)
343
344Perl implements a class using a package, but the presence of a
345package doesn't imply the presence of a class.  A package is just a
346namespace.  A class is a package that provides subroutines that can be
347used as methods.  A method is just a subroutine that expects, as its
348first argument, either the name of a package (for "static" methods),
349or a reference to something (for "virtual" methods).
350
351A module is a file that (by convention) provides a class of the same
352name (sans the .pm), plus an import method in that class that can be
353called to fetch exported symbols.  This module may implement some of
354its methods by loading dynamic C or C++ objects, but that should be
355totally transparent to the user of the module.  Likewise, the module
356might set up an AUTOLOAD function to slurp in subroutine definitions on
357demand, but this is also transparent.  Only the F<.pm> file is required to
358exist.  See L<perlsub>, L<perlobj>, and L<AutoLoader> for details about
359the AUTOLOAD mechanism.
360
361=head2 Guidelines for Module Creation
362
363=over 4
364
365=item  *
366
367Do similar modules already exist in some form?
368
369If so, please try to reuse the existing modules either in whole or
370by inheriting useful features into a new class.  If this is not
371practical try to get together with the module authors to work on
372extending or enhancing the functionality of the existing modules.
373A perfect example is the plethora of packages in perl4 for dealing
374with command line options.
375
376If you are writing a module to expand an already existing set of
377modules, please coordinate with the author of the package.  It
378helps if you follow the same naming scheme and module interaction
379scheme as the original author.
380
381=item  *
382
383Try to design the new module to be easy to extend and reuse.
384
385Try to C<use warnings;> (or C<use warnings qw(...);>).
386Remember that you can add C<no warnings qw(...);> to individual blocks
387of code that need less warnings.
388
389Use blessed references.  Use the two argument form of bless to bless
390into the class name given as the first parameter of the constructor,
391e.g.,:
392
393 sub new {
394     my $class = shift;
395     return bless {}, $class;
396 }
397
398or even this if you'd like it to be used as either a static
399or a virtual method.
400
401 sub new {
402     my $self  = shift;
403     my $class = ref($self) || $self;
404     return bless {}, $class;
405 }
406
407Pass arrays as references so more parameters can be added later
408(it's also faster).  Convert functions into methods where
409appropriate.  Split large methods into smaller more flexible ones.
410Inherit methods from other modules if appropriate.
411
412Avoid class name tests like: C<die "Invalid" unless ref $ref eq 'FOO'>.
413Generally you can delete the C<eq 'FOO'> part with no harm at all.
414Let the objects look after themselves! Generally, avoid hard-wired
415class names as far as possible.
416
417Avoid C<< $r->Class::func() >> where using C<@ISA=qw(... Class ...)> and
418C<< $r->func() >> would work.
419
420Use autosplit so little used or newly added functions won't be a
421burden to programs that don't use them. Add test functions to
422the module after __END__ either using AutoSplit or by saying:
423
424 eval join('',<main::DATA>) || die $@ unless caller();
425
426Does your module pass the 'empty subclass' test? If you say
427C<@SUBCLASS::ISA = qw(YOURCLASS);> your applications should be able
428to use SUBCLASS in exactly the same way as YOURCLASS.  For example,
429does your application still work if you change:  C<< $obj = YOURCLASS->new(); >>
430into: C<< $obj = SUBCLASS->new(); >> ?
431
432Avoid keeping any state information in your packages. It makes it
433difficult for multiple other packages to use yours. Keep state
434information in objects.
435
436Always use B<-w>.
437
438Try to C<use strict;> (or C<use strict qw(...);>).
439Remember that you can add C<no strict qw(...);> to individual blocks
440of code that need less strictness.
441
442Always use B<-w>.
443
444Follow the guidelines in L<perlstyle>.
445
446Always use B<-w>.
447
448=item  *
449
450Some simple style guidelines
451
452The perlstyle manual supplied with Perl has many helpful points.
453
454Coding style is a matter of personal taste. Many people evolve their
455style over several years as they learn what helps them write and
456maintain good code.  Here's one set of assorted suggestions that
457seem to be widely used by experienced developers:
458
459Use underscores to separate words.  It is generally easier to read
460$var_names_like_this than $VarNamesLikeThis, especially for
461non-native speakers of English. It's also a simple rule that works
462consistently with VAR_NAMES_LIKE_THIS.
463
464Package/Module names are an exception to this rule. Perl informally
465reserves lowercase module names for 'pragma' modules like integer
466and strict. Other modules normally begin with a capital letter and
467use mixed case with no underscores (need to be short and portable).
468
469You may find it helpful to use letter case to indicate the scope
470or nature of a variable. For example:
471
472 $ALL_CAPS_HERE   constants only (beware clashes with Perl vars)
473 $Some_Caps_Here  package-wide global/static
474 $no_caps_here    function scope my() or local() variables
475
476Function and method names seem to work best as all lowercase.
477e.g., C<< $obj->as_string() >>.
478
479You can use a leading underscore to indicate that a variable or
480function should not be used outside the package that defined it.
481
482=item  *
483
484Select what to export.
485
486Do NOT export method names!
487
488Do NOT export anything else by default without a good reason!
489
490Exports pollute the namespace of the module user.  If you must
491export try to use @EXPORT_OK in preference to @EXPORT and avoid
492short or common names to reduce the risk of name clashes.
493
494Generally anything not exported is still accessible from outside the
495module using the ModuleName::item_name (or C<< $blessed_ref->method >>)
496syntax.  By convention you can use a leading underscore on names to
497indicate informally that they are 'internal' and not for public use.
498
499(It is actually possible to get private functions by saying:
500C<my $subref = sub { ... };  &$subref;>.  But there's no way to call that
501directly as a method, because a method must have a name in the symbol
502table.)
503
504As a general rule, if the module is trying to be object oriented
505then export nothing. If it's just a collection of functions then
506@EXPORT_OK anything but use @EXPORT with caution.
507
508=item  *
509
510Select a name for the module.
511
512This name should be as descriptive, accurate, and complete as
513possible.  Avoid any risk of ambiguity. Always try to use two or
514more whole words.  Generally the name should reflect what is special
515about what the module does rather than how it does it.  Please use
516nested module names to group informally or categorize a module.
517There should be a very good reason for a module not to have a nested name.
518Module names should begin with a capital letter.
519
520Having 57 modules all called Sort will not make life easy for anyone
521(though having 23 called Sort::Quick is only marginally better :-).
522Imagine someone trying to install your module alongside many others.
523
524If you are developing a suite of related modules/classes it's good
525practice to use nested classes with a common prefix as this will
526avoid namespace clashes. For example: Xyz::Control, Xyz::View,
527Xyz::Model etc. Use the modules in this list as a naming guide.
528
529If adding a new module to a set, follow the original author's
530standards for naming modules and the interface to methods in
531those modules.
532
533If developing modules for private internal or project specific use,
534that will never be released to the public, then you should ensure
535that their names will not clash with any future public module. You
536can do this either by using the reserved Local::* category or by
537using a category name that includes an underscore like Foo_Corp::*.
538
539To be portable each component of a module name should be limited to
54011 characters. If it might be used on MS-DOS then try to ensure each is
541unique in the first 8 characters. Nested modules make this easier.
542
543For additional guidance on the naming of modules, please consult:
544
545    https://pause.perl.org/pause/query?ACTION=pause_namingmodules
546
547or send mail to the <module-authors@perl.org> mailing list.
548
549=item  *
550
551Have you got it right?
552
553How do you know that you've made the right decisions? Have you
554picked an interface design that will cause problems later? Have
555you picked the most appropriate name? Do you have any questions?
556
557The best way to know for sure, and pick up many helpful suggestions,
558is to ask someone who knows. The <module-authors@perl.org> mailing list
559is useful for this purpose; it's also accessible via news interface as
560perl.module-authors at nntp.perl.org.
561
562All you need to do is post a short summary of the module, its
563purpose and interfaces. A few lines on each of the main methods is
564probably enough. (If you post the whole module it might be ignored
565by busy people - generally the very people you want to read it!)
566
567Don't worry about posting if you can't say when the module will be
568ready - just say so in the message. It might be worth inviting
569others to help you, they may be able to complete it for you!
570
571=item  *
572
573README and other Additional Files.
574
575It's well known that software developers usually fully document the
576software they write. If, however, the world is in urgent need of
577your software and there is not enough time to write the full
578documentation please at least provide a README file containing:
579
580=over 10
581
582=item *
583
584A description of the module/package/extension etc.
585
586=item *
587
588A copyright notice - see below.
589
590=item *
591
592Prerequisites - what else you may need to have.
593
594=item *
595
596How to build it - possible changes to Makefile.PL etc.
597
598=item *
599
600How to install it.
601
602=item *
603
604Recent changes in this release, especially incompatibilities
605
606=item *
607
608Changes / enhancements you plan to make in the future.
609
610=back
611
612If the README file seems to be getting too large you may wish to
613split out some of the sections into separate files: INSTALL,
614Copying, ToDo etc.
615
616=over 4
617
618=item *
619
620Adding a Copyright Notice.
621
622How you choose to license your work is a personal decision.
623The general mechanism is to assert your Copyright and then make
624a declaration of how others may copy/use/modify your work.
625
626Perl, for example, is supplied with two types of licence: The GNU GPL
627and The Artistic Licence (see the files README, Copying, and Artistic,
628or L<perlgpl> and L<perlartistic>).  Larry has good reasons for NOT
629just using the GNU GPL.
630
631My personal recommendation, out of respect for Larry, Perl, and the
632Perl community at large is to state something simply like:
633
634 Copyright (c) 1995 Your Name. All rights reserved.
635 This program is free software; you can redistribute it and/or
636 modify it under the same terms as Perl itself.
637
638This statement should at least appear in the README file. You may
639also wish to include it in a Copying file and your source files.
640Remember to include the other words in addition to the Copyright.
641
642=item  *
643
644Give the module a version/issue/release number.
645
646To be fully compatible with the Exporter and MakeMaker modules you
647should store your module's version number in a non-my package
648variable called $VERSION.  This should be a positive floating point
649number with at least two digits after the decimal (i.e., hundredths,
650e.g, C<$VERSION = "0.01">).  Don't use a "1.3.2" style version.
651See L<Exporter> for details.
652
653It may be handy to add a function or method to retrieve the number.
654Use the number in announcements and archive file names when
655releasing the module (ModuleName-1.02.tar.Z).
656See perldoc ExtUtils::MakeMaker.pm for details.
657
658=item  *
659
660How to release and distribute a module.
661
662If possible, register the module with CPAN. Follow the instructions
663and links on:
664
665   https://www.cpan.org/modules/04pause.html
666
667and upload to:
668
669   https://pause.perl.org/
670
671and notify <modules@perl.org>. This will allow anyone to install
672your module using the C<cpan> tool distributed with Perl.
673
674By using the WWW interface you can ask the Upload Server to mirror
675your modules from your ftp or WWW site into your own directory on
676CPAN!
677
678=item  *
679
680Take care when changing a released module.
681
682Always strive to remain compatible with previous released versions.
683Otherwise try to add a mechanism to revert to the
684old behavior if people rely on it.  Document incompatible changes.
685
686=back
687
688=back
689
690=head2 Guidelines for Converting Perl 4 Library Scripts into Modules
691
692=over 4
693
694=item  *
695
696There is no requirement to convert anything.
697
698If it ain't broke, don't fix it! Perl 4 library scripts should
699continue to work with no problems. You may need to make some minor
700changes (like escaping non-array @'s in double quoted strings) but
701there is no need to convert a .pl file into a Module for just that.
702
703=item  *
704
705Consider the implications.
706
707All Perl applications that make use of the script will need to
708be changed (slightly) if the script is converted into a module.  Is
709it worth it unless you plan to make other changes at the same time?
710
711=item  *
712
713Make the most of the opportunity.
714
715If you are going to convert the script to a module you can use the
716opportunity to redesign the interface.  The guidelines for module
717creation above include many of the issues you should consider.
718
719=item  *
720
721The pl2pm utility will get you started.
722
723This utility will read *.pl files (given as parameters) and write
724corresponding *.pm files. The pl2pm utilities does the following:
725
726=over 10
727
728=item *
729
730Adds the standard Module prologue lines
731
732=item *
733
734Converts package specifiers from ' to ::
735
736=item *
737
738Converts die(...) to croak(...)
739
740=item *
741
742Several other minor changes
743
744=back
745
746Being a mechanical process pl2pm is not bullet proof. The converted
747code will need careful checking, especially any package statements.
748Don't delete the original .pl file till the new .pm one works!
749
750=back
751
752=head2 Guidelines for Reusing Application Code
753
754=over 4
755
756=item  *
757
758Complete applications rarely belong in the Perl Module Library.
759
760=item  *
761
762Many applications contain some Perl code that could be reused.
763
764Help save the world! Share your code in a form that makes it easy
765to reuse.
766
767=item  *
768
769Break-out the reusable code into one or more separate module files.
770
771=item  *
772
773Take the opportunity to reconsider and redesign the interfaces.
774
775=item  *
776
777In some cases the 'application' can then be reduced to a small
778
779fragment of code built on top of the reusable modules. In these cases
780the application could invoked as:
781
782     % perl -e 'use Module::Name; method(@ARGV)' ...
783or
784     % perl -mModule::Name ...    (in perl5.002 or higher)
785
786=back
787
788=head1 NOTE
789
790Perl does not enforce private and public parts of its modules as you may
791have been used to in other languages like C++, Ada, or Modula-17.  Perl
792doesn't have an infatuation with enforced privacy.  It would prefer
793that you stayed out of its living room because you weren't invited, not
794because it has a shotgun.
795
796The module and its user have a contract, part of which is common law,
797and part of which is "written".  Part of the common law contract is
798that a module doesn't pollute any namespace it wasn't asked to.  The
799written contract for the module (A.K.A. documentation) may make other
800provisions.  But then you know when you C<use RedefineTheWorld> that
801you're redefining the world and willing to take the consequences.
802
803=cut
804
805read_only_bottom_close_and_rename($out);
806