4 use File::Basename qw(&basename &dirname);
7 # List explicitly here the variables you want Configure to
8 # generate. Metaconfig only looks for shell variables, so you
9 # have to mention them as if they were shell variables, not
10 # %Config entries. Thus you write
12 # to ensure Configure will look for $Config{startperl}.
14 # This forces PL files to create target in same directory as PL file.
15 # This is so that make depend always knows where to find PL derivatives.
18 $file = basename($0, '.PL');
19 $file .= '.com' if $^O eq 'VMS';
21 open OUT,">$file" or die "Can't create $file: $!";
23 print "Extracting $file (with variable substitutions)\n";
25 # In this section, perl variables will be expanded during extraction.
26 # You can use $Config{...} to use Configure variables.
28 print OUT <<"!GROK!THIS!";
30 eval 'exec $Config{perlpath} -S \$0 \${1+"\$@"}'
36 # make sure creat()s are neither too much nor too little
37 INIT { eval { umask(0077) } } # doubtless someone has no mask
40 push \@pagers, "$Config{'pager'}" if -x "$Config{'pager'}";
44 # In the following, perl variables are not expanded during extraction.
46 print OUT <<'!NO!SUBS!';
48 use Fcntl; # for sysopen
53 # Perldoc revision #1 -- look up a piece of documentation in .pod format that
54 # is embedded in the perl installation tree.
56 # This is not to be confused with Tom Christiansen's perlman, which is a
57 # man replacement, written in perl. This perldoc is strictly for reading
58 # the perl manuals, though it too is written in perl.
60 # Massive security and correctness patches applied to this
61 # noisome program by Tom Christiansen Sat Mar 11 15:22:33 MST 2000
64 my $me = $0; # Editing $0 is unportable
67 Usage: $me [-h] [-r] [-i] [-v] [-t] [-u] [-m] [-n program] [-l] [-F] [-X] PageName|ModuleName|ProgramName
71 The -h option prints more help. Also try "perldoc perldoc" to get
72 acquainted with the system.
76 my @global_found = ();
77 my $global_target = "";
79 my $Is_VMS = $^O eq 'VMS';
80 my $Is_MSWin32 = $^O eq 'MSWin32';
81 my $Is_Dos = $^O eq 'dos';
83 # refuse to run if we should be tainting and aren't
84 # (but regular users deserve protection too, though!)
85 if (!($Is_VMS || $Is_MSWin32 || $Is_Dos) && ($> == 0 || $< == 0)
86 && !am_taint_checking())
88 die "Superuser must not run $0 without security audit and taint checks.\n";
93 # Erase evidence of previous errors (if any), so exit status is simple.
96 perldoc [options] PageName|ModuleName|ProgramName...
97 perldoc [options] -f BuiltinFunction
98 perldoc [options] -q FAQRegex
101 -h Display this help message
102 -r Recursive search (slow)
104 -t Display pod using pod2text instead of pod2man and nroff
105 (-t is the default on win32)
106 -u Display unformatted pod text
107 -m Display module's file in its entirety
108 -n Specify replacement for nroff
109 -l Display the module's file name
110 -F Arguments are file names, not modules
111 -v Verbosely describe what's going on
112 -X use index if present (looks for pod.idx at $Config{archlib})
113 -q Search the text of questions (not answers) in perlfaq[1-9]
115 PageName|ModuleName...
116 is the name of a piece of documentation that you want to look at. You
117 may either give a descriptive name of the page (as in the case of
118 `perlfunc') the name of a module, either like `Term::Info',
119 `Term/Info', the partial name of a module, like `info', or
120 `makemaker', or the name of a program, like `perldoc'.
123 is the name of a perl function. Will extract documentation from
127 is a regex. Will search perlfaq[1-9] for and extract any
128 questions that match.
130 Any switches in the PERLDOC environment variable will be used before the
131 command line arguments. The optional pod index file contains a list of
132 filenames, one per line.
137 if (defined $ENV{"PERLDOC"}) {
138 require Text::ParseWords;
139 unshift(@ARGV, Text::ParseWords::shellwords($ENV{"PERLDOC"}));
143 my $getopts = "mhtluvriFf:Xq:n:";
144 print OUT <<"!GET!OPTS!";
146 use vars qw( @{[map "\$opt_$_", ($getopts =~ /\w/g)]} );
148 getopts("$getopts") || usage;
151 print OUT <<'!NO!SUBS!';
154 $opt_n = "nroff" if !$opt_n;
158 $podidx = "$Config{'archlib'}/pod.idx";
159 $podidx = "" unless -f $podidx && -r _ && -M _ <= 7;
162 if ((my $opts = do{ no warnings; $opt_t + $opt_u + $opt_m + $opt_l }) > 1) {
163 usage("only one of -t, -u, -m or -l")
167 || !($ENV{TERM} && $ENV{TERM} !~ /dumb|emacs|none|unknown/i))
169 $opt_t = 1 unless $opts;
172 if ($opt_t) { require Pod::Text; import Pod::Text; }
176 @pages = ("perlfunc");
179 @pages = ("perlfaq1" .. "perlfaq9");
185 # Does this look like a module or extension directory?
186 if (-f "Makefile.PL") {
188 # Add ., lib to @INC (if they exist)
189 eval q{ use lib qw(. lib); 1; } or die;
191 # don't add if superuser
192 if ($< && $>) { # don't be looking too hard now!
193 eval q{ use blib; 1 } or die;
198 my($file, $readit) = @_;
199 return 1 if !$readit && $file =~ /\.pod\z/i;
201 open(TEST,"<", $file) or die "Can't open $file: $!";
204 close(TEST) or die "Can't close $file: $!";
208 close(TEST) or die "Can't close $file: $!";
214 my $path = join('/',$dir,$file); # XXX: dirseps
215 return $path if -f $path and -r _;
216 if (!$opt_i or $Is_VMS or $Is_MSWin32 or $Is_Dos or $^O eq 'os2') {
217 # on a case-forgiving file system or if case is important
218 # that is it all we can do
219 warn "Ignored $path: unreadable\n" if -f _;
223 # this is completely wicked. don't mess with $", and if
224 # you do, don't assume / is the dirsep!
228 foreach $p (split(m!/!, $file)){ # XXX: dirseps
233 if ( $p eq $global_target) {
234 my $tmp_path = join ('/', @p); # XXX: dirseps
236 for (@global_found) {
237 $path_f = 1 if $_ eq $tmp_path;
239 push (@global_found, $tmp_path) unless $path_f;
240 print STDERR "Found as @p but directory\n" if $opt_v;
243 elsif (-f _ && -r _) {
247 warn "Ignored $try: unreadable\n";
252 opendir DIR, "@p" or die "opendir @p: $!";
253 while ($cip=readdir(DIR)) {
254 if (lc $cip eq $lcp){
259 closedir DIR or die "closedir @p: $!";
260 return "" unless $found;
262 return "@p" if -f "@p" and -r _;
263 warn "Ignored @p: unreadable\n" if -f _;
272 return "" if length $dir and not -d $dir;
274 return minus_f_nocase($dir,$file);
277 my $path = minus_f_nocase($dir,$file);
278 return $path if length $path and containspod($path);
285 my($recurse,$s,@dirs) = @_;
287 $s = VMS::Filespec::unixify($s) if $Is_VMS;
288 return $s if -f $s && containspod($s);
289 printf STDERR "Looking for $s in @dirs\n" if $opt_v;
293 $global_target = (split(m!/!, $s))[-1]; # XXX: dirseps
294 for ($i=0; $i<@dirs; $i++) {
296 ($dir = VMS::Filespec::unixpath($dir)) =~ s!/\z!! if $Is_VMS;
297 if ( ( $ret = check_file $dir,"$s.pod")
298 or ( $ret = check_file $dir,"$s.pm")
299 or ( $ret = check_file $dir,$s)
301 $ret = check_file $dir,"$s.com")
302 or ( $^O eq 'os2' and
303 $ret = check_file $dir,"$s.cmd")
304 or ( ($Is_MSWin32 or $Is_Dos or $^O eq 'os2') and
305 $ret = check_file $dir,"$s.bat")
306 or ( $ret = check_file "$dir/pod","$s.pod")
307 or ( $ret = check_file "$dir/pod",$s)
308 or ( $ret = check_file "$dir/pods","$s.pod")
309 or ( $ret = check_file "$dir/pods",$s)
315 opendir(D,$dir) or die "Can't opendir $dir: $!";
316 my @newdirs = map "$dir/$_", grep { # XXX: dirseps
318 not /^auto\z/s and # save time! don't search auto dirs
319 -d "$dir/$_" # XXX: dirseps
321 closedir(D) or die "Can't closedir $dir: $!";
322 next unless @newdirs;
324 @newdirs = map((s/\.dir\z//,$_)[1],@newdirs) if $Is_VMS;
325 print STDERR "Also looking in @newdirs\n" if $opt_v;
326 push(@dirs,@newdirs);
333 my @data = split /\n{2,}/, shift;
334 shift @data while @data and $data[0] !~ /\S/; # Go to header
335 shift @data if @data and $data[0] =~ /Contributed\s+Perl/; # Skip header
336 pop @data if @data and $data[-1] =~ /^\w/; # Skip footer, like
337 # 28/Jan/99 perl 5.005, patch 53 1
342 my ($file, $tmp, $filter) = @_;
346 # why was this append?
347 sysopen(OUT, $tmp, O_WRONLY | O_EXCL | O_CREAT, 0600)
348 or die ("Can't open $tmp: $!");
349 Pod::Text->new()->parse_from_file($file,\*OUT);
350 close OUT or die "can't close $tmp: $!";
353 my $cmd = "pod2man --lax $file | $opt_n -man";
354 $cmd .= " | col -x" if $^O =~ /hpux/;
356 $rslt = filter_nroff($rslt) if $filter;
357 unless (($err = $?)) {
358 # why was this append?
359 sysopen(TMP, $tmp, O_WRONLY | O_EXCL | O_CREAT, 0600)
360 or die "Can't open $tmp: $!";
362 or die "Can't print $tmp: $!";
364 or die "Can't close $tmp: $!";
367 if ($opt_u or $err or -z $tmp) { # XXX: race with -z
368 # why was this append?
369 sysopen(OUT, $tmp, O_WRONLY | O_EXCL | O_CREAT, 0600)
370 or die "Can't open $tmp: $!";
371 open(IN,"<", $file) or die("Can't open $file: $!");
375 $cut = $1 eq 'cut' if /^=(\w+)/;
378 or die "Can't print $tmp: $!";
380 close IN or die "Can't close $file: $!";
381 close OUT or die "Can't close $tmp: $!";
386 my ($tmp, $no_tty, @pagers) = @_;
388 open(TMP,"<", $tmp) or die "Can't open $tmp: $!";
391 print or die "Can't print to stdout: $!";
393 close TMP or die "Can't close while $tmp: $!";
396 foreach my $pager (@pagers) {
397 last if system("$pager $tmp") == 0;
406 1 while unlink($_); # XXX: expect failure
408 unlink($_); # or die "Can't unlink $_: $!";
415 if ($podidx && open(PODIDX, $podidx)) {
417 $searchfor =~ s,::,/,g; # XXX: dirseps
418 print STDERR "Searching for '$searchfor' in $podidx\n" if $opt_v;
422 push(@found, $_) if m,/$searchfor(?:\.(?:pod|pm))?\z,i;
424 close(PODIDX) or die "Can't close $podidx: $!";
427 print STDERR "Searching for $_\n" if $opt_v;
428 # We must look both in @INC for library modules and in PATH
429 # for executables, like h2xs or perldoc itself.
430 my @searchdirs = @INC;
433 push @found, $_ if $opt_m or containspod($_);
439 for ($i = 0; $trn = $ENV{'DCL$PATH;'.$i}; $i++) {
440 push(@searchdirs,$trn);
442 push(@searchdirs,'perl_root:[lib.pod]') # installed pods
445 push(@searchdirs, grep(-d, split($Config{path_sep},
449 my @files = searchfor(0,$_,@searchdirs);
451 print STDERR "Found as @files\n" if $opt_v;
454 # no match, try recursive search
455 @searchdirs = grep(!/^\.\z/s,@INC);
456 @files= searchfor(1,$_,@searchdirs) if $opt_r;
458 print STDERR "Loosely found as @files\n" if $opt_v;
461 print STDERR "No documentation found for \"$_\".\n";
463 print STDERR "However, try\n";
464 for my $dir (@global_found) {
465 opendir(DIR, $dir) or die "opendir $dir: $!";
466 while (my $file = readdir(DIR)) {
467 next if ($file =~ /^\./s);
468 $file =~ s/\.(pm|pod)\z//; # XXX: badfs
469 print STDERR "\tperldoc $_\::$file\n";
471 closedir DIR or die "closedir $dir: $!";
480 exit ($Is_VMS ? 98962 : 1);
484 print join("\n", @found), "\n";
488 my $lines = $ENV{LINES} || 24;
491 if (! -t STDOUT) { $no_tty = 1 }
492 END { close(STDOUT) || die "Can't close STDOUT: $!" }
494 # until here we could simply exit or die
495 # now we create temporary files that we have to clean up
496 # namely $tmp, $buffer
497 # that's because you did it wrong, should be descriptor based --tchrist
502 $tmp = "$ENV{TEMP}\\perldoc1.$$";
503 $buffer = "$ENV{TEMP}\\perldoc1.b$$";
504 push @pagers, qw( more< less notepad );
505 unshift @pagers, $ENV{PAGER} if $ENV{PAGER};
506 for (@found) { s,/,\\,g }
509 $tmp = 'Sys$Scratch:perldoc.tmp1_'.$$;
510 $buffer = 'Sys$Scratch:perldoc.tmp1_b'.$$;
511 push @pagers, qw( most more less type/page );
514 $tmp = "$ENV{TEMP}/perldoc1.$$";
515 $buffer = "$ENV{TEMP}/perldoc1.b$$";
517 $buffer =~ tr!\\/!//!s;
518 push @pagers, qw( less.exe more.com< );
519 unshift @pagers, $ENV{PAGER} if $ENV{PAGER};
524 $tmp = POSIX::tmpnam();
525 $buffer = POSIX::tmpnam();
526 unshift @pagers, 'less', 'cmd /c more <';
529 # XXX: this is not secure, because it doesn't open it
530 ($tmp, $buffer) = eval { require POSIX }
531 ? (POSIX::tmpnam(), POSIX::tmpnam() )
532 : ("/tmp/perldoc1.$$", "/tmp/perldoc1.b$$" );
534 push @pagers, qw( more less pg view cat );
535 unshift @pagers, $ENV{PAGER} if $ENV{PAGER};
537 unshift @pagers, $ENV{PERLDOC_PAGER} if $ENV{PERLDOC_PAGER};
539 # make sure cleanup called
541 sub END { cleanup($tmp, $buffer) }
544 eval q{ use sigtrap qw(die INT TERM HUP QUIT) };
547 foreach my $pager (@pagers) {
548 if (system($pager, @found) == 0) {
554 use vmsish qw(status exit);
564 my $perlfunc = shift @found;
565 open(PFUNC, "<", $perlfunc)
566 or die("Can't open $perlfunc: $!");
568 # Functions like -r, -e, etc. are listed under `-X'.
569 my $search_string = ($opt_f =~ /^-[rwxoRWXOeszfdlpSbctugkTBMAC]$/)
575 last if /^=head2 Alphabetical Listing of Perl Functions/;
578 # Look for our function
582 if (/^=item\s+\Q$search_string\E\b/o) {
586 last if $found > 1 and not $inlist;
596 ++$found if /^\w/; # found descriptive text
599 die "No documentation for perl function `$opt_f' found\n";
601 close PFUNC or die "Can't open $perlfunc: $!";
605 local @ARGV = @found; # I'm lazy, sue me.
608 my $rx = eval { qr/$opt_q/ } or die <<EOD;
609 Invalid regular expression '$opt_q' given as -q pattern:
611 Did you mean \\Q$opt_q ?
615 for (@found) { die "invalid file spec: $!" if /[<>|]/ }
618 if (/^=head2\s+.*(?:$opt_q)/oi) {
620 push @pod, "=head1 Found in $ARGV\n\n" unless $found_in{$ARGV}++;
629 die("No documentation for perl FAQ keyword `$opt_q' found\n");
636 sysopen(TMP, $buffer, O_WRONLY | O_EXCL | O_CREAT)
637 or die("Can't open $buffer: $!");
638 print TMP "=over 8\n\n";
639 print TMP @pod or die "Can't print $buffer: $!";
641 close TMP or die "Can't close $buffer: $!";
647 printout($_, $tmp, $filter);
649 page($tmp, $no_tty, @pagers);
655 my $nada = substr($arg, 0, 0); # zero-length
656 local $@; # preserve caller's version
657 eval { eval "# $nada" };
658 return length($@) != 0;
661 sub am_taint_checking {
662 my($k,$v) = each %ENV;
663 return is_tainted($v);
671 perldoc - Look up Perl documentation in pod format.
675 B<perldoc> [B<-h>] [B<-v>] [B<-t>] [B<-u>] [B<-m>] [B<-l>] [B<-F>] [B<-X>] PageName|ModuleName|ProgramName
677 B<perldoc> B<-f> BuiltinFunction
679 B<perldoc> B<-q> FAQ Keyword
683 I<perldoc> looks up a piece of documentation in .pod format that is embedded
684 in the perl installation tree or in a perl script, and displays it via
685 C<pod2man | nroff -man | $PAGER>. (In addition, if running under HP-UX,
686 C<col -x> will be used.) This is primarily used for the documentation for
687 the perl library modules.
689 Your system may also have man pages installed for those modules, in
690 which case you can probably just use the man(1) command.
698 Prints out a brief help message.
702 Describes search for the item in detail.
704 =item B<-t> text output
706 Display docs using plain text converter, instead of nroff. This may be faster,
707 but it won't look as nice.
709 =item B<-u> unformatted
711 Find docs only; skip reformatting by pod2*
715 Display the entire module: both code and unformatted pod documentation.
716 This may be useful if the docs don't explain a function in the detail
717 you need, and you'd like to inspect the code directly; perldoc will find
718 the file for you and simply hand it off for display.
720 =item B<-l> file name only
722 Display the file name of the module found.
724 =item B<-F> file names
726 Consider arguments as file names, no search in directories will be performed.
730 The B<-f> option followed by the name of a perl built in function will
731 extract the documentation of this function from L<perlfunc>.
735 The B<-q> option takes a regular expression as an argument. It will search
736 the question headings in perlfaq[1-9] and print the entries matching
737 the regular expression.
739 =item B<-X> use an index if present
741 The B<-X> option looks for a entry whose basename matches the name given on the
742 command line in the file C<$Config{archlib}/pod.idx>. The pod.idx file should
743 contain fully qualified filenames, one per line.
745 =item B<PageName|ModuleName|ProgramName>
747 The item you want to look up. Nested modules (such as C<File::Basename>)
748 are specified either as C<File::Basename> or C<File/Basename>. You may also
749 give a descriptive name of a page, such as C<perlfunc>. You may also give a
750 partial or wrong-case name, such as "basename" for "File::Basename", but
751 this will be slower, if there is more then one page with the same partial
752 name, you will only get the first one.
758 Any switches in the C<PERLDOC> environment variable will be used before the
759 command line arguments. C<perldoc> also searches directories
760 specified by the C<PERL5LIB> (or C<PERLLIB> if C<PERL5LIB> is not
761 defined) and C<PATH> environment variables.
762 (The latter is so that embedded pods for executables, such as
763 C<perldoc> itself, are available.) C<perldoc> will use, in order of
764 preference, the pager defined in C<PERLDOC_PAGER>, C<MANPAGER>, or
765 C<PAGER> before trying to find a pager on its own. (C<MANPAGER> is not
766 used if C<perldoc> was told to display plain text or unformatted pod.)
768 One useful value for C<PERLDOC_PAGER> is C<less -+C -E>.
772 This is perldoc v2.01.
776 Kenneth Albanowski <kjahds@kjahds.com>
778 Minor updates by Andy Dougherty <doughera@lafcol.lafayette.edu>,
784 # Version 2.01: Sat Mar 11 15:22:33 MST 2000
785 # Tom Christiansen <tchrist@perl.com>, querulously.
786 # Security and correctness patches.
787 # What a twisted bit of distasteful spaghetti code.
789 # Version 1.15: Tue Aug 24 01:50:20 EST 1999
790 # Charles Wilson <cwilson@ece.gatech.edu>
791 # changed /pod/ directory to /pods/ for cygwin
792 # to support cygwin/win32
793 # Version 1.14: Wed Jul 15 01:50:20 EST 1998
794 # Robin Barker <rmb1@cise.npl.co.uk>
795 # -strict, -w cleanups
796 # Version 1.13: Fri Feb 27 16:20:50 EST 1997
797 # Gurusamy Sarathy <gsar@activestate.com>
798 # -doc tweaks for -F and -X options
799 # Version 1.12: Sat Apr 12 22:41:09 EST 1997
800 # Gurusamy Sarathy <gsar@activestate.com>
801 # -various fixes for win32
802 # Version 1.11: Tue Dec 26 09:54:33 EST 1995
803 # Kenneth Albanowski <kjahds@kjahds.com>
804 # -added Charles Bailey's further VMS patches, and -u switch
805 # -added -t switch, with pod2text support
807 # Version 1.10: Thu Nov 9 07:23:47 EST 1995
808 # Kenneth Albanowski <kjahds@kjahds.com>
810 # -added better error recognition (on no found pages, just exit. On
811 # missing nroff/pod2man, just display raw pod.)
812 # -added recursive/case-insensitive matching (thanks, Andreas). This
813 # slows things down a bit, unfortunately. Give a precise name, and
816 # Version 1.01: Tue May 30 14:47:34 EDT 1995
817 # Andy Dougherty <doughera@lafcol.lafayette.edu>
818 # -added pod documentation.
819 # -added PATH searching.
820 # -added searching pod/ subdirectory (mainly to pick up perlfunc.pod
826 # Cache directories read during sloppy match
829 close OUT or die "Can't close $file: $!";
830 chmod 0755, $file or die "Can't reset permissions for $file: $!\n";
831 exec("$Config{'eunicefix'} $file") if $Config{'eunicefix'} ne ':';