$Bill Luebkert Sent on December 15, 2005 8:29 PM -0800

  >Suresh Govindachar wrote:
  >
  >>   Wanting to capture the output of perldoc with a perl variable,
  >>   I looked at perldoc.bat.  What perldoc.bat does is run the
  >>   following script with the arguments given to perldoc:
  >
  > <snip>
  >
  >>   1) What does the following line do? 
  >> 
  >>     eval 'exec C:\opt\perl\bin\perl.exe -S $0 ${1+"$@"}'
  >>         if 0;
  >
  > That's just running perl against the bat file.
  
  But doesn't the if 0 condition mean that nothing happens?
  (The call to perl to run the bat file from the shebang line 
  onwards happens with a dos-cmd line).

  >>     As far as I know, that line does nothing.
  >> 
  >>   2) The lines that get the required documentation are:
  >> 
  >>           use Pod::Perldoc;
  >>           exit( Pod::Perldoc->run() );
  >
  > Right - once you run perl on the file instead of cmd.exe.
  >
  >>      I guess Pod::Perldoc->run() figures out what the user
  >>      wants to look up by examining argv/argc, correct? 
  >> 
  >>      Pod::Perldoc->run() returns success or failure, not the
  >>      documentation.  The documentation just gets printed. 
  >>      So I need to fool the code into printing to a variable.
  >>      How would this be done?
  >> 
  >>   3) Any better way to look up documentation into a perl variable?
  >
  > Try something like (you may have to add paths if not on your path):
  >
  > my @lines = `perldoc -f waitpid`;
  > print "Lines:[EMAIL PROTECTED]";
  
  I am hoping to be able to avoid the system call by something like 
  tieing STDOUT and STDERR to a variable.

  (Attached below is some code $Bill had provided 
  on Mon, 14 Mar 2005 17:59:58 -0800).

  --Suresh

  This should handle it (minimally tested - modify to suit) :
  
  #!perl --
  
  use strict;
  use warnings;
  
  our $argv = join ' ', @ARGV, ' ';     # save orig args
  our %A;               # get commandline switches into %A
  for (my $ii = 0; $ii < @ARGV; ) {
        last if $ARGV[$ii] =~ /^--$/;
        if ($ARGV[$ii] !~ /^-{1,2}(.*)$/) { $ii++; next; }
        my $arg = $1; splice @ARGV, $ii, 1;
        if ($arg =~ /^([\w]+)=(.*)$/) { $A{$1} = $2; } else { $A{$1}++; }
  }
  my $debug = $A{d} || 0;
  
  (my $prog = $0) =~ s/^.*[\\\/]//;
  my $usage = <<EOD;
  
  Usage: $prog [-k] [<options>] <arg-list>
  
        -k      if present perform the following logic, else call perldoc
                with <arglist> and return result of first successful :
                        "perldoc <options> arg-list"
                        "perldoc <options> -f arg"
                        "perldoc <options> -q arg"
                or return failure message
  
  EOD
  
  $argv =~ s/\s*-[dk]\s+/ /g; $argv =~ s/\s\s+/ /g;
  print "argv=$argv\n" if $debug;
  
  if (not $A{k}) {
        print `perldoc $argv`;
  } else {
  
        # if "perldoc [options] arg" succeeds, returns it;
  
        my $res = `perldoc $argv 2>&1`;
        print $res and exit if $? == 0;
        print "results=$res\n" if $debug;
  
        # else if "perldoc [options] -f arg" succeeds, returns it;
  
        if ($argv !~ /\s*-f\s+/) {
                $res = `perldoc -f $argv 2>&1`;
                print $res and exit if $? == 0;
                print "results=$res\n" if $debug;
        }
  
        # else if "perldoc [options] -q arg" succeeds, returns it;
  
        if ($argv !~ /\s*-q\s+/) {
                $res = `perldoc -q $argv 2>&1`;
                print $res and exit if $? == 0;
                print "results=$res\n" if $debug;
        }
  
        # else returns failure message.
  
        print "Error finding any perldoc results\n";
  }
  
  __END__
  

_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to