Blame README

Packit 258f23
NAME
Packit 258f23
    IPC::Run - system() and background procs w/ piping, redirs, ptys (Unix,
Packit 258f23
    Win32)
Packit 258f23
Packit 258f23
SYNOPSIS
Packit 258f23
       ## First,a command to run:
Packit 258f23
          my @cat = qw( cat );
Packit 258f23
Packit 258f23
       ## Using run() instead of system():
Packit 258f23
          use IPC::Run qw( run timeout );
Packit 258f23
Packit 258f23
          run \@cat, \$in, \$out, \$err, timeout( 10 ) or die "cat: $?"
Packit 258f23
Packit 258f23
          # Can do I/O to sub refs and filenames, too:
Packit 258f23
          run \@cat, '<', "in.txt", \&out, \&err or die "cat: $?"
Packit 258f23
          run \@cat, '<', "in.txt", '>>', "out.txt", '2>>', "err.txt";
Packit 258f23
Packit 258f23
Packit 258f23
          # Redirecting using pseudo-terminals instead of pipes.
Packit 258f23
          run \@cat, '<pty<', \$in,  '>pty>', \$out_and_err;
Packit 258f23
Packit 258f23
       ## Scripting subprocesses (like Expect):
Packit 258f23
Packit 258f23
          use IPC::Run qw( start pump finish timeout );
Packit 258f23
Packit 258f23
          # Incrementally read from / write to scalars. 
Packit 258f23
          # $in is drained as it is fed to cat's stdin,
Packit 258f23
          # $out accumulates cat's stdout
Packit 258f23
          # $err accumulates cat's stderr
Packit 258f23
          # $h is for "harness".
Packit 258f23
          my $h = start \@cat, \$in, \$out, \$err, timeout( 10 );
Packit 258f23
Packit 258f23
          $in .= "some input\n";
Packit 258f23
          pump $h until $out =~ /input\n/g;
Packit 258f23
Packit 258f23
          $in .= "some more input\n";
Packit 258f23
          pump $h until $out =~ /\G.*more input\n/;
Packit 258f23
Packit 258f23
          $in .= "some final input\n";
Packit 258f23
          finish $h or die "cat returned $?";
Packit 258f23
Packit 258f23
          warn $err if $err; 
Packit 258f23
          print $out;         ## All of cat's output
Packit 258f23
Packit 258f23
       # Piping between children
Packit 258f23
          run \@cat, '|', \@gzip;
Packit 258f23
Packit 258f23
       # Multiple children simultaneously (run() blocks until all
Packit 258f23
       # children exit, use start() for background execution):
Packit 258f23
          run \@foo1, '&', \@foo2;
Packit 258f23
Packit 258f23
       # Calling \&set_up_child in the child before it executes the
Packit 258f23
       # command (only works on systems with true fork() & exec())
Packit 258f23
       # exceptions thrown in set_up_child() will be propagated back
Packit 258f23
       # to the parent and thrown from run().
Packit 258f23
          run \@cat, \$in, \$out,
Packit 258f23
             init => \&set_up_child;
Packit 258f23
Packit 258f23
       # Read from / write to file handles you open and close
Packit 258f23
          open IN,  '
Packit 258f23
          open OUT, '>out.txt' or die $!;
Packit 258f23
          print OUT "preamble\n";
Packit 258f23
          run \@cat, \*IN, \*OUT or die "cat returned $?";
Packit 258f23
          print OUT "postamble\n";
Packit 258f23
          close IN;
Packit 258f23
          close OUT;
Packit 258f23
Packit 258f23
       # Create pipes for you to read / write (like IPC::Open2 & 3).
Packit 258f23
          $h = start
Packit 258f23
             \@cat,
Packit 258f23
                '
Packit 258f23
                '>pipe', \*OUT,
Packit 258f23
                '2>pipe', \*ERR 
Packit 258f23
             or die "cat returned $?";
Packit 258f23
          print IN "some input\n";
Packit 258f23
          close IN;
Packit 258f23
          print <OUT>, <ERR>;
Packit 258f23
          finish $h;
Packit 258f23
Packit 258f23
       # Mixing input and output modes
Packit 258f23
          run \@cat, 'in.txt', \&catch_some_out, \*ERR_LOG );
Packit 258f23
Packit 258f23
       # Other redirection constructs
Packit 258f23
          run \@cat, '>&', \$out_and_err;
Packit 258f23
          run \@cat, '2>&1;;
Packit 258f23
          run \@cat, '0<&3;;
Packit 258f23
          run \@cat, '<&-;;
Packit 258f23
          run \@cat, '3<', \$in3;
Packit 258f23
          run \@cat, '4>', \$out4;
Packit 258f23
          # etc.
Packit 258f23
Packit 258f23
       # Passing options:
Packit 258f23
          run \@cat, 'in.txt', debug => 1;
Packit 258f23
Packit 258f23
       # Call this system's shell, returns TRUE on 0 exit code
Packit 258f23
       # THIS IS THE OPPOSITE SENSE OF system()'s RETURN VALUE
Packit 258f23
          run "cat a b c" or die "cat returned $?";
Packit 258f23
Packit 258f23
       # Launch a sub process directly, no shell.  Can't do redirection
Packit 258f23
       # with this form, it's here to behave like system() with an
Packit 258f23
       # inverted result.
Packit 258f23
          $r = run "cat a b c";
Packit 258f23
Packit 258f23
       # Read from a file in to a scalar
Packit 258f23
          run io( "filename", 'r', \$recv );
Packit 258f23
          run io( \*HANDLE,   'r', \$recv );
Packit 258f23
Packit 258f23
DESCRIPTION
Packit 258f23
    IPC::Run allows you to run and interact with child processes using
Packit 258f23
    files, pipes, and pseudo-ttys. Both system()-style and scripted usages
Packit 258f23
    are supported and may be mixed. Likewise, functional and OO API styles
Packit 258f23
    are both supported and may be mixed.
Packit 258f23
Packit 258f23
    Various redirection operators reminiscent of those seen on common Unix
Packit 258f23
    and DOS command lines are provided.
Packit 258f23
Packit 258f23
    Before digging in to the details a few LIMITATIONS are important enough
Packit 258f23
    to be mentioned right up front:
Packit 258f23
Packit 258f23
    Win32 Support
Packit 258f23
        Win32 support is working but EXPERIMENTAL, but does pass all
Packit 258f23
        relevant tests on NT 4.0. See "Win32 LIMITATIONS".
Packit 258f23
Packit 258f23
    pty Support
Packit 258f23
        If you need pty support, IPC::Run should work well enough most of
Packit 258f23
        the time, but IO::Pty is being improved, and IPC::Run will be
Packit 258f23
        improved to use IO::Pty's new features when it is release.
Packit 258f23
Packit 258f23
        The basic problem is that the pty needs to initialize itself before
Packit 258f23
        the parent writes to the master pty, or the data written gets lost.
Packit 258f23
        So IPC::Run does a sleep(1) in the parent after forking to
Packit 258f23
        (hopefully) give the child a chance to run. This is a kludge that
