home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / Share / Editores / Perl5 / perl / bin / perldoc.bat < prev    next >
Encoding:
DOS Batch File  |  1997-08-10  |  11.6 KB  |  457 lines

  1. @rem = '--*-Perl-*--
  2. @echo off
  3. perl -x -S %0 %1 %2 %3 %4 %5 %6 %7 %8 %9
  4. goto endofperl
  5. @rem ';
  6. #!perl
  7. #line 8
  8. #perl
  9.     eval 'exec c:\perl\bin\perl.exe -S $0 ${1+"$@"}'
  10.     if $running_under_some_shell;
  11.  
  12. @pagers = ();
  13. push @pagers, "more /e" if -x "more /e";
  14.  
  15. #
  16. # Perldoc revision #1 -- look up a piece of documentation in .pod format that
  17. # is embedded in the perl installation tree.
  18. #
  19. # This is not to be confused with Tom Christianson's perlman, which is a
  20. # man replacement, written in perl. This perldoc is strictly for reading
  21. # the perl manuals, though it too is written in perl.
  22.  
  23. if(@ARGV<1) {
  24.         $0 =~ s,.*/,,;
  25.     die <<EOF;
  26. Usage: $0 [-h] [-v] [-t] [-u] [-m] [-l] PageName|ModuleName|ProgramName
  27.        $0 -f PerlFunc
  28.  
  29. We suggest you use "perldoc perldoc" to get aquainted 
  30. with the system.
  31. EOF
  32. }
  33.  
  34. use Getopt::Std;
  35. use Config '%Config';
  36.  
  37. $Is_VMS = $^O eq 'VMS';
  38. $Is_MSWin32 = $^O eq 'MSWin32';
  39.  
  40. sub usage{
  41.     warn "@_\n" if @_;
  42.     # Erase evidence of previous errors (if any), so exit status is simple.
  43.     $! = 0;
  44.     die <<EOF;
  45. perldoc [options] PageName|ModuleName|ProgramName...
  46. perldoc [options] -f BuiltinFunction
  47.  
  48. Options:
  49.     -h   Display this help message
  50.     -t   Display pod using pod2text instead of pod2man and nroff
  51.              (-t is the default on win32)
  52.     -u     Display unformatted pod text
  53.     -m   Display modules file in its entirety
  54.     -l   Display the modules file name
  55.     -v     Verbosely describe what's going on
  56.  
  57. PageName|ModuleName...
  58.          is the name of a piece of documentation that you want to look at. You 
  59.          may either give a descriptive name of the page (as in the case of
  60.          `perlfunc') the name of a module, either like `Term::Info', 
  61.          `Term/Info', the partial name of a module, like `info', or 
  62.          `makemaker', or the name of a program, like `perldoc'.
  63.  
  64. BuiltinFunction
  65.          is the name of a perl function.  Will extract documentation from
  66.          `perlfunc'.
  67.          
  68. Any switches in the PERLDOC environment variable will be used before the 
  69. command line arguments.
  70.  
  71. EOF
  72. }
  73.  
  74. use Text::ParseWords;
  75.  
  76.  
  77. unshift(@ARGV,shellwords($ENV{"PERLDOC"}));
  78.  
  79. getopts("mhtluvf:") || usage;
  80.  
  81. usage if $opt_h || $opt_h; # avoid -w warning
  82.  
  83. if ($opt_t + $opt_u + $opt_m + $opt_l > 1) {
  84.     usage("only one of -t, -u, -m or -l")
  85. } elsif ($Is_MSWin32) {
  86.     $opt_t = 1 unless $opt_t + $opt_u + $opt_m + $opt_l;
  87. }
  88.  
  89. if ($opt_t) { require Pod::Text; import Pod::Text; }
  90.  
  91. if ($opt_f) {
  92.    @pages = ("perlfunc");
  93. } else {
  94.    @pages = @ARGV;
  95. }
  96.  
  97.  
  98.  
  99. sub containspod {
  100.     my($file) = @_;
  101.     local($_);
  102.     open(TEST,"<$file");
  103.     while(<TEST>) {
  104.         if(/^=head/) {
  105.             close(TEST);
  106.             return 1;
  107.         }
  108.     }
  109.     close(TEST);
  110.     return 0;
  111. }
  112.  
  113. sub minus_f_nocase {
  114.      my($file) = @_;
  115.      # on a case-forgiving file system we can simply use -f $file
  116.      if ($Is_VMS or $Is_MSWin32 or $^O eq 'os2') {
  117.         return ( -f $file ) ? $file : '';
  118.      }
  119.      local *DIR;
  120.      local($")="/";
  121.      my(@p,$p,$cip);
  122.      foreach $p (split(/\//, $file)){
  123.      if (-d ("@p/$p")){
  124.          push @p, $p;
  125.      } elsif (-f ("@p/$p")) {
  126.          return "@p/$p";
  127.      } else {
  128.          my $found=0;
  129.          my $lcp = lc $p;
  130.          opendir DIR, "@p";
  131.          while ($cip=readdir(DIR)) {
  132.          if (lc $cip eq $lcp){
  133.              $found++;
  134.              last;
  135.          }
  136.          }
  137.          closedir DIR;
  138.          return "" unless $found;
  139.          push @p, $cip;
  140.          return "@p" if -f "@p";
  141.      }
  142.      }
  143.      return; # is not a file
  144.  }
  145.  
  146.   sub searchfor {
  147.       my($recurse,$s,@dirs) = @_;
  148.       $s =~ s!::!/!g;
  149.       $s = VMS::Filespec::unixify($s) if $Is_VMS;
  150.     return $s if -f $s && containspod($s);
  151.       printf STDERR "looking for $s in @dirs\n" if $opt_v;
  152.      my $ret;
  153.      my $i;
  154.      my $dir;
  155.       for ($i=0;$i<@dirs;$i++) {
  156.           $dir = $dirs[$i];
  157.           ($dir = VMS::Filespec::unixpath($dir)) =~ s!/$!! if $Is_VMS;
  158.          if ((    $ret = minus_f_nocase "$dir/$s.pod")
  159.          or ( $ret = minus_f_nocase "$dir/$s.pm"  and containspod($ret))
  160.          or ( $ret = minus_f_nocase "$dir/$s"     and containspod($ret))
  161.           or ( $Is_VMS and 
  162.               $ret = minus_f_nocase "$dir/$s.com" and containspod($ret))
  163.         or ( $^O eq 'os2' and 
  164.               $ret = minus_f_nocase "$dir/$s.cmd" and containspod($ret))
  165.         or ( ($Is_MSWin32 or $^O eq 'os2') and 
  166.               $ret = minus_f_nocase "$dir/$s.bat" and containspod($ret))
  167.          or ( $ret = minus_f_nocase "$dir/pod/$s.pod")
  168.          or ( $ret = minus_f_nocase "$dir/pod/$s" and containspod($ret)))
  169.          { return $ret; }
  170.          
  171.          if($recurse) {
  172.             opendir(D,$dir);
  173.             my(@newdirs) = grep(-d,map("$dir/$_",grep(!/^\.\.?$/,readdir(D))));
  174.             closedir(D);
  175.             @newdirs = map((s/.dir$//,$_)[1],@newdirs) if $Is_VMS;
  176.             next unless @newdirs;
  177.             print STDERR "Also looking in @newdirs\n" if $opt_v;
  178.             push(@dirs,@newdirs);
  179.          }
  180.      }
  181.       return ();
  182.   }
  183.  
  184.  
  185. foreach (@pages) {
  186.     print STDERR "Searching for $_\n" if $opt_v;
  187.     # We must look both in @INC for library modules and in PATH
  188.     # for executables, like h2xs or perldoc itself.
  189.     @searchdirs = @INC;
  190.     unless ($opt_m) { 
  191.         if ($Is_VMS) {
  192.         my($i,$trn);
  193.         for ($i = 0; $trn = $ENV{'DCL$PATH'.$i}; $i++) {
  194.             push(@searchdirs,$trn);
  195.         }
  196.         } else {
  197.             push(@searchdirs, grep(-d, split($Config{path_sep}, 
  198.                          $ENV{'PATH'})));
  199.         }
  200.         @files= searchfor(0,$_,@searchdirs);
  201.     }
  202.     if( @files ) {
  203.         print STDERR "Found as @files\n" if $opt_v;
  204.     } else {
  205.         # no match, try recursive search
  206.         
  207.         @searchdirs = grep(!/^\.$/,@INC);
  208.         
  209.         
  210.         @files= searchfor(1,$_,@searchdirs);
  211.         if( @files ) {
  212.             print STDERR "Loosely found as @files\n" if $opt_v;
  213.         } else {
  214.             print STDERR "No documentation found for '$_'\n";
  215.         }
  216.     }
  217.     push(@found,@files);
  218. }
  219.  
  220. if(!@found) {
  221.     exit ($Is_VMS ? 98962 : 1);
  222. }
  223.  
  224. if ($opt_l) {
  225.     print join("\n", @found), "\n";
  226.     exit;
  227. }
  228.  
  229. if( ! -t STDOUT ) { $no_tty = 1 }
  230.  
  231. if ($Is_MSWin32) {
  232.     $tmp = "$ENV{TEMP}\\perldoc1.$$";
  233.     push @pagers, qw( more< less notepad );
  234.     unshift @pagers, $ENV{PAGER}  if $ENV{PAGER};
  235. } elsif ($Is_VMS) {
  236.     $tmp = 'Sys$Scratch:perldoc.tmp1_'.$$;
  237.     push @pagers, qw( most more less type/page );
  238. } else {
  239.     if ($^O eq 'os2') {
  240.       require POSIX;
  241.       $tmp = POSIX::tmpnam();
  242.     } else {
  243.       $tmp = "/tmp/perldoc1.$$";      
  244.     }
  245.     push @pagers, qw( more less pg view cat );
  246.     unshift @pagers, $ENV{PAGER}  if $ENV{PAGER};
  247. }
  248. unshift @pagers, $ENV{PERLDOC_PAGER} if $ENV{PERLDOC_PAGER};
  249.  
  250. if ($opt_m) {
  251.     foreach $pager (@pagers) {
  252.         system("$pager @found") or exit;
  253.     }
  254.     if ($Is_VMS) { eval 'use vmsish qw(status exit); exit $?' }
  255.     exit 1;
  256.  
  257. if ($opt_f) {
  258.    my $perlfunc = shift @found;
  259.    open(PFUNC, $perlfunc) or die "Can't open $perlfunc: $!";
  260.  
  261.    # Skip introduction
  262.    while (<PFUNC>) {
  263.        last if /^=head2 Alphabetical Listing of Perl Functions/;
  264.    }
  265.  
  266.    # Look for our function
  267.    my $found = 0;
  268.    while (<PFUNC>) {
  269.        if (/^=item\s+\Q$opt_f\E\b/o)  {
  270.        $found++;
  271.        } elsif (/^=item/) {
  272.        last if $found;
  273.        }
  274.        push(@pod, $_) if $found;
  275.    }
  276.    if (@pod) {
  277.        if ($opt_t) {
  278.        open(FORMATTER, "| pod2text") || die "Can't start filter";
  279.        print FORMATTER "=over 8\n\n";
  280.        print FORMATTER @pod;
  281.        print FORMATTER "=back\n";
  282.        close(FORMATTER);
  283.        } else {
  284.        print @pod;
  285.        }
  286.    } else {
  287.        die "No documentation for perl function `$opt_f' found\n";
  288.    }
  289.    exit;
  290. }
  291.  
  292. foreach (@found) {
  293.  
  294.     if($opt_t) {
  295.         open(TMP,">>$tmp");
  296.         Pod::Text::pod2text($_,*TMP);
  297.         close(TMP);
  298.     } elsif(not $opt_u) {
  299.         my $cmd = "pod2man --lax $_ | nroff -man";
  300.         $cmd .= " | col -x" if $^O =~ /hpux/;
  301.         $rslt = `$cmd`;
  302.         unless(($err = $?)) {
  303.             open(TMP,">>$tmp");
  304.             print TMP $rslt;
  305.             close TMP;
  306.         }
  307.     }
  308.                                                     
  309.     if( $opt_u or $err or -z $tmp) {
  310.         open(OUT,">>$tmp");
  311.         open(IN,"<$_");
  312.         $cut = 1;
  313.         while (<IN>) {
  314.             $cut = $1 eq 'cut' if /^=(\w+)/;
  315.             next if $cut;
  316.             print OUT;
  317.         }
  318.         close(IN);
  319.         close(OUT);
  320.     }
  321. }
  322.  
  323. if( $no_tty ) {
  324.     open(TMP,"<$tmp");
  325.     print while <TMP>;
  326.     close(TMP);
  327. } else {
  328.     foreach $pager (@pagers) {
  329.         system("$pager $tmp") or last;
  330.     }
  331. }
  332.  
  333. 1 while unlink($tmp); #Possibly pointless VMSism
  334.  
  335. exit 0;
  336.  
  337. __END__
  338.  
  339. =head1 NAME
  340.  
  341. perldoc - Look up Perl documentation in pod format.
  342.  
  343. =head1 SYNOPSIS
  344.  
  345. B<perldoc> [B<-h>] [B<-v>] [B<-t>] [B<-u>] [B<-m>] [B<-l>] PageName|ModuleName|ProgramName
  346.  
  347. B<perldoc> B<-f> BuiltinFunction
  348.  
  349. =head1 DESCRIPTION
  350.  
  351. I<perldoc> looks up a piece of documentation in .pod format that is embedded
  352. in the perl installation tree or in a perl script, and displays it via
  353. C<pod2man | nroff -man | $PAGER>. (In addition, if running under HP-UX,
  354. C<col -x> will be used.) This is primarily used for the documentation for
  355. the perl library modules.
  356.  
  357. Your system may also have man pages installed for those modules, in
  358. which case you can probably just use the man(1) command.
  359.  
  360. =head1 OPTIONS
  361.  
  362. =over 5
  363.  
  364. =item B<-h> help
  365.  
  366. Prints out a brief help message.
  367.  
  368. =item B<-v> verbose
  369.  
  370. Describes search for the item in detail.
  371.  
  372. =item B<-t> text output
  373.  
  374. Display docs using plain text converter, instead of nroff. This may be faster,
  375. but it won't look as nice.
  376.  
  377. =item B<-u> unformatted
  378.  
  379. Find docs only; skip reformatting by pod2*
  380.  
  381. =item B<-m> module
  382.  
  383. Display the entire module: both code and unformatted pod documentation.
  384. This may be useful if the docs don't explain a function in the detail
  385. you need, and you'd like to inspect the code directly; perldoc will find
  386. the file for you and simply hand it off for display.
  387.  
  388. =item B<-l> file name only
  389.  
  390. Display the file name of the module found.
  391.  
  392. =item B<-f> perlfunc
  393.  
  394. The B<-f> option followed by the name of a perl built in function will
  395. extract the documentation of this function from L<perlfunc>.
  396.  
  397. =item B<PageName|ModuleName|ProgramName>
  398.  
  399. The item you want to look up.  Nested modules (such as C<File::Basename>)
  400. are specified either as C<File::Basename> or C<File/Basename>.  You may also
  401. give a descriptive name of a page, such as C<perlfunc>. You make also give a
  402. partial or wrong-case name, such as "basename" for "File::Basename", but
  403. this will be slower, if there is more then one page with the same partial
  404. name, you will only get the first one.
  405.  
  406. =back
  407.  
  408. =head1 ENVIRONMENT
  409.  
  410. Any switches in the C<PERLDOC> environment variable will be used before the 
  411. command line arguments.  C<perldoc> also searches directories
  412. specified by the C<PERL5LIB> (or C<PERLLIB> if C<PERL5LIB> is not
  413. defined) and C<PATH> environment variables.
  414. (The latter is so that embedded pods for executables, such as
  415. C<perldoc> itself, are available.)
  416.  
  417. =head1 AUTHOR
  418.  
  419. Kenneth Albanowski <kjahds@kjahds.com>
  420.  
  421. Minor updates by Andy Dougherty <doughera@lafcol.lafayette.edu>
  422.  
  423. =cut
  424.  
  425. #
  426. # Version 1.12: Sat Apr 12 22:41:09 EST 1997
  427. #       Gurusamy Sarathy <gsar@umich.edu>
  428. #    -various fixes for win32
  429. # Version 1.11: Tue Dec 26 09:54:33 EST 1995
  430. #       Kenneth Albanowski <kjahds@kjahds.com>
  431. #   -added Charles Bailey's further VMS patches, and -u switch
  432. #   -added -t switch, with pod2text support
  433. # Version 1.10: Thu Nov  9 07:23:47 EST 1995
  434. #        Kenneth Albanowski <kjahds@kjahds.com>
  435. #    -added VMS support
  436. #    -added better error recognition (on no found pages, just exit. On
  437. #     missing nroff/pod2man, just display raw pod.)
  438. #    -added recursive/case-insensitive matching (thanks, Andreas). This
  439. #     slows things down a bit, unfortunately. Give a precise name, and
  440. #     it'll run faster.
  441. #
  442. # Version 1.01:    Tue May 30 14:47:34 EDT 1995
  443. #        Andy Dougherty  <doughera@lafcol.lafayette.edu>
  444. #   -added pod documentation.
  445. #   -added PATH searching.
  446. #   -added searching pod/ subdirectory (mainly to pick up perlfunc.pod
  447. #    and friends.
  448. #
  449. #
  450. # TODO:
  451. #
  452. #    Cache directories read during sloppy match
  453. __END__
  454. :endofperl
  455.