Packit 258f23
        works well on non heavily loaded systems :(.
Packit 258f23
Packit 258f23
        ptys are not supported yet under Win32, but will be emulated...
Packit 258f23
Packit 258f23
    Debugging Tip
Packit 258f23
        You may use the environment variable "IPCRUNDEBUG" to see what's
Packit 258f23
        going on under the hood:
Packit 258f23
Packit 258f23
           $ IPCRUNDEBUG=basic   myscript     # prints minimal debugging
Packit 258f23
           $ IPCRUNDEBUG=data    myscript     # prints all data reads/writes
Packit 258f23
           $ IPCRUNDEBUG=details myscript     # prints lots of low-level details
Packit 258f23
           $ IPCRUNDEBUG=gory    myscript     # (Win32 only) prints data moving through
Packit 258f23
                                              # the helper processes.
Packit 258f23
Packit 258f23
    We now return you to your regularly scheduled documentation.
Packit 258f23
Packit 258f23
  Harnesses
Packit 258f23
    Child processes and I/O handles are gathered in to a harness, then
Packit 258f23
    started and run until the processing is finished or aborted.
Packit 258f23
Packit 258f23
  run() vs. start(); pump(); finish();
Packit 258f23
    There are two modes you can run harnesses in: run() functions as an
Packit 258f23
    enhanced system(), and start()/pump()/finish() allow for background
Packit 258f23
    processes and scripted interactions with them.
Packit 258f23
Packit 258f23
    When using run(), all data to be sent to the harness is set up in
Packit 258f23
    advance (though one can feed subprocesses input from subroutine refs to
Packit 258f23
    get around this limitation). The harness is run and all output is
Packit 258f23
    collected from it, then any child processes are waited for:
Packit 258f23
Packit 258f23
       run \@cmd, \<
Packit 258f23
       blah
Packit 258f23
       IN
Packit 258f23
Packit 258f23
       ## To precompile harnesses and run them later:
Packit 258f23
       my $h = harness \@cmd, \<
Packit 258f23
       blah
Packit 258f23
       IN
Packit 258f23
Packit 258f23
       run $h;
Packit 258f23
Packit 258f23
    The background and scripting API is provided by start(), pump(), and
Packit 258f23
    finish(): start() creates a harness if need be (by calling harness())
Packit 258f23
    and launches any subprocesses, pump() allows you to poll them for
Packit 258f23
    activity, and finish() then monitors the harnessed activities until they
Packit 258f23
    complete.
Packit 258f23
Packit 258f23
       ## Build the harness, open all pipes, and launch the subprocesses
Packit 258f23
       my $h = start \@cat, \$in, \$out;
Packit 258f23
       $in = "first input\n";
Packit 258f23
Packit 258f23
       ## Now do I/O.  start() does no I/O.
Packit 258f23
       pump $h while length $in;  ## Wait for all input to go
Packit 258f23
Packit 258f23
       ## Now do some more I/O.
Packit 258f23
       $in = "second input\n";
Packit 258f23
       pump $h until $out =~ /second input/;
Packit 258f23
Packit 258f23
       ## Clean up
Packit 258f23
       finish $h or die "cat returned $?";
Packit 258f23
Packit 258f23
    You can optionally compile the harness with harness() prior to
Packit 258f23
    start()ing or run()ing, and you may omit start() between harness() and
Packit 258f23
    pump(). You might want to do these things if you compile your harnesses
Packit 258f23
    ahead of time.
Packit 258f23
Packit 258f23
  Using regexps to match output
Packit 258f23
    As shown in most of the scripting examples, the read-to-scalar facility
Packit 258f23
    for gathering subcommand's output is often used with regular expressions
Packit 258f23
    to detect stopping points. This is because subcommand output often
Packit 258f23
    arrives in dribbles and drabs, often only a character or line at a time.
Packit 258f23
    This output is input for the main program and piles up in variables like
Packit 258f23
    the $out and $err in our examples.
Packit 258f23
Packit 258f23
    Regular expressions can be used to wait for appropriate output in
Packit 258f23
    several ways. The "cat" example in the previous section demonstrates how
Packit 258f23
    to pump() until some string appears in the output. Here's an example
Packit 258f23
    that uses "smb" to fetch files from a remote server:
Packit 258f23
Packit 258f23
       $h = harness \@smbclient, \$in, \$out;
Packit 258f23
Packit 258f23
       $in = "cd /src\n";
Packit 258f23
       $h->pump until $out =~ /^smb.*> \Z/m;
Packit 258f23
       die "error cding to /src:\n$out" if $out =~ "ERR";
Packit 258f23
       $out = '';
Packit 258f23
Packit 258f23
       $in = "mget *\n";
Packit 258f23
       $h->pump until $out =~ /^smb.*> \Z/m;
Packit 258f23
       die "error retrieving files:\n$out" if $out =~ "ERR";
Packit 258f23
Packit 258f23
       $in = "quit\n";
Packit 258f23
       $h->finish;
Packit 258f23
Packit 258f23
    Notice that we carefully clear $out after the first command/response
Packit 258f23
    cycle? That's because IPC::Run does not delete $out when we continue,
Packit 258f23
    and we don't want to trip over the old output in the second
Packit 258f23
    command/response cycle.
Packit 258f23
Packit 258f23
    Say you want to accumulate all the output in $out and analyze it
Packit 258f23
    afterwards. Perl offers incremental regular expression matching using
Packit 258f23
    the "m//gc" and pattern matching idiom and the "\G" assertion. IPC::Run
Packit 258f23
    is careful not to disturb the current "pos()" value for scalars it
Packit 258f23
    appends data to, so we could modify the above so as not to destroy $out
Packit 258f23
    by adding a couple of "/gc" modifiers. The "/g" keeps us from tripping
Packit 258f23
    over the previous prompt and the "/c" keeps us from resetting the prior
Packit 258f23
    match position if the expected prompt doesn't materialize immediately:
Packit 258f23
Packit 258f23
       $h = harness \@smbclient, \$in, \$out;
Packit 258f23
Packit 258f23
       $in = "cd /src\n";
Packit 258f23
       $h->pump until $out =~ /^smb.*> \Z/mgc;
Packit 258f23
       die "error cding to /src:\n$out" if $out =~ "ERR";
Packit 258f23
Packit 258f23
       $in = "mget *\n";
Packit 258f23
       $h->pump until $out =~ /^smb.*> \Z/mgc;
Packit 258f23
       die "error retrieving files:\n$out" if $out =~ "ERR";
Packit 258f23
Packit 258f23
       $in = "quit\n";
Packit 258f23
       $h->finish;
Packit 258f23
Packit 258f23
       analyze( $out );
Packit 258f23
Packit 258f23
    When using this technique, you may want to preallocate $out to have
Packit 258f23
    plenty of memory or you may find that the act of growing $out each time
Packit 258f23
    new input arrives causes an "O(length($out)^2)" slowdown as $out grows.
Packit 258f23
    Say we expect no more than 10,000 characters of input at the most. To
Packit 258f23
    preallocate memory to $out, do something like:
Packit 258f23
Packit 258f23
       my $out = "x" x 10_000;
Packit 258f23
       $out = "";
Packit 258f23
Packit 258f23
    "perl" will allocate at least 10,000 characters' worth of space, then
Packit 258f23
    mark the $out as having 0 length without freeing all that yummy RAM.
Packit 258f23
Packit 258f23
  Timeouts and Timers
Packit 258f23
    More than likely, you don't want your subprocesses to run forever, and
Packit 258f23
    sometimes it's nice to know that they're going a little slowly. Timeouts
Packit 258f23
    throw exceptions after a some time has elapsed, timers merely cause
Packit 258f23
    pump() to return after some time has elapsed. Neither is reset/restarted
Packit 258f23
    automatically.
Packit 258f23
Packit 258f23
    Timeout objects are created by calling timeout( $interval ) and passing
Packit 258f23
    the result to run(), start() or harness(). The timeout period starts
Packit 258f23
    ticking just after all the child processes have been fork()ed or
Packit 258f23
    spawn()ed, and are polled for expiration in run(), pump() and finish().
Packit 258f23
    If/when they expire, an exception is thrown. This is typically useful to
Packit 258f23
    keep a subprocess from taking too long.
Packit 258f23
Packit 258f23
    If a timeout occurs in run(), all child processes will be terminated and
Packit 258f23
    all file/pipe/ptty descriptors opened by run() will be closed. File
Packit 258f23
    descriptors opened by the parent process and passed in to run() are not
Packit 258f23
    closed in this event.
Packit 258f23
Packit 258f23
    If a timeout occurs in pump(), pump_nb(), or finish(), it's up to you to
Packit 258f23
    decide whether to kill_kill() all the children or to implement some more
Packit 258f23
    graceful fallback. No I/O will be closed in pump(), pump_nb() or
Packit 258f23
    finish() by such an exception (though I/O is often closed down in those
Packit 258f23
    routines during the natural course of events).
Packit 258f23
Packit 258f23
    Often an exception is too harsh. timer( $interval ) creates timer
Packit 258f23
    objects that merely prevent pump() from blocking forever. This can be
Packit 258f23
    useful for detecting stalled I/O or printing a soothing message or "."
Packit 258f23
    to pacify an anxious user.
Packit 258f23
Packit 258f23
    Timeouts and timers can both be restarted at any time using the timer's
Packit 258f23
    start() method (this is not the start() that launches subprocesses). To
Packit 258f23
    restart a timer, you need to keep a reference to the timer:
Packit 258f23
Packit 258f23
       ## Start with a nice long timeout to let smbclient connect.  If
Packit 258f23
       ## pump or finish take too long, an exception will be thrown.
Packit 258f23
Packit 258f23
     my $h;
Packit 258f23
     eval {
Packit 258f23
       $h = harness \@smbclient, \$in, \$out, \$err, ( my $t = timeout 30 );
Packit 258f23
       sleep 11;  # No effect: timer not running yet
Packit 258f23
Packit 258f23
       start $h;
Packit 258f23
       $in = "cd /src\n";
Packit 258f23
       pump $h until ! length $in;
Packit 258f23
Packit 258f23
       $in = "ls\n";
Packit 258f23
       ## Now use a short timeout, since this should be faster
Packit 258f23
       $t->start( 5 );
Packit 258f23
       pump $h until ! length $in;
Packit 258f23
Packit 258f23
       $t->start( 10 );  ## Give smbclient a little while to shut down.
Packit 258f23
       $h->finish;
Packit 258f23
     };
Packit 258f23
     if ( $@ ) {
Packit 258f23
       my $x = $@;    ## Preserve $@ in case another exception occurs
Packit 258f23
       $h->kill_kill; ## kill it gently, then brutally if need be, or just
Packit 258f23
                       ## brutally on Win32.
Packit 258f23
       die $x;
Packit 258f23
     }
Packit 258f23
Packit 258f23
    Timeouts and timers are *not* checked once the subprocesses are shut
Packit 258f23
    down; they will not expire in the interval between the last valid
Packit 258f23
    process and when IPC::Run scoops up the processes' result codes, for
Packit 258f23
    instance.
Packit 258f23
Packit 258f23
  Spawning synchronization, child exception propagation
Packit 258f23
    start() pauses the parent until the child executes the command or CODE
Packit 258f23
    reference and propagates any exceptions thrown (including exec()
Packit 258f23
    failure) back to the parent. This has several pleasant effects: any
Packit 258f23
    exceptions thrown in the child, including exec() failure, come flying
Packit 258f23
    out of start() or run() as though they had occurred in the parent.
Packit 258f23
Packit 258f23
    This includes exceptions your code thrown from init subs. In this
Packit 258f23
    example:
Packit 258f23
Packit 258f23
       eval {
Packit 258f23
          run \@cmd, init => sub { die "blast it! foiled again!" };
Packit 258f23
       };
Packit 258f23
       print $@;
Packit 258f23
Packit 258f23
    the exception "blast it! foiled again" will be thrown from the child
Packit 258f23
    process (preventing the exec()) and printed by the parent.
Packit 258f23
Packit 258f23
    In situations like
Packit 258f23
Packit 258f23
       run \@cmd1, "|", \@cmd2, "|", \@cmd3;
Packit 258f23
Packit 258f23
    @cmd1 will be initted and exec()ed before @cmd2, and @cmd2 before @cmd3.
Packit 258f23
    This can save time and prevent oddball errors emitted by later commands
Packit 258f23
    when earlier commands fail to execute. Note that IPC::Run doesn't start
Packit 258f23
    any commands unless it can find the executables referenced by all
Packit 258f23
    commands. These executables must pass both the "-f" and "-x" tests
Packit 258f23
    described in perlfunc.
Packit 258f23
Packit 258f23
    Another nice effect is that init() subs can take their time doing things
Packit 258f23
    and there will be no problems caused by a parent continuing to execute
Packit 258f23
    before a child's init() routine is complete. Say the init() routine
Packit 258f23
    needs to open a socket or a temp file that the parent wants to connect
Packit 258f23
    to; without this synchronization, the parent will need to implement a
Packit 258f23
    retry loop to wait for the child to run, since often, the parent gets a
Packit 258f23
    lot of things done before the child's first timeslice is allocated.
Packit 258f23
Packit 258f23
    This is also quite necessary for pseudo-tty initialization, which needs
Packit 258f23
    to take place before the parent writes to the child via pty. Writes that
Packit 258f23
    occur before the pty is set up can get lost.
Packit 258f23
Packit 258f23
    A final, minor, nicety is that debugging output from the child will be
Packit 258f23
    emitted before the parent continues on, making for much clearer
Packit 258f23
    debugging output in complex situations.
Packit 258f23
Packit 258f23
    The only drawback I can conceive of is that the parent can't continue to
Packit 258f23
    operate while the child is being initted. If this ever becomes a problem
Packit 258f23
    in the field, we can implement an option to avoid this behavior, but I
Packit 258f23
    don't expect it to.
Packit 258f23
Packit 258f23
    Win32: executing CODE references isn't supported on Win32, see "Win32
Packit 258f23
    LIMITATIONS" for details.
Packit 258f23
Packit 258f23
  Syntax
Packit 258f23
    run(), start(), and harness() can all take a harness specification as
Packit 258f23
    input. A harness specification is either a single string to be passed to
Packit 258f23
    the systems' shell:
Packit 258f23
Packit 258f23
       run "echo 'hi there'";
Packit 258f23
Packit 258f23
    or a list of commands, io operations, and/or timers/timeouts to execute.
Packit 258f23
    Consecutive commands must be separated by a pipe operator '|' or an '&'.
Packit 258f23
    External commands are passed in as array references, and, on systems
Packit 258f23
    supporting fork(), Perl code may be passed in as subs:
Packit 258f23
Packit 258f23
       run \@cmd;
Packit 258f23
       run \@cmd1, '|', \@cmd2;
Packit 258f23
       run \@cmd1, '&', \@cmd2;
Packit 258f23
       run \&sub1;
Packit 258f23
       run \&sub1, '|', \&sub2;
Packit 258f23
       run \&sub1, '&', \&sub2;
Packit 258f23
Packit 258f23
    '|' pipes the stdout of \@cmd1 the stdin of \@cmd2, just like a shell
Packit 258f23
    pipe. '&' does not. Child processes to the right of a '&' will have
Packit 258f23
    their stdin closed unless it's redirected-to.
Packit 258f23
Packit 258f23
    IPC::Run::IO objects may be passed in as well, whether or not child
Packit 258f23
    processes are also specified:
Packit 258f23
Packit 258f23
       run io( "infile", ">", \$in ), io( "outfile", "<", \$in );
Packit 258f23
Packit 258f23
    as can IPC::Run::Timer objects:
Packit 258f23
Packit 258f23
       run \@cmd, io( "outfile", "<", \$in ), timeout( 10 );
Packit 258f23
Packit 258f23
    Commands may be followed by scalar, sub, or i/o handle references for
Packit 258f23
    redirecting child process input & output:
Packit 258f23
Packit 258f23
       run \@cmd,  \undef,            \$out;
Packit 258f23
       run \@cmd,  \$in,              \$out;
Packit 258f23
       run \@cmd1, \&in, '|', \@cmd2, \*OUT;
Packit 258f23
       run \@cmd1, \*IN, '|', \@cmd2, \&out;
Packit 258f23
Packit 258f23
    This is known as succinct redirection syntax, since run(), start() and
Packit 258f23
    harness(), figure out which file descriptor to redirect and how. File
Packit 258f23
    descriptor 0 is presumed to be an input for the child process, all
Packit 258f23
    others are outputs. The assumed file descriptor always starts at 0,
Packit 258f23
    unless the command is being piped to, in which case it starts at 1.
Packit 258f23
Packit 258f23
    To be explicit about your redirects, or if you need to do more complex
Packit 258f23
    things, there's also a redirection operator syntax:
Packit 258f23
Packit 258f23
       run \@cmd, '<', \undef, '>',  \$out;
Packit 258f23
       run \@cmd, '<', \undef, '>&', \$out_and_err;
Packit 258f23
       run(
Packit 258f23
          \@cmd1,
Packit 258f23
             '<', \$in,
Packit 258f23
          '|', \@cmd2,
Packit 258f23
             \$out
Packit 258f23
       );
Packit 258f23
Packit 258f23
    Operator syntax is required if you need to do something other than
Packit 258f23
    simple redirection to/from scalars or subs, like duping or closing file
Packit 258f23
    descriptors or redirecting to/from a named file. The operators are
Packit 258f23
    covered in detail below.
Packit 258f23
Packit 258f23
    After each \@cmd (or \&foo), parsing begins in succinct mode and toggles
Packit 258f23
    to operator syntax mode when an operator (ie plain scalar, not a ref) is
Packit 258f23
    seen. Once in operator syntax mode, parsing only reverts to succinct
Packit 258f23
    mode when a '|' or '&' is seen.
Packit 258f23
Packit 258f23
    In succinct mode, each parameter after the \@cmd specifies what to do
Packit 258f23
    with the next highest file descriptor. These File descriptor start with
Packit 258f23
    0 (stdin) unless stdin is being piped to ("'|', \@cmd"), in which case
Packit 258f23
    they start with 1 (stdout). Currently, being on the left of a pipe
Packit 258f23
    ("\@cmd, \$out, \$err, '|'") does *not* cause stdout to be skipped,
Packit 258f23
    though this may change since it's not as DWIMerly as it could be. Only
Packit 258f23
    stdin is assumed to be an input in succinct mode, all others are assumed
Packit 258f23
    to be outputs.
Packit 258f23
Packit 258f23
    If no piping or redirection is specified for a child, it will inherit
Packit 258f23
    the parent's open file handles as dictated by your system's
Packit 258f23
    close-on-exec behavior and the $^F flag, except that processes after a
Packit 258f23
    '&' will not inherit the parent's stdin. Also note that $^F does not
Packit 258f23
    affect file descriptors obtained via POSIX, since it only applies to
Packit 258f23
    full-fledged Perl file handles. Such processes will have their stdin
Packit 258f23
    closed unless it has been redirected-to.
Packit 258f23
Packit 258f23
    If you want to close a child processes stdin, you may do any of:
Packit 258f23
Packit 258f23
       run \@cmd, \undef;
Packit 258f23
       run \@cmd, \"";
Packit 258f23
       run \@cmd, '<&-;;
Packit 258f23
       run \@cmd, '0<&-;;
Packit 258f23
Packit 258f23
    Redirection is done by placing redirection specifications immediately
Packit 258f23
    after a command or child subroutine:
Packit 258f23
Packit 258f23
       run \@cmd1,      \$in, '|', \@cmd2,      \$out;
Packit 258f23
       run \@cmd1, '<', \$in, '|', \@cmd2, '>', \$out;
Packit 258f23
Packit 258f23
    If you omit the redirection operators, descriptors are counted starting
Packit 258f23
    at 0. Descriptor 0 is assumed to be input, all others are outputs. A
Packit 258f23
    leading '|' consumes descriptor 0, so this works as expected.
Packit 258f23
Packit 258f23
       run \@cmd1, \$in, '|', \@cmd2, \$out;
Packit 258f23
Packit 258f23
    The parameter following a redirection operator can be a scalar ref, a
Packit 258f23
    subroutine ref, a file name, an open filehandle, or a closed filehandle.
Packit 258f23
Packit 258f23
    If it's a scalar ref, the child reads input from or sends output to that
Packit 258f23
    variable:
Packit 258f23
Packit 258f23
       $in = "Hello World.\n";
Packit 258f23
       run \@cat, \$in, \$out;
Packit 258f23
       print $out;
Packit 258f23
Packit 258f23
    Scalars used in incremental (start()/pump()/finish()) applications are
Packit 258f23
    treated as queues: input is removed from input scalers, resulting in
Packit 258f23
    them dwindling to '', and output is appended to output scalars. This is
Packit 258f23
    not true of harnesses run() in batch mode.
Packit 258f23
Packit 258f23
    It's usually wise to append new input to be sent to the child to the
Packit 258f23
    input queue, and you'll often want to zap output queues to '' before
Packit 258f23
    pumping.
Packit 258f23
Packit 258f23
       $h = start \@cat, \$in;
Packit 258f23
       $in = "line 1\n";
Packit 258f23
       pump $h;
Packit 258f23
       $in .= "line 2\n";
Packit 258f23
       pump $h;
Packit 258f23
       $in .= "line 3\n";
Packit 258f23
       finish $h;
Packit 258f23
Packit 258f23
    The final call to finish() must be there: it allows the child
Packit 258f23
    process(es) to run to completion and waits for their exit values.
Packit 258f23
Packit 258f23
OBSTINATE CHILDREN
Packit 258f23
    Interactive applications are usually optimized for human use. This can
Packit 258f23
    help or hinder trying to interact with them through modules like
Packit 258f23
    IPC::Run. Frequently, programs alter their behavior when they detect
Packit 258f23
    that stdin, stdout, or stderr are not connected to a tty, assuming that
Packit 258f23
    they are being run in batch mode. Whether this helps or hurts depends on
Packit 258f23
    which optimizations change. And there's often no way of telling what a
Packit 258f23
    program does in these areas other than trial and error and occasionally,
Packit 258f23
    reading the source. This includes different versions and implementations
Packit 258f23
    of the same program.
Packit 258f23
Packit 258f23
    All hope is not lost, however. Most programs behave in reasonably
Packit 258f23
    tractable manners, once you figure out what it's trying to do.
Packit 258f23
Packit 258f23
    Here are some of the issues you might need to be aware of.
Packit 258f23
Packit 258f23
    *   fflush()ing stdout and stderr
Packit 258f23
Packit 258f23
        This lets the user see stdout and stderr immediately. Many programs
Packit 258f23
        undo this optimization if stdout is not a tty, making them harder to
Packit 258f23
        manage by things like IPC::Run.
Packit 258f23
Packit 258f23
        Many programs decline to fflush stdout or stderr if they do not
Packit 258f23
        detect a tty there. Some ftp commands do this, for instance.
Packit 258f23
Packit 258f23
        If this happens to you, look for a way to force interactive
Packit 258f23
        behavior, like a command line switch or command. If you can't, you
Packit 258f23
        will need to use a pseudo terminal ('<pty<' and '>pty>').
Packit 258f23
Packit 258f23
    *   false prompts
Packit 258f23
Packit 258f23
        Interactive programs generally do not guarantee that output from
Packit 258f23
        user commands won't contain a prompt string. For example, your shell
Packit 258f23
        prompt might be a '$', and a file named '$' might be the only file
Packit 258f23
        in a directory listing.
Packit 258f23
Packit 258f23
        This can make it hard to guarantee that your output parser won't be
Packit 258f23
        fooled into early termination of results.
Packit 258f23
Packit 258f23
        To help work around this, you can see if the program can alter it's
Packit 258f23
        prompt, and use something you feel is never going to occur in actual
Packit 258f23
        practice.
Packit 258f23
Packit 258f23
        You should also look for your prompt to be the only thing on a line:
Packit 258f23
Packit 258f23
           pump $h until $out =~ /^<SILLYPROMPT>\s?\z/m;
Packit 258f23
Packit 258f23
        (use "(?!\n)\Z" in place of "\z" on older perls).
Packit 258f23
Packit 258f23
        You can also take the approach that IPC::ChildSafe takes and emit a
Packit 258f23
        command with known output after each 'real' command you issue, then
Packit 258f23
        look for this known output. See new_appender() and new_chunker() for
Packit 258f23
        filters that can help with this task.
Packit 258f23
Packit 258f23
        If it's not convenient or possibly to alter a prompt or use a known
Packit 258f23
        command/response pair, you might need to autodetect the prompt in
Packit 258f23
        case the local version of the child program is different then the
Packit 258f23
        one you tested with, or if the user has control over the look & feel
Packit 258f23
        of the prompt.
Packit 258f23
Packit 258f23
    *   Refusing to accept input unless stdin is a tty.
Packit 258f23
Packit 258f23
        Some programs, for security reasons, will only accept certain types
Packit 258f23
        of input from a tty. su, notable, will not prompt for a password
Packit 258f23
        unless it's connected to a tty.
Packit 258f23
Packit 258f23
        If this is your situation, use a pseudo terminal ('
Packit 258f23
        '>pty>').
Packit 258f23
Packit 258f23
    *   Not prompting unless connected to a tty.
Packit 258f23
Packit 258f23
        Some programs don't prompt unless stdin or stdout is a tty. See if
Packit 258f23
        you can turn prompting back on. If not, see if you can come up with
Packit 258f23
        a command that you can issue after every real command and look for
Packit 258f23
        it's output, as IPC::ChildSafe does. There are two filters included
Packit 258f23
        with IPC::Run that can help with doing this: appender and chunker
Packit 258f23
        (see new_appender() and new_chunker()).
Packit 258f23
Packit 258f23
    *   Different output format when not connected to a tty.
Packit 258f23
Packit 258f23
        Some commands alter their formats to ease machine parsability when
Packit 258f23
        they aren't connected to a pipe. This is actually good, but can be
Packit 258f23
        surprising.
Packit 258f23
Packit 258f23
PSEUDO TERMINALS
Packit 258f23
    On systems providing pseudo terminals under /dev, IPC::Run can use
Packit 258f23
    IO::Pty (available on CPAN) to provide a terminal environment to
Packit 258f23
    subprocesses. This is necessary when the subprocess really wants to
Packit 258f23
    think it's connected to a real terminal.
Packit 258f23
Packit 258f23
  CAVEATS
Packit 258f23
    Pseudo-terminals are not pipes, though they are similar. Here are some
Packit 258f23
    differences to watch out for.
Packit 258f23
Packit 258f23
    Echoing
Packit 258f23
        Sending to stdin will cause an echo on stdout, which occurs before
Packit 258f23
        each line is passed to the child program. There is currently no way
Packit 258f23
        to disable this, although the child process can and should disable
Packit 258f23
        it for things like passwords.
Packit 258f23
Packit 258f23
    Shutdown
Packit 258f23
        IPC::Run cannot close a pty until all output has been collected.
Packit 258f23
        This means that it is not possible to send an EOF to stdin by
Packit 258f23
        half-closing the pty, as we can when using a pipe to stdin.
Packit 258f23
Packit 258f23
        This means that you need to send the child process an exit command
Packit 258f23
        or signal, or run() / finish() will time out. Be careful not to
Packit 258f23
        expect a prompt after sending the exit command.
Packit 258f23
Packit 258f23
    Command line editing
Packit 258f23
        Some subprocesses, notable shells that depend on the user's prompt
Packit 258f23
        settings, will reissue the prompt plus the command line input so far
Packit 258f23
        once for each character.
Packit 258f23
Packit 258f23
    '>pty>' means '&>pty>', not '1>pty>'
Packit 258f23
        The pseudo terminal redirects both stdout and stderr unless you
Packit 258f23
        specify a file descriptor. If you want to grab stderr separately, do
Packit 258f23
        this:
Packit 258f23
Packit 258f23
           start \@cmd, '<pty<', \$in, '>pty>', \$out, '2>', \$err;
Packit 258f23
Packit 258f23
    stdin, stdout, and stderr not inherited
Packit 258f23
        Child processes harnessed to a pseudo terminal have their stdin,
Packit 258f23
        stdout, and stderr completely closed before any redirection
Packit 258f23
        operators take effect. This casts of the bonds of the controlling
Packit 258f23
        terminal. This is not done when using pipes.
Packit 258f23
Packit 258f23
        Right now, this affects all children in a harness that has a pty in
Packit 258f23
        use, even if that pty would not affect a particular child. That's a
Packit 258f23
        bug and will be fixed. Until it is, it's best not to mix-and-match
Packit 258f23
        children.
Packit 258f23
Packit 258f23
  Redirection Operators
Packit 258f23
       Operator       SHNP   Description
Packit 258f23
       ========       ====   ===========
Packit 258f23
       <, N<          SHN    Redirects input to a child's fd N (0 assumed)
Packit 258f23
Packit 258f23
       >, N>          SHN    Redirects output from a child's fd N (1 assumed)
Packit 258f23
       >>, N>>        SHN    Like '>', but appends to scalars or named files
Packit 258f23
       >&, &>         SHN    Redirects stdout & stderr from a child process
Packit 258f23
Packit 258f23
       
Packit 258f23
       >pty, N>pty    S      Like '>', but uses a pseudo-tty instead of a pipe
Packit 258f23
Packit 258f23
       N<&M                  Dups input fd N to input fd M
Packit 258f23
       M>&N                  Dups output fd N to input fd M
Packit 258f23
       N<&-                  Closes fd N
Packit 258f23
Packit 258f23
       
Packit 258f23
       >pipe, N>pipe     P   Pipe opens H for caller to read, write, close.
Packit 258f23
Packit 258f23
    'N' and 'M' are placeholders for integer file descriptor numbers. The
Packit 258f23
    terms 'input' and 'output' are from the child process's perspective.
Packit 258f23
Packit 258f23
    The SHNP field indicates what parameters an operator can take:
Packit 258f23
Packit 258f23
       S: \$scalar or \&function references.  Filters may be used with
Packit 258f23
          these operators (and only these).
Packit 258f23
       H: \*HANDLE or IO::Handle for caller to open, and close
Packit 258f23
       N: "file name".
Packit 258f23
       P: \*HANDLE opened by IPC::Run as the parent end of a pipe, but read
Packit 258f23
          and written to and closed by the caller (like IPC::Open3).
Packit 258f23
Packit 258f23
    Redirecting input: [n]<, [n]
Packit 258f23
        You can input the child reads on file descriptor number n to come
Packit 258f23
        from a scalar variable, subroutine, file handle, or a named file. If
Packit 258f23
        stdin is not redirected, the parent's stdin is inherited.
Packit 258f23
Packit 258f23
           run \@cat, \undef          ## Closes child's stdin immediately
Packit 258f23
              or die "cat returned $?"; 
Packit 258f23
Packit 258f23
           run \@cat, \$in;
Packit 258f23
Packit 258f23
           run \@cat, \<
Packit 258f23
           blah
Packit 258f23
           TOHERE
Packit 258f23
Packit 258f23
           run \@cat, \&input;       ## Calls &input, feeding data returned
Packit 258f23
                                      ## to child's.  Closes child's stdin
Packit 258f23
                                      ## when undef is returned.
Packit 258f23
Packit 258f23
        Redirecting from named files requires you to use the input
Packit 258f23
        redirection operator:
Packit 258f23
Packit 258f23
           run \@cat, '<.profile';
Packit 258f23
           run \@cat, '<', '.profile';
Packit 258f23
Packit 258f23
           open IN, "
Packit 258f23
           run \@cat, \*IN;
Packit 258f23
           run \@cat, *IN{IO};
Packit 258f23
Packit 258f23
        The form used second example here is the safest, since filenames
Packit 258f23
        like "0" and "&more\n" won't confuse &run:
Packit 258f23
Packit 258f23
        You can't do either of
Packit 258f23
Packit 258f23
           run \@a, *IN;      ## INVALID
Packit 258f23
           run \@a, '<', *IN; ## BUGGY: Reads file named like "*main::A"
Packit 258f23
Packit 258f23
        because perl passes a scalar containing a string that looks like
Packit 258f23
        "*main::A" to &run, and &run can't tell the difference between that
Packit 258f23
        and a redirection operator or a file name. &run guarantees that any
Packit 258f23
        scalar you pass after a redirection operator is a file name.
Packit 258f23
Packit 258f23
        If your child process will take input from file descriptors other
Packit 258f23
        than 0 (stdin), you can use a redirection operator with any of the
Packit 258f23
        valid input forms (scalar ref, sub ref, etc.):
Packit 258f23
Packit 258f23
           run \@cat, '3<', \$in3;
Packit 258f23
Packit 258f23
        When redirecting input from a scalar ref, the scalar ref is used as
Packit 258f23
        a queue. This allows you to use &harness and pump() to feed
Packit 258f23
        incremental bits of input to a coprocess. See "Coprocesses" below
Packit 258f23
        for more information.
Packit 258f23
Packit 258f23
        The 
Packit 258f23
        glob reference it takes as an argument:
Packit 258f23
Packit 258f23
           $h = start \@cat, '
Packit 258f23
           print IN "hello world\n";
Packit 258f23
           pump $h;
Packit 258f23
           close IN;
Packit 258f23
           finish $h;
Packit 258f23
Packit 258f23
        Unlike the other '<' operators, IPC::Run does nothing further with
Packit 258f23
        it: you are responsible for it. The previous example is functionally
Packit 258f23
        equivalent to:
Packit 258f23
Packit 258f23
           pipe( \*R, \*IN ) or die $!;
Packit 258f23
           $h = start \@cat, '<', \*IN;
Packit 258f23
           print IN "hello world\n";
Packit 258f23
           pump $h;
Packit 258f23
           close IN;
Packit 258f23
           finish $h;
Packit 258f23
Packit 258f23
        This is like the behavior of IPC::Open2 and IPC::Open3.
Packit 258f23
Packit 258f23
        Win32: The handle returned is actually a socket handle, so you can
Packit 258f23
        use select() on it.
Packit 258f23
Packit 258f23
    Redirecting output: [n]>, [n]>>, [n]>&[m], [n]>pipe
Packit 258f23
        You can redirect any output the child emits to a scalar variable,
Packit 258f23
        subroutine, file handle, or file name. You can have &run truncate or
Packit 258f23
        append to named files or scalars. If you are redirecting stdin as
Packit 258f23
        well, or if the command is on the receiving end of a pipeline ('|'),
Packit 258f23
        you can omit the redirection operator:
Packit 258f23
Packit 258f23
           @ls = ( 'ls' );
Packit 258f23
           run \@ls, \undef, \$out
Packit 258f23
              or die "ls returned $?"; 
Packit 258f23
Packit 258f23
           run \@ls, \undef, \&out;  ## Calls &out each time some output
Packit 258f23
                                      ## is received from the child's 
Packit 258f23
                                      ## when undef is returned.
Packit 258f23
Packit 258f23
           run \@ls, \undef, '2>ls.err';
Packit 258f23
           run \@ls, '2>', 'ls.err';
Packit 258f23
Packit 258f23
        The two parameter form guarantees that the filename will not be
Packit 258f23
        interpreted as a redirection operator:
Packit 258f23
Packit 258f23
           run \@ls, '>', "&more";
Packit 258f23
           run \@ls, '2>', ">foo\n";
Packit 258f23
Packit 258f23
        You can pass file handles you've opened for writing:
Packit 258f23
Packit 258f23
           open( *OUT, ">out.txt" );
Packit 258f23
           open( *ERR, ">err.txt" );
Packit 258f23
           run \@cat, \*OUT, \*ERR;
Packit 258f23
Packit 258f23
        Passing a scalar reference and a code reference requires a little
Packit 258f23
        more work, but allows you to capture all of the output in a scalar
Packit 258f23
        or each piece of output by a callback:
Packit 258f23
Packit 258f23
        These two do the same things:
Packit 258f23
Packit 258f23
           run( [ 'ls' ], '2>', sub { $err_out .= $_[0] } );
Packit 258f23
Packit 258f23
        does the same basic thing as:
Packit 258f23
Packit 258f23
           run( [ 'ls' ], '2>', \$err_out );
Packit 258f23
Packit 258f23
        The subroutine will be called each time some data is read from the
Packit 258f23
        child.
Packit 258f23
Packit 258f23
        The >pipe operator is different in concept than the other '>'
Packit 258f23
        operators, although it's syntax is similar:
Packit 258f23
Packit 258f23
           $h = start \@cat, $in, '>pipe', \*OUT, '2>pipe', \*ERR;
Packit 258f23
           $in = "hello world\n";
Packit 258f23
           finish $h;
Packit 258f23
           print <OUT>;
Packit 258f23
           print <ERR>;
Packit 258f23
           close OUT;
Packit 258f23
           close ERR;
Packit 258f23
Packit 258f23
        causes two pipe to be created, with one end attached to cat's stdout
Packit 258f23
        and stderr, respectively, and the other left open on OUT and ERR, so
Packit 258f23
        that the script can manually read(), select(), etc. on them. This is
Packit 258f23
        like the behavior of IPC::Open2 and IPC::Open3.
Packit 258f23
Packit 258f23
        Win32: The handle returned is actually a socket handle, so you can
Packit 258f23
        use select() on it.
Packit 258f23
Packit 258f23
    Duplicating output descriptors: >&m, n>&m
Packit 258f23
        This duplicates output descriptor number n (default is 1 if n is
Packit 258f23
        omitted) from descriptor number m.
Packit 258f23
Packit 258f23
    Duplicating input descriptors: <&m, n<&m
Packit 258f23
        This duplicates input descriptor number n (default is 0 if n is
Packit 258f23
        omitted) from descriptor number m
Packit 258f23
Packit 258f23
    Closing descriptors: <&-, 3<&-
Packit 258f23
        This closes descriptor number n (default is 0 if n is omitted). The
Packit 258f23
        following commands are equivalent:
Packit 258f23
Packit 258f23
           run \@cmd, \undef;
Packit 258f23
           run \@cmd, '<&-;;
Packit 258f23
           run \@cmd, '
Packit 258f23
Packit 258f23
        Doing
Packit 258f23
Packit 258f23
           run \@cmd, \$in, '<&-;;    ## SIGPIPE recipe.
Packit 258f23
Packit 258f23
        is dangerous: the parent will get a SIGPIPE if $in is not empty.
Packit 258f23
Packit 258f23
    Redirecting both stdout and stderr: &>, >&, &>pipe, >pipe&
Packit 258f23
        The following pairs of commands are equivalent:
Packit 258f23
Packit 258f23
           run \@cmd, '>&', \$out;       run \@cmd, '>', \$out,     '2>&1;;
Packit 258f23
           run \@cmd, '>&', 'out.txt';   run \@cmd, '>', 'out.txt', '2>&1;;
Packit 258f23
Packit 258f23
        etc.
Packit 258f23
Packit 258f23
        File descriptor numbers are not permitted to the left or the right
Packit 258f23
        of these operators, and the '&' may occur on either end of the
Packit 258f23
        operator.
Packit 258f23
Packit 258f23
        The '&>pipe' and '>pipe&' variants behave like the '>pipe' operator,
Packit 258f23
        except that both stdout and stderr write to the created pipe.
Packit 258f23
Packit 258f23
    Redirection Filters
Packit 258f23
        Both input redirections and output redirections that use scalars or
Packit 258f23
        subs as endpoints may have an arbitrary number of filter subs placed
Packit 258f23
        between them and the child process. This is useful if you want to
Packit 258f23
        receive output in chunks, or if you want to massage each chunk of
Packit 258f23
        data sent to the child. To use this feature, you must use operator
Packit 258f23
        syntax:
Packit 258f23
Packit 258f23
           run(
Packit 258f23
              \@cmd
Packit 258f23
                 '<', \&in_filter_2, \&in_filter_1, $in,
Packit 258f23
                 '>', \&out_filter_1, \&in_filter_2, $out,
Packit 258f23
           );
Packit 258f23
Packit 258f23
        This capability is not provided for IO handles or named files.
Packit 258f23
Packit 258f23
        Two filters are provided by IPC::Run: appender and chunker. Because
Packit 258f23
        these may take an argument, you need to use the constructor
Packit 258f23
        functions new_appender() and new_chunker() rather than using \&
Packit 258f23
        syntax:
Packit 258f23
Packit 258f23
           run(
Packit 258f23
              \@cmd
Packit 258f23
                 '<', new_appender( "\n" ), $in,
Packit 258f23
                 '>', new_chunker, $out,
Packit 258f23
           );
Packit 258f23
Packit 258f23
  Just doing I/O
Packit 258f23
    If you just want to do I/O to a handle or file you open yourself, you
Packit 258f23
    may specify a filehandle or filename instead of a command in the harness
Packit 258f23
    specification:
Packit 258f23
Packit 258f23
       run io( "filename", '>', \$recv );
Packit 258f23
Packit 258f23
       $h = start io( $io, '>', \$recv );
Packit 258f23
Packit 258f23
       $h = harness \@cmd, '&', io( "file", '<', \$send );
Packit 258f23
Packit 258f23
  Options
Packit 258f23
    Options are passed in as name/value pairs:
Packit 258f23
Packit 258f23
       run \@cat, \$in, debug => 1;
Packit 258f23
Packit 258f23
    If you pass the debug option, you may want to pass it in first, so you
Packit 258f23
    can see what parsing is going on:
Packit 258f23
Packit 258f23
       run debug => 1, \@cat, \$in;
Packit 258f23
Packit 258f23
    debug
Packit 258f23
        Enables debugging output in parent and child. Debugging info is
Packit 258f23
        emitted to the STDERR that was present when IPC::Run was first
Packit 258f23
        "use()"ed (it's "dup()"ed out of the way so that it can be
Packit 258f23
        redirected in children without having debugging output emitted on
Packit 258f23
        it).
Packit 258f23
Packit 258f23
RETURN VALUES
Packit 258f23
    harness() and start() return a reference to an IPC::Run harness. This is
Packit 258f23
    blessed in to the IPC::Run package, so you may make later calls to
Packit 258f23
    functions as members if you like:
Packit 258f23
Packit 258f23
       $h = harness( ... );
Packit 258f23
       $h->start;
Packit 258f23
       $h->pump;
Packit 258f23
       $h->finish;
Packit 258f23
Packit 258f23
       $h = start( .... );
Packit 258f23
       $h->pump;
Packit 258f23
       ...
Packit 258f23
Packit 258f23
    Of course, using method call syntax lets you deal with any IPC::Run
Packit 258f23
    subclasses that might crop up, but don't hold your breath waiting for
Packit 258f23
    any.
Packit 258f23
Packit 258f23
    run() and finish() return TRUE when all subcommands exit with a 0 result
Packit 258f23
    code. This is the opposite of perl's system() command.
Packit 258f23
Packit 258f23
    All routines raise exceptions (via die()) when error conditions are
Packit 258f23
    recognized. A non-zero command result is not treated as an error
Packit 258f23
    condition, since some commands are tests whose results are reported in
Packit 258f23
    their exit codes.
Packit 258f23
Packit 258f23
ROUTINES
Packit 258f23
        run Run takes a harness or harness specification and runs it,
Packit 258f23
            pumping all input to the child(ren), closing the input pipes
Packit 258f23
            when no more input is available, collecting all output that
Packit 258f23
            arrives, until the pipes delivering output are closed, then
Packit 258f23
            waiting for the children to exit and reaping their result codes.
Packit 258f23
Packit 258f23
            You may think of "run( ... )" as being like
Packit 258f23
Packit 258f23
               start( ... )->finish();
Packit 258f23
Packit 258f23
            , though there is one subtle difference: run() does not set
Packit 258f23
            \$input_scalars to '' like finish() does. If an exception is
Packit 258f23
            thrown from run(), all children will be killed off "gently", and
Packit 258f23
            then "annihilated" if they do not go gently (in to that dark
Packit 258f23
            night. sorry).
Packit 258f23
Packit 258f23
            If any exceptions are thrown, this does a "kill_kill" before
Packit 258f23
            propagating them.
Packit 258f23
Packit 258f23
        signal
Packit 258f23
               ## To send it a specific signal by name ("USR1"):
Packit 258f23
               signal $h, "USR1";
Packit 258f23
               $h->signal ( "USR1" );
Packit 258f23
Packit 258f23
            If $signal is provided and defined, sends a signal to all child
Packit 258f23
            processes. Try not to send numeric signals, use "KILL" instead
Packit 258f23
            of 9, for instance. Numeric signals aren't portable.
Packit 258f23
Packit 258f23
            Throws an exception if $signal is undef.
Packit 258f23
Packit 258f23
            This will *not* clean up the harness, "finish" it if you kill
Packit 258f23
            it.
Packit 258f23
Packit 258f23
            Normally TERM kills a process gracefully (this is what the
Packit 258f23
            command line utility "kill" does by default), INT is sent by one
Packit 258f23
            of the keys "^C", "Backspace" or "", and "QUIT" is used to
Packit 258f23
            kill a process and make it coredump.
Packit 258f23
Packit 258f23
            The "HUP" signal is often used to get a process to "restart",
Packit 258f23
            rereading config files, and "USR1" and "USR2" for really
Packit 258f23
            application-specific things.
Packit 258f23
Packit 258f23
            Often, running "kill -l" (that's a lower case "L") on the
Packit 258f23
            command line will list the signals present on your operating
Packit 258f23
            system.
Packit 258f23
Packit 258f23
            WARNING: The signal subsystem is not at all portable. We *may*
Packit 258f23
            offer to simulate "TERM" and "KILL" on some operating systems,
Packit 258f23
            submit code to me if you want this.
Packit 258f23
Packit 258f23
            WARNING 2: Up to and including perl v5.6.1, doing almost
Packit 258f23
            anything in a signal handler could be dangerous. The most safe
Packit 258f23
            code avoids all mallocs and system calls, usually by
Packit 258f23
            preallocating a flag before entering the signal handler,
Packit 258f23
            altering the flag's value in the handler, and responding to the
Packit 258f23
            changed value in the main system:
Packit 258f23
Packit 258f23
               my $got_usr1 = 0;
Packit 258f23
               sub usr1_handler { ++$got_signal }
Packit 258f23
Packit 258f23
               $SIG{USR1} = \&usr1_handler;
Packit 258f23
               while () { sleep 1; print "GOT IT" while $got_usr1--; }
Packit 258f23
Packit 258f23
            Even this approach is perilous if ++ and -- aren't atomic on
Packit 258f23
            your system (I've never heard of this on any modern CPU large
Packit 258f23
            enough to run perl).
Packit 258f23
Packit 258f23
        kill_kill
Packit 258f23
               ## To kill off a process:
Packit 258f23
               $h->kill_kill;
Packit 258f23
               kill_kill $h;
Packit 258f23
Packit 258f23
               ## To specify the grace period other than 30 seconds:
Packit 258f23
               kill_kill $h, grace => 5;
Packit 258f23
Packit 258f23
               ## To send QUIT instead of KILL if a process refuses to die:
Packit 258f23
               kill_kill $h, coup_d_grace => "QUIT";
Packit 258f23
Packit 258f23
            Sends a "TERM", waits for all children to exit for up to 30
Packit 258f23
            seconds, then sends a "KILL" to any that survived the "TERM".
Packit 258f23
Packit 258f23
            Will wait for up to 30 more seconds for the OS to successfully
Packit 258f23
            "KILL" the processes.
Packit 258f23
Packit 258f23
            The 30 seconds may be overridden by setting the "grace" option,
Packit 258f23
            this overrides both timers.
Packit 258f23
Packit 258f23
            The harness is then cleaned up.
Packit 258f23
Packit 258f23
            The doubled name indicates that this function may kill again and
Packit 258f23
            avoids colliding with the core Perl "kill" function.
Packit 258f23
Packit 258f23
            Returns a 1 if the "TERM" was sufficient, or a 0 if "KILL" was
Packit 258f23
            required. Throws an exception if "KILL" did not permit the
Packit 258f23
            children to be reaped.
Packit 258f23
Packit 258f23
            NOTE: The grace period is actually up to 1 second longer than
Packit 258f23
            that given. This is because the granularity of "time" is 1
Packit 258f23
            second. Let me know if you need finer granularity, we can
Packit 258f23
            leverage Time::HiRes here.
Packit 258f23
Packit 258f23
            Win32: Win32 does not know how to send real signals, so "TERM"
Packit 258f23
            is a full-force kill on Win32. Thus all talk of grace periods,
Packit 258f23
            etc. do not apply to Win32.
Packit 258f23
Packit 258f23
        harness
Packit 258f23
            Takes a harness specification and returns a harness. This
Packit 258f23
            harness is blessed in to IPC::Run, allowing you to use method
Packit 258f23
            call syntax for run(), start(), et al if you like.
Packit 258f23
Packit 258f23
            harness() is provided so that you can pre-build harnesses if you
Packit 258f23
            would like to, but it's not required..
Packit 258f23
Packit 258f23
            You may proceed to run(), start() or pump() after calling
Packit 258f23
            harness() (pump() calls start() if need be). Alternatively, you
Packit 258f23
            may pass your harness specification to run() or start() and let
Packit 258f23
            them harness() for you. You can't pass harness specifications to
Packit 258f23
            pump(), though.
Packit 258f23
Packit 258f23
        close_terminal
Packit 258f23
            This is used as (or in) an init sub to cast off the bonds of a
Packit 258f23
            controlling terminal. It must precede all other redirection ops
Packit 258f23
            that affect STDIN, STDOUT, or STDERR to be guaranteed effective.
Packit 258f23
Packit 258f23
        start
Packit 258f23
               $h = start(
Packit 258f23
                  \@cmd, \$in, \$out, ...,
Packit 258f23
                  timeout( 30, name => "process timeout" ),
Packit 258f23
                  $stall_timeout = timeout( 10, name => "stall timeout"   ),
Packit 258f23
               );
Packit 258f23
Packit 258f23
               $h = start \@cmd, '<', \$in, '|', \@cmd2, ...;
Packit 258f23
Packit 258f23
            start() accepts a harness or harness specification and returns a
Packit 258f23
            harness after building all of the pipes and launching (via
Packit 258f23
            fork()/exec(), or, maybe someday, spawn()) all the child
Packit 258f23
            processes. It does not send or receive any data on the pipes,
Packit 258f23
            see pump() and finish() for that.
Packit 258f23
Packit 258f23
            You may call harness() and then pass it's result to start() if
Packit 258f23
            you like, but you only need to if it helps you structure or tune
Packit 258f23
            your application. If you do call harness(), you may skip start()
Packit 258f23
            and proceed directly to pump.
Packit 258f23
Packit 258f23
            start() also starts all timers in the harness. See
Packit 258f23
            IPC::Run::Timer for more information.
Packit 258f23
Packit 258f23
            start() flushes STDOUT and STDERR to help you avoid duplicate
Packit 258f23
            output. It has no way of asking Perl to flush all your open
Packit 258f23
            filehandles, so you are going to need to flush any others you
Packit 258f23
            have open. Sorry.
Packit 258f23
Packit 258f23
            Here's how if you don't want to alter the state of $| for your
Packit 258f23
            filehandle:
Packit 258f23
Packit 258f23
               $ofh = select HANDLE; $of = $|; $| = 1; $| = $of; select $ofh;
Packit 258f23
Packit 258f23
            If you don't mind leaving output unbuffered on HANDLE, you can
Packit 258f23
            do the slightly shorter
Packit 258f23
Packit 258f23
               $ofh = select HANDLE; $| = 1; select $ofh;
Packit 258f23
Packit 258f23
            Or, you can use IO::Handle's flush() method:
Packit 258f23
Packit 258f23
               use IO::Handle;
Packit 258f23
               flush HANDLE;
Packit 258f23
Packit 258f23
            Perl needs the equivalent of C's fflush( (FILE *)NULL ).
Packit 258f23
Packit 258f23
        adopt
Packit 258f23
            Experimental feature. NOT FUNCTIONAL YET, NEED TO CLOSE FDS
Packit 258f23
            BETTER IN CHILDREN. SEE t/adopt.t for a test suite.
Packit 258f23
Packit 258f23
        pump
Packit 258f23
               pump $h;
Packit 258f23
               $h->pump;
Packit 258f23
Packit 258f23
            Pump accepts a single parameter harness. It blocks until it
Packit 258f23
            delivers some input or receives some output. It returns TRUE if
Packit 258f23
            there is still input or output to be done, FALSE otherwise.
Packit 258f23
Packit 258f23
            pump() will automatically call start() if need be, so you may
Packit 258f23
            call harness() then proceed to pump() if that helps you
Packit 258f23
            structure your application.
Packit 258f23
Packit 258f23
            If pump() is called after all harnessed activities have
Packit 258f23
            completed, a "process ended prematurely" exception to be thrown.
Packit 258f23
            This allows for simple scripting of external applications
Packit 258f23
            without having to add lots of error handling code at each step
Packit 258f23
            of the script:
Packit 258f23
Packit 258f23
               $h = harness \@smbclient, \$in, \$out, $err;
Packit 258f23
Packit 258f23
               $in = "cd /foo\n";
Packit 258f23
               $h->pump until $out =~ /^smb.*> \Z/m;
Packit 258f23
               die "error cding to /foo:\n$out" if $out =~ "ERR";
Packit 258f23
               $out = '';
Packit 258f23
Packit 258f23
               $in = "mget *\n";
Packit 258f23
               $h->pump until $out =~ /^smb.*> \Z/m;
Packit 258f23
               die "error retrieving files:\n$out" if $out =~ "ERR";
Packit 258f23
Packit 258f23
               $h->finish;
Packit 258f23
Packit 258f23
               warn $err if $err;
Packit 258f23
Packit 258f23
        pump_nb
Packit 258f23
               pump_nb $h;
Packit 258f23
               $h->pump_nb;
Packit 258f23
Packit 258f23
            "pump() non-blocking", pumps if anything's ready to be pumped,
Packit 258f23
            returns immediately otherwise. This is useful if you're doing
Packit 258f23
            some long-running task in the foreground, but don't want to
Packit 258f23
            starve any child processes.
Packit 258f23
Packit 258f23
        pumpable
Packit 258f23
            Returns TRUE if calling pump() won't throw an immediate "process
Packit 258f23
            ended prematurely" exception. This means that there are open I/O
Packit 258f23
            channels or active processes. May yield the parent processes'
Packit 258f23
            time slice for 0.01 second if all pipes are to the child and all
Packit 258f23
            are paused. In this case we can't tell if the child is dead, so
Packit 258f23
            we yield the processor and then attempt to reap the child in a
Packit 258f23
            nonblocking way.
Packit 258f23
Packit 258f23
        reap_nb
Packit 258f23
            Attempts to reap child processes, but does not block.
Packit 258f23
Packit 258f23
            Does not currently take any parameters, one day it will allow
Packit 258f23
            specific children to be reaped.
Packit 258f23
Packit 258f23
            Only call this from a signal handler if your "perl" is recent
Packit 258f23
            enough to have safe signal handling (5.6.1 did not, IIRC, but it
Packit 258f23
            was being discussed on perl5-porters). Calling this (or doing
Packit 258f23
            any significant work) in a signal handler on older "perl"s is
Packit 258f23
            asking for seg faults.
Packit 258f23
Packit 258f23
        finish
Packit 258f23
            This must be called after the last start() or pump() call for a
Packit 258f23
            harness, or your system will accumulate defunct processes and
Packit 258f23
            you may "leak" file descriptors.
Packit 258f23
Packit 258f23
            finish() returns TRUE if all children returned 0 (and were not
Packit 258f23
            signaled and did not coredump, ie ! $?), and FALSE otherwise
Packit 258f23
            (this is like run(), and the opposite of system()).
Packit 258f23
Packit 258f23
            Once a harness has been finished, it may be run() or start()ed
Packit 258f23
            again, including by pump()s auto-start.
Packit 258f23
Packit 258f23
            If this throws an exception rather than a normal exit, the
Packit 258f23
            harness may be left in an unstable state, it's best to kill the
Packit 258f23
            harness to get rid of all the child processes, etc.
Packit 258f23
Packit 258f23
            Specifically, if a timeout expires in finish(), finish() will
Packit 258f23
            not kill all the children. Call "<$h-"kill_kill>> in this case
Packit 258f23
            if you care. This differs from the behavior of "run".
Packit 258f23
Packit 258f23
        result
Packit 258f23
               $h->result;
Packit 258f23
Packit 258f23
            Returns the first non-zero result code (ie $? >> 8). See
Packit 258f23
            "full_result" to get the $? value for a child process.
Packit 258f23
Packit 258f23
            To get the result of a particular child, do:
Packit 258f23
Packit 258f23
               $h->result( 0 );  # first child's $? >> 8
Packit 258f23
               $h->result( 1 );  # second child
Packit 258f23
Packit 258f23
            or
Packit 258f23
Packit 258f23
               ($h->results)[0]
Packit 258f23
               ($h->results)[1]
Packit 258f23
Packit 258f23
            Returns undef if no child processes were spawned and no child
Packit 258f23
            number was specified. Throws an exception if an out-of-range
Packit 258f23
            child number is passed.
Packit 258f23
Packit 258f23
        results
Packit 258f23
            Returns a list of child exit values. See "full_results" if you
Packit 258f23
            want to know if a signal killed the child.
Packit 258f23
Packit 258f23
            Throws an exception if the harness is not in a finished state.
Packit 258f23
Packit 258f23
        full_result
Packit 258f23
               $h->full_result;
Packit 258f23
Packit 258f23
            Returns the first non-zero $?. See "result" to get the first $?
Packit 258f23
            >> 8 value for a child process.
Packit 258f23
Packit 258f23
            To get the result of a particular child, do:
Packit 258f23
Packit 258f23
               $h->full_result( 0 );  # first child's $?
Packit 258f23
               $h->full_result( 1 );  # second child
Packit 258f23
Packit 258f23
            or
Packit 258f23
Packit 258f23
               ($h->full_results)[0]
Packit 258f23
               ($h->full_results)[1]
Packit 258f23
Packit 258f23
            Returns undef if no child processes were spawned and no child
Packit 258f23
            number was specified. Throws an exception if an out-of-range
Packit 258f23
            child number is passed.
Packit 258f23
Packit 258f23
        full_results
Packit 258f23
            Returns a list of child exit values as returned by "wait". See
Packit 258f23
            "results" if you don't care about coredumps or signals.
Packit 258f23
Packit 258f23
            Throws an exception if the harness is not in a finished state.
Packit 258f23
Packit 258f23
FILTERS
Packit 258f23
    These filters are used to modify input our output between a child
Packit 258f23
    process and a scalar or subroutine endpoint.
Packit 258f23
Packit 258f23
    binary
Packit 258f23
           run \@cmd, ">", binary, \$out;
Packit 258f23
           run \@cmd, ">", binary, \$out;  ## Any TRUE value to enable
Packit 258f23
           run \@cmd, ">", binary 0, \$out;  ## Any FALSE value to disable
Packit 258f23
Packit 258f23
        This is a constructor for a "binmode" "filter" that tells IPC::Run
Packit 258f23
        to keep the carriage returns that would ordinarily be edited out for
Packit 258f23
        you (binmode is usually off). This is not a real filter, but an
Packit 258f23
        option masquerading as a filter.
Packit 258f23
Packit 258f23
        It's not named "binmode" because you're likely to want to call
Packit 258f23
        Perl's binmode in programs that are piping binary data around.
Packit 258f23
Packit 258f23
    new_chunker
Packit 258f23
        This breaks a stream of data in to chunks, based on an optional
Packit 258f23
        scalar or regular expression parameter. The default is the Perl
Packit 258f23
        input record separator in $/, which is a newline be default.
Packit 258f23
Packit 258f23
           run \@cmd, '>', new_chunker, \&lines_handler;
Packit 258f23
           run \@cmd, '>', new_chunker( "\r\n" ), \&lines_handler;
Packit 258f23
Packit 258f23
        Because this uses $/ by default, you should always pass in a
Packit 258f23
        parameter if you are worried about other code (modules, etc)
Packit 258f23
        modifying $/.
Packit 258f23
Packit 258f23
        If this filter is last in a filter chain that dumps in to a scalar,
Packit 258f23
        the scalar must be set to '' before a new chunk will be written to
Packit 258f23
        it.
Packit 258f23
Packit 258f23
        As an example of how a filter like this can be written, here's a
Packit 258f23
        chunker that splits on newlines:
Packit 258f23
Packit 258f23
           sub line_splitter {
Packit 258f23
              my ( $in_ref, $out_ref ) = @_;
Packit 258f23
Packit 258f23
              return 0 if length $$out_ref;
Packit 258f23
Packit 258f23
              return input_avail && do {
Packit 258f23
                 while (1) {
Packit 258f23
                    if ( $$in_ref =~ s/\A(.*?\n)// ) {
Packit 258f23
                       $$out_ref .= $1;
Packit 258f23
                       return 1;
Packit 258f23
                    }
Packit 258f23
                    my $hmm = get_more_input;
Packit 258f23
                    unless ( defined $hmm ) {
Packit 258f23
                       $$out_ref = $$in_ref;
Packit 258f23
                       $$in_ref = '';
Packit 258f23
                       return length $$out_ref ? 1 : 0;
Packit 258f23
                    }
Packit 258f23
                    return 0 if $hmm eq 0;
Packit 258f23
                 }
Packit 258f23
              }
Packit 258f23
           };
Packit 258f23
Packit 258f23
    new_appender
Packit 258f23
        This appends a fixed string to each chunk of data read from the
Packit 258f23
        source scalar or sub. This might be useful if you're writing
Packit 258f23
        commands to a child process that always must end in a fixed string,
Packit 258f23
        like "\n":
Packit 258f23
Packit 258f23
           run( \@cmd,
Packit 258f23
              '<', new_appender( "\n" ), \&commands,
Packit 258f23
           );
Packit 258f23
Packit 258f23
        Here's a typical filter sub that might be created by new_appender():
Packit 258f23
Packit 258f23
           sub newline_appender {
Packit 258f23
              my ( $in_ref, $out_ref ) = @_;
Packit 258f23
Packit 258f23
              return input_avail && do {
Packit 258f23
                 $$out_ref = join( '', $$out_ref, $$in_ref, "\n" );
Packit 258f23
                 $$in_ref = '';
Packit 258f23
                 1;
Packit 258f23
              }
Packit 258f23
           };
Packit 258f23
Packit 258f23
    new_string_source
Packit 258f23
        TODO: Needs confirmation. Was previously undocumented. in this
Packit 258f23
        module.
Packit 258f23
Packit 258f23
        This is a filter which is exportable. Returns a sub which appends
Packit 258f23
        the data passed in to the output buffer and returns 1 if data was
Packit 258f23
        appended. 0 if it was an empty string and undef if no data was
Packit 258f23
        passed.
Packit 258f23
Packit 258f23
        NOTE: Any additional variables passed to new_string_source will be
Packit 258f23
        passed to the sub every time it's called and appended to the output.
Packit 258f23
Packit 258f23
    new_string_sink
Packit 258f23
        TODO: Needs confirmation. Was previously undocumented.
Packit 258f23
Packit 258f23
        This is a filter which is exportable. Returns a sub which pops the
Packit 258f23
        data out of the input stream and pushes it onto the string.
Packit 258f23
Packit 258f23
    io  Takes a filename or filehandle, a redirection operator, optional
Packit 258f23
        filters, and a source or destination (depends on the redirection
Packit 258f23
        operator). Returns an IPC::Run::IO object suitable for harness()ing
Packit 258f23
        (including via start() or run()).
Packit 258f23
Packit 258f23
        This is shorthand for
Packit 258f23
Packit 258f23
           require IPC::Run::IO;
Packit 258f23
Packit 258f23
              ... IPC::Run::IO->new(...) ...
Packit 258f23
Packit 258f23
    timer
Packit 258f23
           $h = start( \@cmd, \$in, \$out, $t = timer( 5 ) );
Packit 258f23
Packit 258f23
           pump $h until $out =~ /expected stuff/ || $t->is_expired;
Packit 258f23
Packit 258f23
        Instantiates a non-fatal timer. pump() returns once each time a
Packit 258f23
        timer expires. Has no direct effect on run(), but you can pass a
Packit 258f23
        subroutine to fire when the timer expires.
Packit 258f23
Packit 258f23
        See "timeout" for building timers that throw exceptions on
Packit 258f23
        expiration.
Packit 258f23
Packit 258f23
        See "timer" in IPC::Run::Timer for details.
Packit 258f23
Packit 258f23
    timeout
Packit 258f23
           $h = start( \@cmd, \$in, \$out, $t = timeout( 5 ) );
Packit 258f23
Packit 258f23
           pump $h until $out =~ /expected stuff/;
Packit 258f23
Packit 258f23
        Instantiates a timer that throws an exception when it expires. If
Packit 258f23
        you don't provide an exception, a default exception that matches
Packit 258f23
        /^IPC::Run: .*timed out/ is thrown by default. You can pass in your
Packit 258f23
        own exception scalar or reference:
Packit 258f23
Packit 258f23
           $h = start(
Packit 258f23
              \@cmd, \$in, \$out,
Packit 258f23
              $t = timeout( 5, exception => 'slowpoke' ),
Packit 258f23
           );
Packit 258f23
Packit 258f23
        or set the name used in debugging message and in the default
Packit 258f23
        exception string:
Packit 258f23
Packit 258f23
           $h = start(
Packit 258f23
              \@cmd, \$in, \$out,
Packit 258f23
              timeout( 50, name => 'process timer' ),
Packit 258f23
              $stall_timer = timeout( 5, name => 'stall timer' ),
Packit 258f23
           );
Packit 258f23
Packit 258f23
           pump $h until $out =~ /started/;
Packit 258f23
Packit 258f23
           $in = 'command 1';
Packit 258f23
           $stall_timer->start;
Packit 258f23
           pump $h until $out =~ /command 1 finished/;
Packit 258f23
Packit 258f23
           $in = 'command 2';
Packit 258f23
           $stall_timer->start;
Packit 258f23
           pump $h until $out =~ /command 2 finished/;
Packit 258f23
Packit 258f23
           $in = 'very slow command 3';
Packit 258f23
           $stall_timer->start( 10 );
Packit 258f23
           pump $h until $out =~ /command 3 finished/;
Packit 258f23
Packit 258f23
           $stall_timer->start( 5 );
Packit 258f23
           $in = 'command 4';
Packit 258f23
           pump $h until $out =~ /command 4 finished/;
Packit 258f23
Packit 258f23
           $stall_timer->reset; # Prevent restarting or expirng
Packit 258f23
           finish $h;
Packit 258f23
Packit 258f23
        See "timer" for building non-fatal timers.
Packit 258f23
Packit 258f23
        See "timer" in IPC::Run::Timer for details.
Packit 258f23
Packit 258f23
FILTER IMPLEMENTATION FUNCTIONS
Packit 258f23
    These functions are for use from within filters.
Packit 258f23
Packit 258f23
    input_avail
Packit 258f23
        Returns TRUE if input is available. If none is available, then
Packit 258f23
        &get_more_input is called and its result is returned.
Packit 258f23
Packit 258f23
        This is usually used in preference to &get_more_input so that the
Packit 258f23
        calling filter removes all data from the $in_ref before more data
Packit 258f23
        gets read in to $in_ref.
Packit 258f23
Packit 258f23
        "input_avail" is usually used as part of a return expression:
Packit 258f23
Packit 258f23
           return input_avail && do {
Packit 258f23
              ## process the input just gotten
Packit 258f23
              1;
Packit 258f23
           };
Packit 258f23
Packit 258f23
        This technique allows input_avail to return the undef or 0 that a
Packit 258f23
        filter normally returns when there's no input to process. If a
Packit 258f23
        filter stores intermediate values, however, it will need to react to
Packit 258f23
        an undef:
Packit 258f23
Packit 258f23
           my $got = input_avail;
Packit 258f23
           if ( ! defined $got ) {
Packit 258f23
              ## No more input ever, flush internal buffers to $out_ref
Packit 258f23
           }
Packit 258f23
           return $got unless $got;
Packit 258f23
           ## Got some input, move as much as need be
Packit 258f23
           return 1 if $added_to_out_ref;
Packit 258f23
Packit 258f23
    get_more_input
Packit 258f23
        This is used to fetch more input in to the input variable. It
Packit 258f23
        returns undef if there will never be any more input, 0 if there is
Packit 258f23
        none now, but there might be in the future, and TRUE if more input
Packit 258f23
        was gotten.
Packit 258f23
Packit 258f23
        "get_more_input" is usually used as part of a return expression, see
Packit 258f23
        "input_avail" for more information.
Packit 258f23
Packit 258f23
TODO
Packit 258f23
    These will be addressed as needed and as time allows.
Packit 258f23
Packit 258f23
    Stall timeout.
Packit 258f23
Packit 258f23
    Expose a list of child process objects. When I do this, each child
Packit 258f23
    process is likely to be blessed into IPC::Run::Proc.
Packit 258f23
Packit 258f23
    $kid->abort(), $kid->kill(), $kid->signal( $num_or_name ).
Packit 258f23
Packit 258f23
    Write tests for /(full_)?results?/ subs.
Packit 258f23
Packit 258f23
    Currently, pump() and run() only work on systems where select() works on
Packit 258f23
    the filehandles returned by pipe(). This does *not* include ActiveState
Packit 258f23
    on Win32, although it does work on cygwin under Win32 (thought the tests
Packit 258f23
    whine a bit). I'd like to rectify that, suggestions and patches welcome.
Packit 258f23
Packit 258f23
    Likewise start() only fully works on fork()/exec() machines (well, just
Packit 258f23
    fork() if you only ever pass perl subs as subprocesses). There's some
Packit 258f23
    scaffolding for calling Open3::spawn_with_handles(), but that's
Packit 258f23
    untested, and not that useful with limited select().
Packit 258f23
Packit 258f23
    Support for "\@sub_cmd" as an argument to a command which gets replaced
Packit 258f23
    with /dev/fd or the name of a temporary file containing foo's output.
Packit 258f23
    This is like <(sub_cmd ...) found in bash and csh (IIRC).
Packit 258f23
Packit 258f23
    Allow multiple harnesses to be combined as independent sets of processes
Packit 258f23
    in to one 'meta-harness'.
Packit 258f23
Packit 258f23
    Allow a harness to be passed in place of an \@cmd. This would allow
Packit 258f23
    multiple harnesses to be aggregated.
Packit 258f23
Packit 258f23
    Ability to add external file descriptors w/ filter chains and endpoints.
Packit 258f23
Packit 258f23
    Ability to add timeouts and timing generators (i.e. repeating timeouts).
Packit 258f23
Packit 258f23
    High resolution timeouts.
Packit 258f23
Packit 258f23
Win32 LIMITATIONS
Packit 258f23
    Fails on Win9X
Packit 258f23
        If you want Win9X support, you'll have to debug it or fund me
Packit 258f23
        because I don't use that system any more. The Win32 subsysem has
Packit 258f23
        been extended to use temporary files in simple run() invocations and
Packit 258f23
        these may actually work on Win9X too, but I don't have time to work
Packit 258f23
        on it.
Packit 258f23
Packit 258f23
    May deadlock on Win2K (but not WinNT4 or WinXPPro)
Packit 258f23
        Spawning more than one subprocess on Win2K causes a deadlock I
Packit 258f23
        haven't figured out yet, but simple uses of run() often work. Passes
Packit 258f23
        all tests on WinXPPro and WinNT.
Packit 258f23
Packit 258f23
    no support yet for <pty< and >pty>
Packit 258f23
        These are likely to be implemented as "<" and ">" with binmode on,
Packit 258f23
        not sure.
Packit 258f23
Packit 258f23
    no support for file descriptors higher than 2 (stderr)
Packit 258f23
        Win32 only allows passing explicit fds 0, 1, and 2. If you really,
Packit 258f23
        really need to pass file handles, us Win32API:: GetOsFHandle() or
Packit 258f23
        ::FdGetOsFHandle() to get the integer handle and pass it to the
Packit 258f23
        child process using the command line, environment, stdin,
Packit 258f23
        intermediary file, or other IPC mechanism. Then use that handle in
Packit 258f23
        the child (Win32API.pm provides ways to reconstitute Perl file
Packit 258f23
        handles from Win32 file handles).
Packit 258f23
Packit 258f23
    no support for subroutine subprocesses (CODE refs)
Packit 258f23
        Can't fork(), so the subroutines would have no context, and closures
Packit 258f23
        certainly have no meaning
Packit 258f23
Packit 258f23
        Perhaps with Win32 fork() emulation, this can be supported in a
Packit 258f23
        limited fashion, but there are other very serious problems with
Packit 258f23
        that: all parent fds get dup()ed in to the thread emulating the
Packit 258f23
        forked process, and that keeps the parent from being able to close
Packit 258f23
        all of the appropriate fds.
Packit 258f23
Packit 258f23
    no support for init => sub {} routines.
Packit 258f23
        Win32 processes are created from scratch, there is no way to do an
Packit 258f23
        init routine that will affect the running child. Some limited
Packit 258f23
        support might be implemented one day, do chdir() and %ENV changes
Packit 258f23
        can be made.
Packit 258f23
Packit 258f23
    signals
Packit 258f23
        Win32 does not fully support signals. signal() is likely to cause
Packit 258f23
        errors unless sending a signal that Perl emulates, and "kill_kill()"
Packit 258f23
        is immediately fatal (there is no grace period).
Packit 258f23
Packit 258f23
    helper processes
Packit 258f23
        IPC::Run uses helper processes, one per redirected file, to adapt
Packit 258f23
        between the anonymous pipe connected to the child and the TCP socket
Packit 258f23
        connected to the parent. This is a waste of resources and will
Packit 258f23
        change in the future to either use threads (instead of helper
Packit 258f23
        processes) or a WaitForMultipleObjects call (instead of select).
Packit 258f23
        Please contact me if you can help with the WaitForMultipleObjects()
Packit 258f23
        approach; I haven't figured out how to get at it without C code.
Packit 258f23
Packit 258f23
    shutdown pause
Packit 258f23
        There seems to be a pause of up to 1 second between when a child
Packit 258f23
        program exits and the corresponding sockets indicate that they are
Packit 258f23
        closed in the parent. Not sure why.
Packit 258f23
Packit 258f23
    binmode
Packit 258f23
        binmode is not supported yet. The underpinnings are implemented,
Packit 258f23
        just ask if you need it.
Packit 258f23
Packit 258f23
    IPC::Run::IO
Packit 258f23
        IPC::Run::IO objects can be used on Unix to read or write arbitrary
Packit 258f23
        files. On Win32, they will need to use the same helper processes to
Packit 258f23
        adapt from non-select()able filehandles to select()able ones (or
Packit 258f23
        perhaps WaitForMultipleObjects() will work with them, not sure).
Packit 258f23
Packit 258f23
    startup race conditions
Packit 258f23
        There seems to be an occasional race condition between child process
Packit 258f23
        startup and pipe closings. It seems like if the child is not fully
Packit 258f23
        created by the time CreateProcess returns and we close the TCP
Packit 258f23
        socket being handed to it, the parent socket can also get closed.
Packit 258f23
        This is seen with the Win32 pumper applications, not the "real"
Packit 258f23
        child process being spawned.
Packit 258f23
Packit 258f23
        I assume this is because the kernel hasn't gotten around to
Packit 258f23
        incrementing the reference count on the child's end (since the child
Packit 258f23
        was slow in starting), so the parent's closing of the child end
Packit 258f23
        causes the socket to be closed, thus closing the parent socket.
Packit 258f23
Packit 258f23
        Being a race condition, it's hard to reproduce, but I encountered it
Packit 258f23
        while testing this code on a drive share to a samba box. In this
Packit 258f23
        case, it takes t/run.t a long time to spawn it's child processes
Packit 258f23
        (the parent hangs in the first select for several seconds until the
Packit 258f23
        child emits any debugging output).
Packit 258f23
Packit 258f23
        I have not seen it on local drives, and can't reproduce it at will,
Packit 258f23
        unfortunately. The symptom is a "bad file descriptor in select()"
Packit 258f23
        error, and, by turning on debugging, it's possible to see that
Packit 258f23
        select() is being called on a no longer open file descriptor that
Packit 258f23
        was returned from the _socket() routine in Win32Helper. There's a
Packit 258f23
        new confess() that checks for this ("PARENT_HANDLE no longer open"),
Packit 258f23
        but I haven't been able to reproduce it (typically).
Packit 258f23
Packit 258f23
LIMITATIONS
Packit 258f23
    On Unix, requires a system that supports "waitpid( $pid, WNOHANG )" so
Packit 258f23
    it can tell if a child process is still running.
Packit 258f23
Packit 258f23
    PTYs don't seem to be non-blocking on some versions of Solaris. Here's a
Packit 258f23
    test script contributed by Borislav Deianov <borislav@ensim.com> to see
Packit 258f23
    if you have the problem. If it dies, you have the problem.
Packit 258f23
Packit 258f23
       #!/usr/bin/perl
Packit 258f23
Packit 258f23
       use IPC::Run qw(run);
Packit 258f23
       use Fcntl;
Packit 258f23
       use IO::Pty;
Packit 258f23
Packit 258f23
       sub makecmd {
Packit 258f23
           return ['perl', '-e', 
Packit 258f23
                   '<STDIN>, print "\n" x '.$_[0].'; while(<STDIN>){last if /end/}'];
Packit 258f23
       }
Packit 258f23
Packit 258f23
       #pipe R, W;
Packit 258f23
       #fcntl(W, F_SETFL, O_NONBLOCK);
Packit 258f23
       #while (syswrite(W, "\n", 1)) { $pipebuf++ };
Packit 258f23
       #print "pipe buffer size is $pipebuf\n";
Packit 258f23
       my $pipebuf=4096;
Packit 258f23
       my $in = "\n" x ($pipebuf * 2) . "end\n";
Packit 258f23
       my $out;
Packit 258f23
Packit 258f23
       $SIG{ALRM} = sub { die "Never completed!\n" };
Packit 258f23
Packit 258f23
       print "reading from scalar via pipe...";
Packit 258f23
       alarm( 2 );
Packit 258f23
       run(makecmd($pipebuf * 2), '<', \$in, '>', \$out);
Packit 258f23
       alarm( 0 );
Packit 258f23
       print "done\n";
Packit 258f23
Packit 258f23
       print "reading from code via pipe... ";
Packit 258f23
       alarm( 2 );
Packit 258f23
       run(makecmd($pipebuf * 3), '<', sub { $t = $in; undef $in; $t}, '>', \$out);
Packit 258f23
       alarm( 0 );
Packit 258f23
       print "done\n";
Packit 258f23
Packit 258f23
       $pty = IO::Pty->new();
Packit 258f23
       $pty->blocking(0);
Packit 258f23
       $slave = $pty->slave();
Packit 258f23
       while ($pty->syswrite("\n", 1)) { $ptybuf++ };
Packit 258f23
       print "pty buffer size is $ptybuf\n";
Packit 258f23
       $in = "\n" x ($ptybuf * 3) . "end\n";
Packit 258f23
Packit 258f23
       print "reading via pty... ";
Packit 258f23
       alarm( 2 );
Packit 258f23
       run(makecmd($ptybuf * 3), '<pty<', \$in, '>', \$out);
Packit 258f23
       alarm(0);
Packit 258f23
       print "done\n";
Packit 258f23
Packit 258f23
    No support for ';', '&&', '||', '{ ... }', etc: use perl's, since run()
Packit 258f23
    returns TRUE when the command exits with a 0 result code.
Packit 258f23
Packit 258f23
    Does not provide shell-like string interpolation.
Packit 258f23
Packit 258f23
    No support for "cd", "setenv", or "export": do these in an init() sub
Packit 258f23
Packit 258f23
       run(
Packit 258f23
          \cmd,
Packit 258f23
             ...
Packit 258f23
             init => sub {
Packit 258f23
                chdir $dir or die $!;
Packit 258f23
                $ENV{FOO}='BAR'
Packit 258f23
             }
Packit 258f23
       );
Packit 258f23
Packit 258f23
    Timeout calculation does not allow absolute times, or specification of
Packit 258f23
    days, months, etc.
Packit 258f23
Packit 258f23
    WARNING: Function coprocesses ("run \&foo, ...") suffer from two
Packit 258f23
    limitations. The first is that it is difficult to close all filehandles
Packit 258f23
    the child inherits from the parent, since there is no way to scan all
Packit 258f23
    open FILEHANDLEs in Perl and it both painful and a bit dangerous to
Packit 258f23
    close all open file descriptors with "POSIX::close()". Painful because
Packit 258f23
    we can't tell which fds are open at the POSIX level, either, so we'd
Packit 258f23
    have to scan all possible fds and close any that we don't want open
Packit 258f23
    (normally "exec()" closes any non-inheritable but we don't "exec()" for
Packit 258f23
    &sub processes.
Packit 258f23
Packit 258f23
    The second problem is that Perl's DESTROY subs and other on-exit cleanup
Packit 258f23
    gets run in the child process. If objects are instantiated in the parent
Packit 258f23
    before the child is forked, the DESTROY will get run once in the parent
Packit 258f23
    and once in the child. When coprocess subs exit, POSIX::_exit is called
Packit 258f23
    to work around this, but it means that objects that are still referred
Packit 258f23
    to at that time are not cleaned up. So setting package vars or closure
Packit 258f23
    vars to point to objects that rely on DESTROY to affect things outside
Packit 258f23
    the process (files, etc), will lead to bugs.
Packit 258f23
Packit 258f23
    I goofed on the syntax: "<pipe" vs. "<pty<" and ">filename" are both
Packit 258f23
    oddities.
Packit 258f23
Packit 258f23
TODO
Packit 258f23
    Allow one harness to "adopt" another:
Packit 258f23
           $new_h = harness \@cmd2;
Packit 258f23
           $h->adopt( $new_h );
Packit 258f23
Packit 258f23
    Close all filehandles not explicitly marked to stay open.
Packit 258f23
        The problem with this one is that there's no good way to scan all
Packit 258f23
        open FILEHANDLEs in Perl, yet you don't want child processes
Packit 258f23
        inheriting handles willy-nilly.
Packit 258f23
Packit 258f23
INSPIRATION
Packit 258f23
    Well, select() and waitpid() badly needed wrapping, and open3() isn't
Packit 258f23
    open-minded enough for me.
Packit 258f23
Packit 258f23
    The shell-like API inspired by a message Russ Allbery sent to
Packit 258f23
    perl5-porters, which included:
Packit 258f23
Packit 258f23
       I've thought for some time that it would be
Packit 258f23
       nice to have a module that could handle full Bourne shell pipe syntax
Packit 258f23
       internally, with fork and exec, without ever invoking a shell.  Something
Packit 258f23
       that you could give things like:
Packit 258f23
Packit 258f23
       pipeopen (PIPE, [ qw/cat file/ ], '|', [ 'analyze', @args ], '>&3');
Packit 258f23
Packit 258f23
    Message ylln51p2b6.fsf@windlord.stanford.edu, on 2000/02/04.
Packit 258f23
Packit 258f23
SUPPORT
Packit 258f23
    Bugs should always be submitted via the GitHub bug tracker
Packit 258f23
Packit 258f23
    <https://github.com/toddr/IPC-Run/issues>
Packit 258f23
Packit 258f23
AUTHORS
Packit 258f23
    Adam Kennedy <adamk@cpan.org>
Packit 258f23
Packit 258f23
    Barrie Slaymaker <barries@slaysys.com>
Packit 258f23
Packit 258f23
COPYRIGHT
Packit 258f23
    Some parts copyright 2008 - 2009 Adam Kennedy.
Packit 258f23
Packit 258f23
    Copyright 1999 Barrie Slaymaker.
Packit 258f23
Packit 258f23
    You may distribute under the terms of either the GNU General Public
Packit 258f23
    License or the Artistic License, as specified in the README file.
Packit 258f23