Seth Woolley's Man Viewer

IPC::Run(3) - IPC::Run - system() and background procs w/ piping, redirs, ptys (Unix, Win32) - man 3 IPC::Run

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

IPC::Run(3)           User Contributed Perl Documentation          IPC::Run(3)



NAME
       IPC::Run - system() and background procs w/ piping, redirs, ptys (Unix,
       Win32)

SYNOPSIS
          ## First,a command to run:
             my @cat = qw( cat ) ;

          ## Using run() instead of system():
             use IPC::Run qw( run timeout(1,3x,3x cbreak) ) ;

             run \@cmd, \$in(1,8), \$out, \$err, timeout(1,3x,3x cbreak)( 10 ) or die "cat: $?"

             # Can do I/O to sub refs and filenames, too:
             run \@cmd, "in.txt", \&out, \&err or die "cat: $?"
             run \@cat, "in.txt", '>>', "out.txt", '2>>', "err.txt" ;

             # Redirecting using psuedo-terminals instad of pipes.
             run \@cat, '<pty<', \$in(1,8),  '>pty>', \$out_and_err ;

          ## Scripting subprocesses (like Expect):

             use IPC::Run qw( start pump finish timeout(1,3x,3x cbreak) ) ;

             # Incrementally read(2,n,1 builtins) from / write(1,2) to scalars.
             # $in(1,8) is drained as it is fed to cat's stdin,
             # $out accumulates cat's stdout
             # $err accumulates cat's stderr
             # $h is for "harness".
             my $h = start \@cat, \$in(1,8), \$out, \$err, timeout(1,3x,3x cbreak)( 10 ) ;

             $in(1,8) .= "some input\n" ;
             pump $h until $out =~ /input\n/g ;

             $in(1,8) .= "some more input\n" ;
             pump $h until $out =~ /\G.*more input\n/ ;

             $in(1,8) .= "some final input\n" ;
             finish $h or die "cat returned $?" ;

             warn $err if(3,n) $err ;
             print $out ;         ## All of cat's output

          # Piping between children
             run \@cat, '|', \@gzip ;

          # Multiple children simultaneously (run() blocks until all
          # children exit(3,n,1 builtins), use start() for background execution):
             run \@foo1, '&', \@foo2 ;

          # Calling \&set_up_child in(1,8) the child before it executes the
          # command (only works on systems with true fork() & exec(3,n,1 builtins)())
          # exceptions thrown in(1,8) set_up_child() will be propagated back
          # to the parent and thrown from run().
             run \@cat, \$in(1,8), \$out,
                init => \&set_up_child ;

          # Read from / write(1,2) to file(1,n) handles you open(2,3,n) and close(2,7,n)
             open(2,3,n) IN,  '<in.txt'  or die $! ;
             open(2,3,n) OUT, '>out.txt' or die $! ;
             print OUT "preamble\n" ;
             run \@cat, \*IN, \*OUT or die "cat returned $?" ;
             print OUT "postamble\n" ;
             close(2,7,n) IN ;
             close(2,7,n) OUT ;

          # Create pipes for you to read(2,n,1 builtins) / write(1,2) (like IPC::Open2 & 3).
             $h = start
                \@cat,
                   '<pipe(2,8)', \*IN,
                   '>pipe(2,8)', \*OUT,
                   '2>pipe(2,8)', \*ERR
                or die "cat returned $?" ;
             print IN "some input\n" ;
             close(2,7,n) IN ;
             print <OUT>, <ERR> ;
             finish $h ;

          # Mixing input and output modes
             run \@cat, 'in.txt', \&catch_some_out, \*ERR_LOG ) ;

          # Other redirection constructs
             run \@cat, '>&', \$out_and_err ;
             run \@cat, '2>&1' ;
             run \@cat, '0<&3' ;
             run \@cat, '<&-' ;
             run \@cat, '3<', \$in3 ;
             run \@cat, '4>', \$out4 ;
             # etc.

          # Passing options:
             run \@cat, 'in.txt', debug => 1 ;

          # Call this system's shell, returns TRUE on 0 exit(3,n,1 builtins) code
          # THIS IS THE OPPOSITE SENSE OF system()'s RETURN VALUE
             run "cat a b c" or die "cat returned $?" ;

          # Launch a sub process directly, no shell.  Can't do redirection
          # with this form, it's here to behave like system() with an
          # inverted result.
             $r = run "cat a b c" ;

          # Read from a file(1,n) in(1,8) to a scalar
             run io( "filename", 'r', \$recv ) ;
             run io( \*HANDLE,   'r', \$recv ) ;

DESCRIPTION
       IPC::Run allows you run and interact with child processes using files,
       pipes, and pseudo-ttys.  Both system()-style and scripted usages are
       supported and may be mixed.  Likewise, functional and OO API styles are
       both supported and may be mixed.

       Various redirection operators reminiscent of those seen on common Unix
       and DOS command lines are provided.

       Before digging in(1,8) to the details a few LIMITATIONS are important enough
       to be mentioned right up front:

       Win32 Support
           Win32 support is working but EXPERIMENTAL, but does pass all rele-
           vant tests on NT 4.0.  See "Win32 LIMITATIONS".

       pty Support
           If you need pty support, IPC::Run should work well enough most of
           the time(1,2,n), but IO::Pty is being improved, and IPC::Run will be
           improved to use IO::Pty's new features when it is release.

           The basic problem is that the pty needs to initialize itself before
           the parent writes to the master(5,8) pty, or the data written gets(3,n) lost.
           So IPC::Run does a sleep(1,3)(1) in(1,8) the parent after forking to (hope-
           fully) give the child a chance to run.  This is a kludge that works
           well on non heavily loaded systems :(.

           ptys are not supported yet under Win32, but will be emulated...

       Debugging Tip
           You may use the environment variable "IPCRUNDEBUG" to see what's
           going on under the hood:

              $ IPCRUNDEBUG=basic   myscript     # prints minimal debugging
              $ IPCRUNDEBUG=data    myscript     # prints all data reads/writes
              $ IPCRUNDEBUG=details myscript     # prints lots of low-level details
              $ IPCRUNDEBUG=gory    myscript     # (Win32 only) prints data moving through
                                                 # the helper processes.

       We now return you to your regularly scheduled documentation.

       Harnesses

       Child processes and I/O handles are gathered in(1,8) to a harness, then
       started and run until the processing is finished or aborted.

       run() vs. start(); pump(); finish();

       There are two modes you can run harnesses in: run() functions as an
       enhanced system(), and start()/pump()/finish() allow for background
       processes and scripted interactions with them.

       When using run(), all data to be sent to the harness is set(7,n,1 builtins) up in(1,8)
       advance (though one can feed subprocesses input from subroutine refs to
       get around this limitation). The harness is run and all output is col-
       lected from it, then any child processes are waited for:

          run \@cmd, \<<IN, \$out ;
          blah
          IN

          ## To precompile harnesses and run them later:
          my $h = harness \@cmd, \<<IN, \$out ;
          blah
          IN

          run $h ;

       The background and scripting API is provided by start(), pump(), and
       finish(): start() creates a harness if(3,n) need be (by calling harness())
       and launches any subprocesses, pump() allows you to poll them for
       activity, and finish() then monitors the harnessed activities until
       they complete.

          ## Build the harness, open(2,3,n) all pipes, and launch the subprocesses
          my $h = start \@cat, \$in(1,8), \$out ;
          $in(1,8) = "first input\n" ;

          ## Now do I/O.  start() does no I/O.
          pump $h while length $in(1,8) ;  ## Wait for all input to go

          ## Now do some more I/O.
          $in(1,8) = "second input\n" ;
          pump $h until $out =~ /second input/ ;

          ## Clean up
          finish $h or die "cat returned $?" ;

       You can optionally compile the harness with harness() prior to
       start()ing or run()ing, and you may omit start() between harness() and
       pump().  You might want to do these things if(3,n) you compile your har-
       nesses ahead of time.

       Using regexps to match output

       As shown in(1,8) most of the scripting examples, the read-to-scalar facility
       for gathering subcommand's output is often used with regular expres-
       sions to detect stopping points.  This is because subcommand output
       often arrives in(1,8) dribbles and drabs, often only a character or line at
       a time.  This output is input for the main program and piles up in(1,8)
       variables like the $out and $err in(1,8) our examples.

       Regular expressions can be used to wait for appropriate output in(1,8) sev-
       eral ways.  The "cat" example in(1,8) the previous section demonstrates how
       to pump() until some string(3,n) appears in(1,8) the output.  Here's an example
       that uses "smb" to fetch files from a remote server:

          $h = harness \@smbclient, \$in(1,8), \$out ;

          $in(1,8) = "cd /src\n" ;
          $h->pump until $out =~ /^smb.*> \Z/m ;
          die "error(8,n) cding to /src:\n$out" if(3,n) $out =~ "ERR" ;
          $out = '' ;

          $in(1,8) = "mget *\n" ;
          $h->pump until $out =~ /^smb.*> \Z/m ;
          die "error(8,n) retrieving files:\n$out" if(3,n) $out =~ "ERR" ;

          $in(1,8) = "quit\n" ;
          $h->finish ;

       Notice that we carefully clear(1,3x,3x clrtobot) $out after the first command/response
       cycle? That's because IPC::Run does not delete $out when we continue,
       and we don't want to trip over the old output in(1,8) the second com-
       mand/response cycle.

       Say you want to accumulate all the output in(1,8) $out and analyze it after-
       wards.  Perl offers incremental regular expression matching using the
       "m//gc" and pattern matching idiom and the "\G" assertion.  IPC::Run is
       careful not to disturb the current "pos()" value for scalars it appends
       data to, so we could modify the above so as not to destroy $out by
       adding a couple of "/gc" modifiers.  The "/g" keeps us from tripping
       over the previous prompt and the "/c" keeps us from resetting the prior
       match position if(3,n) the expected prompt doesn't materialize immediately:

          $h = harness \@smbclient, \$in(1,8), \$out ;

          $in(1,8) = "cd /src\n" ;
          $h->pump until $out =~ /^smb.*> \Z/mgc ;
          die "error(8,n) cding to /src:\n$out" if(3,n) $out =~ "ERR" ;

          $in(1,8) = "mget *\n" ;
          $h->pump until $out =~ /^smb.*> \Z/mgc ;
          die "error(8,n) retrieving files:\n$out" if(3,n) $out =~ "ERR" ;

          $in(1,8) = "quit\n" ;
          $h->finish ;

          analyze( $out ) ;

       When using this technique, you may want to preallocate $out to have
       plenty of memory or you may find that the act of growing $out each time(1,2,n)
       new input arrives causes an "O(length($out)^2)" slowdown as $out grows.
       Say we expect no more than 10,000 characters of input at the most.  To
       preallocate memory to $out, do something like:

          my $out = "x" x 10_000 ;
          $out = "" ;

       "perl" will allocate at least 10,000 characters' worth of space, then
       mark the $out as having 0 length without freeing all that yummy RAM.

       Timeouts and Timers

       More than likely, you don't want your subprocesses to run forever, and
       sometimes it's nice(1,2) to know that they're going a little slowly.  Time-
       outs throw exceptions after a some time(1,2,n) has elapsed, timers merely
       cause pump() to return after some time(1,2,n) has elapsed.  Neither is
       reset(1,7,1 tput)/restarted automatically.

       Timeout objects are created by calling timeout(1,3x,3x cbreak)( $interval ) and passing
       the result to run(), start() or harness().  The timeout(1,3x,3x cbreak) period starts
       ticking just after all the child processes have been fork()ed or
       spawn()ed, and are polled for expiration in(1,8) run(), pump() and finish().
       If/when they expire, an exception is thrown.  This is typically useful
       to keep a subprocess from taking too long.

       If a timeout(1,3x,3x cbreak) occurs in(1,8) run(), all child processes will be terminated
       and all file(1,n)/pipe(2,8)/ptty descriptors opened by run() will be closed.
       File descriptors opened by the parent process and passed in(1,8) to run()
       are not closed in(1,8) this event.

       If a timeout(1,3x,3x cbreak) occurs in(1,8) pump(), pump_nb(), or finish(), it's up to you
       to decide whether to kill_kill() all the children or to implement some
       more graceful fallback.  No I/O will be closed in(1,8) pump(), pump_nb() or
       finish() by such an exception (though I/O is often closed down in(1,8) those
       routines during the natural course of events).

       Often an exception is too harsh.  timer( $interval ) creates timer
       objects that merely prevent pump() from blocking forever.  This can be
       useful for detecting stalled I/O or printing a soothing message or "."
       to pacify an anxious user.

       Timeouts and timers can both be restarted at any time(1,2,n) using the timer's
       start() method (this is not the start() that launches subprocesses).
       To restart a timer, you need to keep a reference to the timer:

          ## Start with a nice(1,2) long timeout(1,3x,3x cbreak) to let smbclient connect.  If
          ## pump or finish take too long, an exception will be thrown.

        my $h ;
        eval {
          $h = harness \@smbclient, \$in(1,8), \$out, \$err, ( my $t = timeout(1,3x,3x cbreak) 30 ) ;
          sleep(1,3) 11 ;  # No effect: timer not running yet

          start $h ;
          $in(1,8) = "cd /src\n" ;
          pump $h until ! length $in(1,8) ;

          $in(1,8) = "ls\n" ;
          ## Now use a short timeout(1,3x,3x cbreak), since this should be faster
          $t->start( 5 ) ;
          pump $h until ! length $in(1,8) ;

          $t->start( 10 ) ;  ## Give smbclient a little while to shut down.
          $h->finish ;
        } ;
        if(3,n) ( $@ ) {
          my $x = $@ ;    ## Preserve $@ in(1,8) case another exception occurs
          $h->kill_kill ; ## kill(1,2,1 builtins) it gently, then brutally if(3,n) need be, or just
                          ## brutally on Win32.
          die $x ;
        }

       Timeouts and timers are not checked once the subprocesses are shut
       down; they will not expire in(1,8) the interval between the last valid
       process and when IPC::Run scoops up the processes' result codes, for
       instance.

       Spawning synchronization, child exception propagation

       start() pauses the parent until the child executes the command or CODE
       reference and propagates any exceptions thrown (including exec(3,n,1 builtins)() fail-
       ure) back to the parent.  This has several pleasant effects: any excep-
       tions thrown in(1,8) the child, including exec(3,n,1 builtins)() failure, come flying out of
       start() or run() as though they had ocurred in(1,8) the parent.

       This includes exceptions your code thrown from init subs.  In this
       example:

          eval {
             run \@cmd, init => sub { die "blast it! foiled again!" } ;
          } ;
          print $@ ;

       the exception "blast it! foiled again" will be thrown from the child
       process (preventing the exec(3,n,1 builtins)()) and printed by the parent.

       In situations like

          run \@cmd1, "|", \@cmd2, "|", \@cmd3 ;

       @cmd1 will be initted and exec(3,n,1 builtins)()ed before @cmd2, and @cmd2 before
       @cmd3.  This can save time(1,2,n) and prevent oddball errors emitted by later
       commands when earlier commands fail to execute.  Note that IPC::Run
       doesn't start any commands unless it can find the executables refer-
       enced by all commands.  These executables must pass both the "-f" and
       "-x" tests described in(1,8) perlfunc.

       Another nice(1,2) effect is that init() subs can take their time(1,2,n) doing
       things and there will be no problems caused by a parent continuing to
       execute before a child's init() routine is complete.  Say the init()
       routine needs to open(2,3,n) a socket(2,7,n) or a temp file(1,n) that the parent wants to
       connect to; without this synchronization, the parent will need to
       implement a retry loop to wait for the child to run, since often, the
       parent gets(3,n) a lot of things done before the child's first timeslice is
       allocated.

       This is also quite necessary for pseudo-tty initialization, which needs
       to take place before the parent writes to the child via pty.  Writes
       that occur before the pty is set(7,n,1 builtins) up can get lost.

       A final, minor, nicety is that debugging output from the child will be
       emitted before the parent continues on, making for much clearer debug-
       ging output in(1,8) complex situations.

       The only drawback I can conceive of is that the parent can't continue
       to operate while the child is being initted.  If this ever becomes a
       problem in(1,8) the field, we can implement an option to avoid this behav-
       ior, but I don't expect it to.

       Win32: executing CODE references isn't supported on Win32, see "Win32
       LIMITATIONS" for details.

       Syntax

       run(), start(), and harness() can all take a harness specification as
       input.  A harness specification is either a single string(3,n) to be passed
       to the systems' shell:

          run "echo(1,3x,1 builtins) 'hi there'" ;

       or a list of commands, io operations, and/or timers/timeouts to exe-
       cute.  Consecutive commands must be separated by a pipe(2,8) operator '|' or
       an '&'.  External commands are passed in(1,8) as array references, and, on
       systems supporting fork(), Perl code may be passed in(1,8) as subs:

          run \@cmd ;
          run \@cmd1, '|', \@cmd2 ;
          run \@cmd1, '&', \@cmd2 ;
          run \&sub1 ;
          run \&sub1, '|', \&sub2 ;
          run \&sub1, '&', \&sub2 ;

       '|' pipes the stdout of \@cmd1 the stdin of \@cmd2, just like a shell
       pipe.  '&' does not.  Child processes to the right of a '&' will have
       their stdin closed unless it's redirected-to.

       IPC::Run::IO objects may be passed in(1,8) as well, whether or not child
       processes are also specified:

          run io( "infile", ">", \$in(1,8) ), io( "outfile", "<", \$in(1,8) ) ;

       as can IPC::Run::Timer objects:

          run \@cmd, io( "outfile", "<", \$in(1,8) ), timeout(1,3x,3x cbreak)( 10 ) ;

       Commands may be followed by scalar, sub, or i/o handle references for
       redirecting child process input & output:

          run \@cmd,  \undef,            \$out ;
          run \@cmd,  \$in(1,8),              \$out ;
          run \@cmd1, \&in(1,8), '|', \@cmd2, \*OUT ;
          run \@cmd1, \*IN, '|', \@cmd2, \&out ;

       This is known as succinct redirection syntax, since run(), start() and
       harness(), figure out which file(1,n) descriptor to redirect and how.  File
       descriptor 0 is presumed to be an input for the child process, all oth-
       ers are outputs.  The assumed file(1,n) descriptor always starts at 0,
       unless the command is being piped to, in(1,8) which case it starts at 1.

       To be explicit about your redirects, or if(3,n) you need to do more complex
       things, there's also a redirection operator syntax:

          run \@cmd, '<', \undef, '>',  \$out ;
          run \@cmd, '<', \undef, '>&', \$out_and_err ;
          run(
             \@cmd1,
                '<', \$in(1,8),
             '|', \@cmd2,
                \$out
          ) ;

       Operator syntax is required if(3,n) you need to do something other than sim-
       ple redirection to/from scalars or subs, like duping or closing file(1,n)
       descriptors or redirecting to/from a named(5,8) file.  The operators are
       covered in(1,8) detail below.

       After each \@cmd (or \&foo), parsing begins in(1,8) succinct mode and tog-
       gles to operator syntax mode when an operator (ie plain scalar, not a
       ref) is seen.  Once in(1,8) operator syntax mode, parsing only reverts to
       succinct mode when a '|' or '&' is seen.

       In succinct mode, each parameter after the \@cmd specifies what to do
       with the next highest file(1,n) descriptor. These File descriptor start with
       0 (stdin) unless stdin is being piped to ("'|', \@cmd"), in(1,8) which case
       they start with 1 (stdout).  Currently, being on the left of a pipe(2,8)
       ("\@cmd, \$out, \$err, '|'") does not cause stdout to be skipped,
       though this may change since it's not as DWIMerly as it could be.  Only
       stdin is assumed to be an input in(1,8) succinct mode, all others are
       assumed to be outputs.

       If no piping or redirection is specified for a child, it will inherit
       the parent's open(2,3,n) file(1,n) handles as dictated by your system's close-on-
       exec(3,n,1 builtins) behavior and the $^F flag, except that processes after a '&' will
       not inherit the parent's stdin. Also note that $^F does not affect file(1,n)
       desciptors obtained via POSIX, since it only applies to full-fledged
       Perl file(1,n) handles.  Such processes will have their stdin closed unless
       it has been redirected-to.

       If you want to close(2,7,n) a child processes stdin, you may do any of:

          run \@cmd, \undef ;
          run \@cmd, \"" ;
          run \@cmd, '<&-' ;
          run \@cmd, '0<&-' ;

       Redirection is done by placing redirection specifications immediately
       after a command or child subroutine:

          run \@cmd1,      \$in(1,8), '|', \@cmd2,      \$out ;
          run \@cmd1, '<', \$in(1,8), '|', \@cmd2, '>', \$out ;

       If you omit the redirection operators, descriptors are counted starting
       at 0.  Descriptor 0 is assumed to be input, all others are outputs.  A
       leading '|' consumes descriptor 0, so this works as expected.

          run \@cmd1, \$in(1,8), '|', \@cmd2, \$out ;

       The parameter following a redirection operator can be a scalar ref, a
       subroutine ref, a file(1,n) name, an open(2,3,n) filehandle, or a closed filehan-
       dle.

       If it's a scalar ref, the child reads input from or sends output to
       that variable:

          $in(1,8) = "Hello World.\n" ;
          run \@cat, \$in(1,8), \$out ;
          print $out ;

       Scalars used in(1,8) incremental (start()/pump()/finish()) applications are
       treated as queues: input is removed from input scalers, resulting in(1,8)
       them dwindling to '', and output is appended to output scalars.  This
       is not true of harnesses run() in(1,8) batch mode.

       It's usually wise to append new input to be sent to the child to the
       input queue(1,3), and you'll often want to zap output queues to '' before
       pumping.

          $h = start \@cat, \$in(1,8) ;
          $in(1,8) = "line 1\n" ;
          pump $h ;
          $in(1,8) .= "line 2\n" ;
          pump $h ;
          $in(1,8) .= "line 3\n" ;
          finish $h ;

       The final call to finish() must be there: it allows the child
       process(es) to run to completion and waits for their exit(3,n,1 builtins) values.

OBSTINATE CHILDREN
       Interactive applications are usually optimized for human use.  This can
       help or hinder trying to interact with them through modules like
       IPC::Run.  Frequently, programs alter their behavior when they detect
       that stdin, stdout, or stderr are not connected to a tty(1,4), assuming that
       they are being run in(1,8) batch mode.  Whether this helps or hurts depends
       on which optimizations change.  And there's often no way of telling
       what a program does in(1,8) these areas other than trial and error(8,n) and,
       occasionally, reading the source.  This includes different versions and
       implementations of the same program.

       All hope is not lost, however.  Most programs behave in(1,8) reasonably
       tractable manners, once you figure out what it's trying to do.

       Here are some of the issues you might need to be aware of.

          fflush()ing stdout and stderr

           This lets the user see stdout and stderr immediately.  Many pro-
           grams undo this optimization if(3,n) stdout is not a tty(1,4), making them
           harder to manage by things like IPC::Run.

           Many programs decline to fflush stdout or stderr if(3,n) they do not
           detect a tty(1,4) there.  Some ftp commands do this, for instance.

           If this happens to you, look(1,8,3 Search::Dict) for a way to force interactive behav-
           ior, like a command line switch(1,n) or command.  If you can't, you will
           need to use a pseudo terminal ('<pty<' and '>pty>').

          false prompts

           Interactive programs generally do not guarantee that output from
           user commands won't contain a prompt string.  For example, your
           shell prompt might be a '$', and a file(1,n) named(5,8) '$' might be the only
           file(1,n) in(1,8) a directory listing.

           This can make it hard to guarantee that your output parser won't be
           fooled into early termination of results.

           To help work around this, you can see if(3,n) the program can alter it's
           prompt, and use something you feel is never going to occur in(1,8)
           actual practice.

           You should also look(1,8,3 Search::Dict) for your prompt to be the only thing on a
           line:

              pump $h until $out =~ /^<SILLYPROMPT>\s?\z/m ;

           (use "(?!\n)\Z" in(1,8) place of "\z" on older perls).

           You can also take the approach that IPC::ChildSafe takes and emit a
           command with known output after each 'real' command you issue, then
           look(1,8,3 Search::Dict) for this known output.  See new_appender() and new_chunker()
           for filters that can help with this task.

           If it's not convenient or possibly to alter a prompt or use a known
           command/response pair, you might need to autodetect the prompt in(1,8)
           case the local version(1,3,5) of the child program is different then the
           one you tested with, or if(3,n) the user has control over the look(1,8,3 Search::Dict) &
           feel of the prompt.

          Refusing to accept(2,8) input unless stdin is a tty.

           Some programs, for security reasons, will only accept(2,8) certain types
           of input from a tty.  su, notable, will not prompt for a password
           unless it's connected to a tty.

           If this is your situation, use a pseudo terminal ('<pty<' and
           '>pty>').

          Not prompting unless connected to a tty.

           Some programs don't prompt unless stdin or stdout is a tty.  See if(3,n)
           you can turn prompting back on.  If not, see if(3,n) you can come up
           with a command that you can issue after every real command and look(1,8,3 Search::Dict)
           for it's output, as IPC::ChildSafe does.   There are two filters
           included with IPC::Run that can help with doing this: appender and
           chunker (see new_appender() and new_chunker()).

          Different output format when not connected to a tty.

           Some commands alter their formats to ease machine parsability when
           they aren't connected to a pipe.  This is actually good, but can be
           surprising.

PSEUDO TERMINALS
       On systems providing pseudo terminals under /dev, IPC::Run can use
       IO::Pty (available on CPAN) to provide a terminal environment to sub-
       processes.  This is necessary when the subprocess really wants to think
       it's connected to a real terminal.

       CAVEATS

       Psuedo-terminals are not pipes, though they are similar.  Here are some
       differences to watch out for.

       Echoing
           Sending to stdin will cause an echo(1,3x,1 builtins) on stdout, which occurs before
           each line is passed to the child program.  There is currently no
           way to disable this, although the child process can and should dis-
           able it for things like passwords.

       Shutdown
           IPC::Run cannot close(2,7,n) a pty until all output has been collected.
           This means that it is not possible to send(2,n) an EOF to stdin by half-
           closing the pty, as we can when using a pipe(2,8) to stdin.

           This means that you need to send(2,n) the child process an exit(3,n,1 builtins) command
           or signal(2,7), or run() / finish() will time(1,2,n) out.  Be careful not to
           expect a prompt after sending the exit(3,n,1 builtins) command.

       Command line editing
           Some subprocesses, notable shells that depend on the user's prompt
           settings, will reissue the prompt plus the command line input so
           far once for each character.

       '>pty>' means '&>pty>', not '1>pty>'
           The pseudo terminal redirects both stdout and stderr unless you
           specify a file(1,n) descriptor.  If you want to grab stderr separately,
           do this:

              start \@cmd, '<pty<', \$in(1,8), '>pty>', \$out, '2>', \$err ;

       stdin, stdout, and stderr not inherited
           Child processes harnessed to a pseudo terminal have their stdin,
           stdout, and stderr completely closed before any redirection opera-
           tors take effect.  This casts of the bonds of the controlling ter-
           minal.  This is not done when using pipes.

           Right now, this affects all children in(1,8) a harness that has a pty in(1,8)
           use, even if(3,n) that pty would not affect a particular child.  That's
           a bug and will be fixed.  Until it is, it's best not to mix-and-
           match children.

       Redirection Operators

          Operator       SHNP   Description
          ========       ====   ===========
          <, N<          SHN    Redirects input to a child's fd N (0 assumed)

          >, N>          SHN    Redirects output from a child's fd N (1 assumed)
          >>, N>>        SHN    Like '>', but appends to scalars or named(5,8) files
          >&, &>         SHN    Redirects stdout & stderr from a child process

          <pty, N<pty    S      Like '<', but uses a pseudo-tty instead of a pipe(2,8)
          >pty, N>pty    S      Like '>', but uses a pseudo-tty instead of a pipe(2,8)

          N<&M                  Dups input fd N to input fd M
          M>&N                  Dups output fd N to input fd M
          N<&-                  Closes fd N

          <pipe(2,8), N<pipe(2,8)     P   Pipe opens H for caller to read(2,n,1 builtins), write(1,2), close.
          >pipe(2,8), N>pipe(2,8)     P   Pipe opens H for caller to read(2,n,1 builtins), write(1,2), close.

       'N' and 'M' are placeholders for integer file(1,n) descriptor numbers.  The
       terms 'input' and 'output' are from the child process's perspective.

       The SHNP field indicates what parameters an operator can take:

          S: \$scalar or \&function references.  Filters may be used with
             these operators (and only these).
          H: \*HANDLE or IO::Handle for caller to open(2,3,n), and close(2,7,n)
          N: "file(1,n) name".
          P: \*HANDLE opened by IPC::Run as the parent end of a pipe(2,8), but read(2,n,1 builtins)
             and written to and closed by the caller (like IPC::Open3).

       Redirecting input: [n]<, [n]<pipe(2,8)
           You can input the child reads on file(1,n) descriptor number n to come
           from a scalar variable, subroutine, file(1,n) handle, or a named(5,8) file.
           If stdin is not redirected, the parent's stdin is inherited.

              run \@cat, \undef          ## Closes child's stdin immediately
                 or die "cat returned $?" ;

              run \@cat, \$in(1,8) ;

              run \@cat, \<<TOHERE ;
              blah
              TOHERE

              run \@cat, \&input ;       ## Calls &input, feeding data returned
                                         ## to child's.  Closes child's stdin
                                         ## when undef is returned.

           Redirecting from named(5,8) files requires you to use the input redi-
           rection operator:

              run \@cat, '<.profile' ;
              run \@cat, '<', '.profile' ;

              open(2,3,n) IN, "<foo" ;
              run \@cat, \*IN ;
              run \@cat, *IN{IO} ;

           The form used second example here is the safest, since filenames
           like "0" and "&more\n" won't confuse &run:

           You can't do either of

              run \@a, *IN ;      ## INVALID
              run \@a, '<', *IN ; ## BUGGY: Reads file(1,n) named(5,8) like "*main::A"

           because perl passes a scalar containing a string(3,n) that looks like
           "*main::A" to &run, and &run can't tell the difference between that
           and a redirection operator or a file(1,n) name.  &run guarantees that
           any scalar you pass after a redirection operator is a file(1,n) name.

           If your child process will take input from file(1,n) descriptors other
           than 0 (stdin), you can use a redirection operator with any of the
           valid input forms (scalar ref, sub ref, etc.):

              run \@cat, '3<', \$in3 ;

           When redirecting input from a scalar ref, the scalar ref is used as
           a queue.  This allows you to use &harness and pump() to feed incre-
           mental bits of input to a coprocess.  See "Coprocesses" below for
           more information.

           The <pipe(2,8) operator opens the write(1,2) half of a pipe(2,8) on the filehandle
           glob(1,3,7,n) reference it takes as an argument:

              $h = start \@cat, '<pipe(2,8)', \*IN ;
              print IN "hello world\n" ;
              pump $h ;
              close(2,7,n) IN ;
              finish $h ;

           Unlike the other '<' operators, IPC::Run does nothing further with
           it: you are responsible for it.  The previous example is function-
           ally equivalent to:

              pipe(2,8)( \*R, \*IN ) or die $! ;
              $h = start \@cat, '<', \*IN ;
              print IN "hello world\n" ;
              pump $h ;
              close(2,7,n) IN ;
              finish $h ;

           This is like the behavior of IPC::Open2 and IPC::Open3.

           Win32: The handle returned is actually a socket(2,7,n) handle, so you can
           use select(2,7,2 select_tut)() on it.

       Redirecting output: [n]>, [n]>>, [n]>&[m], [n]>pipe(2,8)
           You can redirect any output the child emits to a scalar variable,
           subroutine, file(1,n) handle, or file(1,n) name.  You can have &run truncate(2,7)
           or append to named(5,8) files or scalars.  If you are redirecting stdin
           as well, or if(3,n) the command is on the receiving end of a pipeline
           ('|'), you can omit the redirection operator:

              @ls = ( 'ls' ) ;
              run \@ls, \undef, \$out
                 or die "ls returned $?" ;

              run \@ls, \undef, \&out ;  ## Calls &out each time(1,2,n) some output
                                         ## is received from the child's
                                         ## when undef is returned.

              run \@ls, \undef, '2>ls.err' ;
              run \@ls, '2>', 'ls.err' ;

           The two parameter form guarantees that the filename will not be
           interpreted as a redirection operator:

              run \@ls, '>', "&more" ;
              run \@ls, '2>', ">foo\n" ;

           You can pass file(1,n) handles you've opened for writing:

              open(2,3,n)( *OUT, ">out.txt" ) ;
              open(2,3,n)( *ERR, ">err.txt" ) ;
              run \@cat, \*OUT, \*ERR ;

           Passing a scalar reference and a code reference requires a little
           more work, but allows you to capture all of the output in(1,8) a scalar
           or each piece of output by a callback:

           These two do the same things:

              run( [ 'ls' ], '2>', sub { $err_out .= $_[0] } ) ;

           does the same basic thing as:

              run( [ 'ls' ], '2>', \$err_out ) ;

           The subroutine will be called each time(1,2,n) some data is read(2,n,1 builtins) from the
           child.

           The >pipe(2,8) operator is different in(1,8) concept than the other '>' oper-
           ators, although it's syntax is similar:

              $h = start \@cat, $in(1,8), '>pipe(2,8)', \*OUT, '2>pipe(2,8)', \*ERR ;
              $in(1,8) = "hello world\n" ;
              finish $h ;
              print <OUT> ;
              print <ERR> ;
              close(2,7,n) OUT ;
              close(2,7,n) ERR ;

           causes two pipe(2,8) to be created, with one end attached to cat's std-
           out and stderr, respectively, and the other left open(2,3,n) on OUT and
           ERR, so that the script can manually read(2,n,1 builtins)(), select(2,7,2 select_tut)(), etc. on
           them.  This is like the behavior of IPC::Open2 and IPC::Open3.

           Win32: The handle returned is actually a socket(2,7,n) handle, so you can
           use select(2,7,2 select_tut)() on it.

       Duplicating output descriptors: >&m, n>&m
           This duplicates output descriptor number n (default is 1 if(3,n) n is
           omitted) from descriptor number m.

       Duplicating input descriptors: <&m, n<&m
           This duplicates input descriptor number n (default is 0 if(3,n) n is
           omitted) from descriptor number m

       Closing descriptors: <&-, 3<&-
           This closes descriptor number n (default is 0 if(3,n) n is omitted).
           The following commands are equivalent:

              run \@cmd, \undef ;
              run \@cmd, '<&-' ;
              run \@cmd, '<in.txt', '<&-' ;

           Doing

              run \@cmd, \$in(1,8), '<&-' ;    ## SIGPIPE recipe.

           is dangerous: the parent will get a SIGPIPE if(3,n) $in(1,8) is not empty.

       Redirecting both stdout and stderr: &>, >&, &>pipe(2,8), >pipe(2,8)&
           The following pairs of commands are equivalent:

              run \@cmd, '>&', \$out ;       run \@cmd, '>', \$out,     '2>&1' ;
              run \@cmd, '>&', 'out.txt' ;   run \@cmd, '>', 'out.txt', '2>&1' ;

           etc.

           File descriptor numbers are not permitted to the left or the right
           of these operators, and the '&' may occur on either end of the
           operator.

           The '&>pipe(2,8)' and '>pipe(2,8)&' variants behave like the '>pipe(2,8)' opera-
           tor, except that both stdout and stderr write(1,2) to the created pipe.

       Redirection Filters
           Both input redirections and output redirections that use scalars or
           subs as endpoints may have an arbitrary number of filter(1,3x,3x curs_util) subs
           placed between them and the child process.  This is useful if(3,n) you
           want to receive output in(1,8) chunks, or if(3,n) you want to massage each
           chunk of data sent to the child.  To use this feature, you must use
           operator syntax:

              run(
                 \@cmd
                    '<', \&in_filter_2, \&in_filter_1, $in(1,8),
                    '>', \&out_filter_1, \&in_filter_2, $out,
              ) ;

           This capability is not provided for IO handles or named(5,8) files.

           Two filters are provided by IPC::Run: appender and chunker.
           Because these may take an argument, you need to use the constructor
           functions new_appender() and new_chunker() rather than using \&
           syntax:

              run(
                 \@cmd
                    '<', new_appender( "\n" ), $in(1,8),
                    '>', new_chunker, $out,
              ) ;

       Just doing I/O

       If you just want to do I/O to a handle or file(1,n) you open(2,3,n) yourself, you
       may specify a filehandle or filename instead of a command in(1,8) the har-
       ness specification:

          run io( "filename", '>', \$recv ) ;

          $h = start io( $io, '>', \$recv ) ;

          $h = harness \@cmd, '&', io( "file(1,n)", '<', \$send(2,n) ) ;

       Options

       Options are passed in(1,8) as name/value pairs:

          run \@cat, \$in(1,8), debug => 1 ;

       If you pass the debug option, you may want to pass it in(1,8) first, so you
       can see what parsing is going on:

          run debug => 1, \@cat, \$in(1,8) ;

       debug
           Enables debugging output in(1,8) parent and child.  Debugging info(1,5,n) is
           emitted to the STDERR that was present when IPC::Run was first
           "use()"ed (it's "dup()"ed out of the way so that it can be redi-
           rected in(1,8) children without having debugging output emitted on it).

RETURN VALUES
       harness() and start() return a reference to an IPC::Run harness.  This
       is blessed in(1,8) to the IPC::Run package, so you may make later calls to
       functions as members if(3,n) you like:

          $h = harness( ... ) ;
          $h->start ;
          $h->pump ;
          $h->finish ;

          $h = start( .... ) ;
          $h->pump ;
          ...

       Of course, using method call syntax lets you deal with any IPC::Run
       subclasses that might crop up, but don't hold your breath waiting for
       any.

       run() and finish() return TRUE when all subcommands exit(3,n,1 builtins) with a 0
       result code.  This is the opposite of perl's system() command.

       All routines raise(3,n) exceptions (via die()) when error(8,n) conditions are
       recognized.  A non-zero command result is not treated as an error(8,n) con-
       dition, since some commands are tests whose results are reported in(1,8)
       their exit(3,n,1 builtins) codes.

ROUTINES
       run Run takes a harness or harness specification and runs it, pumping
           all input to the child(ren), closing the input pipes when no more
           input is available, collecting all output that arrives, until the
           pipes delivering output are closed, then waiting for the children
           to exit(3,n,1 builtins) and reaping their result codes.

           You may think of "run( ... )" as being like

              start( ... )->finish() ;

           , though there is one subtle difference: run() does not set(7,n,1 builtins)
           \$input_scalars to '' like finish() does.  If an exception is
           thrown from run(), all children will be killed off "gently", and
           then "annihilated" if(3,n) they do not go gently (in(1,8) to that dark night.
           sorry).

           If any exceptions are thrown, this does a "kill_kill" before pro-
           pogating them.

       signal(2,7)
              ## To send(2,n) it a specific signal(2,7) by name ("USR1"):
              signal(2,7) $h, "USR1" ;
              $h->signal(2,7) ( "USR1" ) ;

           If $signal(2,7) is provided and defined, sends a signal(2,7) to all child
           processes.  Try not to send(2,n) numeric signals, use "KILL" instead of
           9, for instance.  Numeric signals aren't portable.

           Throws an exception if(3,n) $signal(2,7) is undef.

           This will not clean up the harness, "finish" it if(3,n) you kill(1,2,1 builtins) it.

           Normally TERM kills a process gracefully (this is what the command
           line utility "kill(1,2,1 builtins)" does by default), INT is sent by one of the
           keys "^C", "Backspace" or "<Del>", and "QUIT" is used to kill(1,2,1 builtins) a
           process and make it coredump.

           The "HUP" signal(2,7) is often used to get a process to "restart",
           rereading config(1,5) files, and "USR1" and "USR2" for really applica-
           tion-specific things.

           Often, running "kill(1,2,1 builtins) -l" (that's a lower case "L") on the command
           line will list the signals present on your operating system.

           WARNING: The signal(2,7) subsystem is not at all portable.  We *may*
           offer to simulate "TERM" and "KILL" on some operating systems, sub-
           mit code to me if(3,n) you want this.

           WARNING 2: Up to and including perl v5.6.1, doing almost anything
           in(1,8) a signal(2,7) handler could be dangerous.  The most safe code avoids
           all mallocs and system calls, usually by preallocating a flag
           before entering the signal(2,7) handler, altering the flag's value in(1,8)
           the handler, and responding to the changed value in(1,8) the main sys-
           tem:

              my $got_usr1 = 0 ;
              sub usr1_handler { ++$got_signal }

              $SIG{USR1} = \&usr1_handler ;
              while () { sleep(1,3) 1 ; print "GOT IT" while $got_usr1-- ; }

           Even this approach is perilous if(3,n) ++ and -- aren't atomic on your
           system (I've never heard of this on any modern CPU large enough to
           run perl).

       kill_kill
              ## To kill(1,2,1 builtins) off a process:
              $h->kill_kill ;
              kill_kill $h ;

              ## To specify the grace period other than 30 seconds:
              kill_kill $h, grace => 5 ;

              ## To send(2,n) QUIT instead of KILL if(3,n) a process refuses to die:
              kill_kill $h, coup_d_grace => "QUIT" ;

           Sends a "TERM", waits for all children to exit(3,n,1 builtins) for up to 30 sec-
           onds, then sends a "KILL" to any that survived the "TERM".

           Will wait for up to 30 more seconds for the OS to sucessfully
           "KILL" the processes.

           The 30 seconds may be overriden by setting the "grace" option, this
           overrides both timers.

           The harness is then cleaned up.

           The doubled name indicates that this function may kill(1,2,1 builtins) again and
           avoids colliding with the core Perl "kill(1,2,1 builtins)" function.

           Returns a 1 if(3,n) the "TERM" was sufficient, or a 0 if(3,n) "KILL" was
           required.  Throws an exception if(3,n) "KILL" did not permit the chil-
           dren to be reaped.

           NOTE: The grace period is actually up to 1 second longer than that
           given.  This is because the granularity of "time(1,2,n)" is 1 second.  Let
           me know if(3,n) you need finer granularity, we can leverage Time::HiRes
           here.

           Win32: Win32 does not know how to send(2,n) real signals, so "TERM" is a
           full-force kill(1,2,1 builtins) on Win32.  Thus all talk of grace periods, etc. do
           not apply to Win32.

       harness
           Takes a harness specification and returns a harness.  This harness
           is blessed in(1,8) to IPC::Run, allowing you to use method call syntax
           for run(), start(), et al if(3,n) you like.

           harness() is provided so that you can pre-build harnesses if(3,n) you
           would like to, but it's not required..

           You may proceed to run(), start() or pump() after calling harness()
           (pump() calls start() if(3,n) need be).  Alternatively, you may pass
           your harness specification to run() or start() and let them har-
           ness() for you.  You can't pass harness specifications to pump(),
           though.

       close_terminal
           This is used as (or in(1,8)) an init sub to cast off the bonds of a con-
           trolling terminal.  It must precede all other redirection ops that
           affect STDIN, STDOUT, or STDERR to be guaranteed effective.

       start
              $h = start(
                 \@cmd, \$in(1,8), \$out, ...,
                 timeout(1,3x,3x cbreak)( 30, name => "process timeout(1,3x,3x cbreak)" ),
                 $stall_timeout = timeout(1,3x,3x cbreak)( 10, name => "stall timeout(1,3x,3x cbreak)"   ),
              ) ;

              $h = start \@cmd, '<', \$in(1,8), '|', \@cmd2, ... ;

           start() accepts a harness or harness specification and returns a
           harness after building all of the pipes and launching (via
           fork()/exec(3,n,1 builtins)(), or, maybe someday, spawn()) all the child processes.
           It does not send(2,n) or receive any data on the pipes, see pump() and
           finish() for that.

           You may call harness() and then pass it's result to start() if(3,n) you
           like, but you only need to if(3,n) it helps you structure or tune your
           application.  If you do call harness(), you may skip start() and
           proceed directly to pump.

           start() also starts all timers in(1,8) the harness.  See IPC::Run::Timer
           for more information.

           start() flushes STDOUT and STDERR to help you avoid duplicate out-
           put.  It has no way of asking Perl to flush(8,n) all your open(2,3,n) filehan-
           dles, so you are going to need to flush(8,n) any others you have open.
           Sorry.

           Here's how if(3,n) you don't want to alter the state of $| for your
           filehandle:

              $ofh = select(2,7,2 select_tut) HANDLE ; $of = $| ; $| = 1 ; $| = $of ; select(2,7,2 select_tut) $ofh;

           If you don't mind leaving output unbuffered on HANDLE, you can do
           the slightly shorter

              $ofh = select(2,7,2 select_tut) HANDLE ; $| = 1 ; select(2,7,2 select_tut) $ofh;

           Or, you can use IO::Handle's flush(8,n)() method:

              use IO::Handle ;
              flush(8,n) HANDLE ;

           Perl needs the equivalent of C's fflush( (FILE *)NULL ).

       pump
              pump $h ;
              $h->pump ;

           Pump accepts a single parameter harness.  It blocks until it deliv-
           ers some input or recieves some output.  It returns TRUE if(3,n) there
           is still input or output to be done, FALSE otherwise.

           pump() will automatically call start() if(3,n) need be, so you may call
           harness() then proceed to pump() if(3,n) that helps you structure your
           application.

           If pump() is called after all harnessed activities have completed,
           a "process ended prematurely" exception to be thrown.  This allows
           for simple scripting of external applications without having to add
           lots of error(8,n) handling code at each step of the script:

              $h = harness \@smbclient, \$in(1,8), \$out, $err ;

              $in(1,8) = "cd /foo\n" ;
              $h->pump until $out =~ /^smb.*> \Z/m ;
              die "error(8,n) cding to /foo:\n$out" if(3,n) $out =~ "ERR" ;
              $out = '' ;

              $in(1,8) = "mget *\n" ;
              $h->pump until $out =~ /^smb.*> \Z/m ;
              die "error(8,n) retrieving files:\n$out" if(3,n) $out =~ "ERR" ;

              $h->finish ;

              warn $err if(3,n) $err ;

       pump_nb
              pump_nb $h ;
              $h->pump_nb ;

           "pump() non-blocking", pumps if(3,n) anything's ready to be pumped,
           returns immediately otherwise.  This is useful if(3,n) you're doing some
           long-running task in(1,8) the foreground, but don't want to starve any
           child processes.

       pumpable
           Returns TRUE if(3,n) calling pump() won't throw an immediate "process
           ended prematurely" exception.  This means that there are open(2,3,n) I/O
           channels or active processes. May yield the parent processes' time(1,2,n)
           slice for 0.01 second if(3,n) all pipes are to the child and all are
           paused.  In this case we can't tell if(3,n) the child is dead, so we
           yield the processor and then attempt to reap the child in(1,8) a non-
           blocking way.

       reap_nb
           Attempts to reap child processes, but does not block.

           Does not currently take any parameters, one day it will allow spe-
           cific children to be reaped.

           Only call this from a signal(2,7) handler if(3,n) your "perl" is recent
           enough to have safe signal(2,7) handling (5.6.1 did not, IIRC, but it
           was beign discussed on perl5-porters).  Calling this (or doing any
           significant work) in(1,8) a signal(2,7) handler on older "perl"s is asking
           for seg faults.

       finish
           This must be called after the last start() or pump() call for a
           harness, or your system will accumulate defunct processes and you
           may "leak" file(1,n) descriptors.

           finish() returns TRUE if(3,n) all children returned 0 (and were not sig-
           naled and did not coredump, ie ! $?), and FALSE otherwise (this is
           like run(), and the opposite of system()).

           Once a harness has been finished, it may be run() or start()ed
           again, including by pump()s auto-start.

           If this throws an exception rather than a normal exit(3,n,1 builtins), the harness
           may be left in(1,8) an unstable state, it's best to kill(1,2,1 builtins) the harness to
           get rid of all the child processes, etc.

           Specifically, if(3,n) a timeout(1,3x,3x cbreak) expires in(1,8) finish(), finish() will not
           kill(1,2,1 builtins) all the children.  Call "<$h-"kill_kill>> in(1,8) this case if(3,n) you
           care.  This differs from the behavior of "run".

       result
              $h->result ;

           Returns the first non-zero result code (ie $? >> 8).  See
           "full_result" to get the $? value for a child process.

           To get the result of a particular child, do:

              $h->result( 0 ) ;  # first child's $? >> 8
              $h->result( 1 ) ;  # second child

           or

              ($h->results)[0]
              ($h->results)[1]

           Returns undef if(3,n) no child processes were spawned and no child num-
           ber was specified.  Throws an exception if(3,n) an out-of-range child
           number is passed.

       results
           Returns a list of child exit(3,n,1 builtins) values.  See "full_results" if(3,n) you
           want to know if(3,n) a signal(2,7) killed the child.

           Throws an exception if(3,n) the harness is not in(1,8) a finished state.

       full_result
              $h->full_result ;

           Returns the first non-zero $?.  See "result" to get the first $? >>
           8 value for a child process.

           To get the result of a particular child, do:

              $h->full_result( 0 ) ;  # first child's $? >> 8
              $h->full_result( 1 ) ;  # second child

           or

              ($h->full_results)[0]
              ($h->full_results)[1]

           Returns undef if(3,n) no child processes were spawned and no child num-
           ber was specified.  Throws an exception if(3,n) an out-of-range child
           number is passed.

       full_results
           Returns a list of child exit(3,n,1 builtins) values as returned by "wait".  See
           "results" if(3,n) you don't care about coredumps or signals.

           Throws an exception if(3,n) the harness is not in(1,8) a finished state.

FILTERS
       These filters are used to modify input our output between a child
       process and a scalar or subroutine endpoint.

       binary
              run \@cmd, ">", binary, \$out ;
              run \@cmd, ">", binary, \$out ;  ## Any TRUE value to enable
              run \@cmd, ">", binary 0, \$out ;  ## Any FALSE value to disable

           This is a constructor for a "binmode" "filter(1,3x,3x curs_util)" that tells IPC::Run
           to keep the carriage returns that would ordinarily be edited out
           for you (binmode is usually off).  This is not a real filter(1,3x,3x curs_util), but
           an option masquerading as a filter.

           It's not named(5,8) "binmode" because you're likely to want to call
           Perl's binmode in(1,8) programs that are piping binary data around.

       new_chunker
           This breaks a stream of data in(1,8) to chunks, based on an optional
           scalar or regular expression parameter.  The default is the Perl
           input record separator in(1,8) $/, which is a newline be default.

              run \@cmd, '>', new_chunker, \&lines_handler ;
              run \@cmd, '>', new_chunker( "\r\n" ), \&lines_handler ;

           Because this uses $/ by default, you should always pass in(1,8) a param-
           eter if(3,n) you are worried about other code (modules, etc) modifying
           $/.

           If this filter(1,3x,3x curs_util) is last in(1,8) a filter(1,3x,3x curs_util) chain that dumps in(1,8) to a scalar,
           the scalar must be set(7,n,1 builtins) to '' before a new chunk will be written to
           it.

           As an example of how a filter(1,3x,3x curs_util) like this can be written, here's a
           chunker that splits on newlines:

              sub line_splitter {
                 my ( $in_ref, $out_ref ) = @_ ;

                 return 0 if(3,n) length $$out_ref ;

                 return input_avail && do {
                    while (1) {
                       if(3,n) ( $$in_ref =~ s/\A(.*?\n)// ) {
                          $$out_ref .= $1 ;
                          return 1 ;
                       }
                       my $hmm = get_more_input ;
                       unless ( defined $hmm ) {
                          $$out_ref = $$in_ref ;
                          $$in_ref = '' ;
                          return length $$out_ref ? 1 : 0 ;
                       }
                       return 0 if(3,n) $hmm eq 0 ;
                    }
                 }
              } ;

       new_appender
           This appends a fixed string(3,n) to each chunk of data read(2,n,1 builtins) from the
           source scalar or sub.  This might be useful if(3,n) you're writing com-
           mands to a child process that always must end in(1,8) a fixed string(3,n),
           like "\n":

              run( \@cmd,
                 '<', new_appender( "\n" ), \&commands,
              ) ;

           Here's a typical filter(1,3x,3x curs_util) sub that might be created by new_appen-
           der():

              sub newline_appender {
                 my ( $in_ref, $out_ref ) = @_ ;

                 return input_avail && do {
                    $$out_ref = join(1,n)( '', $$out_ref, $$in_ref, "\n" ) ;
                    $$in_ref = '' ;
                    1 ;
                 }
              } ;

       io  Takes a filename or filehandle, a redirection operator, optional
           filters, and a source or destination (depends on the redirection
           operator).  Returns an IPC::Run::IO object suitable for har-
           ness()ing (including via start() or run()).

           This is shorthand for

              require IPC::Run::IO ;

                 ... IPC::Run::IO->new(...) ...

       timer
              $h = start( \@cmd, \$in(1,8), \$out, $t = timer( 5 ) ) ;

              pump $h until $out =~ /expected stuff/ || $t->is_expired ;

           Instantiates a non-fatal timer.  pump() returns once each time(1,2,n) a
           timer expires.  Has no direct effect on run(), but you can pass a
           subroutine to fire when the timer expires.

           See "timeout(1,3x,3x cbreak)" for building timers that throw exceptions on expira-
           tion.

           See "timer" in(1,8) IPC::Run::Timer for details.

       timeout(1,3x,3x cbreak)
              $h = start( \@cmd, \$in(1,8), \$out, $t = timeout(1,3x,3x cbreak)( 5 ) ) ;

              pump $h until $out =~ /expected stuff/ ;

           Instantiates a timer that throws an exception when it expires.  If
           you don't provide an exception, a default exception that matches
           /^IPC::Run: .*timed out/ is thrown by default.  You can pass in(1,8)
           your own exception scalar or reference:

              $h = start(
                 \@cmd, \$in(1,8), \$out,
                 $t = timeout(1,3x,3x cbreak)( 5, exception => 'slowpoke' ),
              ) ;

           or set(7,n,1 builtins) the name used in(1,8) debugging message and in(1,8) the default excep-
           tion string:

              $h = start(
                 \@cmd, \$in(1,8), \$out,
                 timeout(1,3x,3x cbreak)( 50, name => 'process timer' ),
                 $stall_timer = timeout(1,3x,3x cbreak)( 5, name => 'stall timer' ),
              ) ;

              pump $h until $out =~ /started/ ;

              $in(1,8) = 'command 1' ;
              $stall_timer->start ;
              pump $h until $out =~ /command 1 finished/ ;

              $in(1,8) = 'command 2' ;
              $stall_timer->start ;
              pump $h until $out =~ /command 2 finished/ ;

              $in(1,8) = 'very slow command 3' ;
              $stall_timer->start( 10 ) ;
              pump $h until $out =~ /command 3 finished/ ;

              $stall_timer->start( 5 ) ;
              $in(1,8) = 'command 4' ;
              pump $h until $out =~ /command 4 finished/ ;

              $stall_timer->reset(1,7,1 tput); # Prevent restarting or expirng
              finish $h ;

           See "timer" for building non-fatal timers.

           See "timer" in(1,8) IPC::Run::Timer for details.

FILTER IMPLEMENTATION FUNCTIONS
       These functions are for use from within filters.

       input_avail
           Returns TRUE if(3,n) input is available.  If none is available, then
           &get_more_input is called and its result is returned.

           This is usually used in(1,8) preference to &get_more_input so that the
           calling filter(1,3x,3x curs_util) removes all data from the $in_ref before more data
           gets(3,n) read(2,n,1 builtins) in(1,8) to $in_ref.

           "input_avail" is usually used as part of a return expression:

              return input_avail && do {
                 ## process the input just gotten
                 1 ;
              } ;

           This technique allows input_avail to return the undef or 0 that a
           filter(1,3x,3x curs_util) normally returns when there's no input to process.  If a
           filter(1,3x,3x curs_util) stores intermediate values, however, it will need to react
           to an undef:

              my $got = input_avail ;
              if(3,n) ( ! defined $got ) {
                 ## No more input ever, flush(8,n) internal buffers to $out_ref
              }
              return $got unless $got ;
              ## Got some input, move(3x,7,3x curs_move) as much as need be
              return 1 if(3,n) $added_to_out_ref ;

       get_more_input
           This is used to fetch more input in(1,8) to the input variable.  It
           returns undef if(3,n) there will never be any more input, 0 if(3,n) there is
           none now, but there might be in(1,8) the future, and TRUE if(3,n) more input
           was gotten.

           "get_more_input" is usually used as part of a return expression,
           see "input_avail" for more information.

TODO
       These will be addressed as needed and as time(1,2,n) allows.

       Stall timeout.

       Expose a list of child process objects.  When I do this, each child
       process is likely to be blessed into IPC::Run::Proc.

       $kid->abort(3,7)(), $kid->kill(1,2,1 builtins)(), $kid->signal(2,7)( $num_or_name ).

       Write tests for /(full_)?results?/ subs.

       Currently, pump() and run() only work on systems where select(2,7,2 select_tut)() works
       on the filehandles returned by pipe(2,8)().  This does *not* include
       ActiveState on Win32, although it does work on cygwin under Win32
       (thought the tests whine a bit).  I'd like to rectify that, suggestions
       and patches welcome.

       Likewise start() only fully works on fork()/exec(3,n,1 builtins)() machines (well, just
       fork() if(3,n) you only ever pass perl subs as subprocesses).  There's some
       scaffolding for calling Open3::spawn_with_handles(), but that's
       untested, and not that useful with limited select(2,7,2 select_tut)().

       Support for "\@sub_cmd" as an argument to a command which gets(3,n) replaced
       with /dev/fd or the name of a temporary file(1,n) containing foo's output.
       This is like <(sub_cmd ...) found in(1,8) bash and csh (IIRC).

       Allow multiple harnesses to be combined as independant sets of pro-
       cesses in(1,8) to one 'meta-harness'.

       Allow a harness to be passed in(1,8) place of an \@cmd.  This would allow
       multiple harnesses to be aggregated.

       Ability to add external file(1,n) descriptors w/ filter(1,3x,3x curs_util) chains and end-
       points.

       Ability to add timeouts and timing generators (i.e. repeating time-
       outs).

       High resolution timeouts.

Win32 LIMITATIONS
       Fails on Win9X
           If you want Win9X support, you'll have to debug it or fund me
           because I don't use that system any more.  The Win32 subsysem has
           been extended to use temporary files in(1,8) simple run() invocations
           and these may actually work on Win9X too, but I don't have time(1,2,n) to
           work on it.

       May deadlock on Win2K (but not WinNT4 or WinXPPro)
           Spawning more than one subprocess on Win2K causes a deadlock I
           haven't figured out yet, but simple uses of run() often work.
           Passes all tests on WinXPPro and WinNT.

       no support yet for <pty< and >pty>
           These are likely to be implemented as "<" and ">" with binmode on,
           not sure.

       no support for file(1,n) descriptors higher than 2 (stderr)
           Win32 only allows passing explicit fds 0, 1, and 2.  If you really,
           really need to pass file(1,n) handles, us Win32API:: GetOsFHandle() or
           ::FdGetOsFHandle() to get the integer handle and pass it to the
           child process using the command line, environment, stdin, interme-
           diary file(1,n), or other IPC mechnism.  Then use that handle in(1,8) the
           child (Win32API.pm provides ways to reconstitute Perl file(1,n) handles
           from Win32 file(1,n) handles).

       no support for subroutine subprocesses (CODE refs)
           Can't fork(), so the subroutines would have no context, and clo-
           sures certainly have no meaning

           Perhaps with Win32 fork() emulation, this can be supported in(1,8) a
           limited fashion, but there are other very serious problems with
           that: all parent fds get dup()ed in(1,8) to the thread emulating the
           forked process, and that keeps the parent from being able to close(2,7,n)
           all of the appropriate fds.

       no support for init => sub {} routines.
           Win32 processes are created from scratch, there is no way to do an
           init routine that will affect the running child.  Some limited sup-
           port might be implemented one day, do chdir() and %ENV changes can
           be made.

       signals
           Win32 does not fully support signals.  signal(2,7)() is likely to cause
           errors unless sending a signal(2,7) that Perl emulates, and
           "kill_kill()" is immediately fatal (there is no grace period).

       helper processes
           IPC::Run uses helper processes, one per redirected file(1,n), to adapt
           between the anonymous pipe(2,8) connected to the child and the TCP
           socket(2,7,n) connected to the parent.  This is a waste of resources and
           will change in(1,8) the future to either use threads (instead of helper
           processes) or a WaitForMultipleObjects call (instead of select(2,7,2 select_tut)).
           Please contact me if(3,n) you can help with the WaitForMultipleObjects()
           approach; I haven't figured out how to get at it without C code.

       shutdown(2,8) pause
           There seems to be a pause of up to 1 second between when a child
           program exits and the corresponding sockets indicate that they are
           closed in(1,8) the parent.  Not sure why.

       binmode
           binmode is not supported yet.  The underpinnings are implemented,
           just ask if(3,n) you need it.

       IPC::Run::IO
           IPC::Run::IO objects can be used on Unix to read(2,n,1 builtins) or write(1,2) arbitrary
           files.  On Win32, they will need to use the same helper processes
           to adapt from non-select(2,7,2 select_tut)()able filehandles to select(2,7,2 select_tut)()able ones (or
           perhaps WaitForMultipleObjects() will work with them, not sure).

       startup race conditions
           There seems to be an occasional race condition between child
           process startup and pipe(2,8) closings.  It seems like if(3,n) the child is
           not fully created by the time(1,2,n) CreateProcess returns and we close(2,7,n)
           the TCP socket(2,7,n) being handed to it, the parent socket(2,7,n) can also get
           closed.  This is seen with the Win32 pumper applications, not the
           "real" child process being spawned.

           I assume this is because the kernel hasn't gotten around to incre-
           menting the reference count on the child's end (since the child was
           slow in(1,8) starting), so the parent's closing of the child end causes
           the socket(2,7,n) to be closed, thus closing the parent socket.

           Being a race condition, it's hard to reproduce, but I encountered
           it while testing this code on a drive share to a samba box.  In
           this case, it takes t/run.t a long time(1,2,n) to spawn it's chile pro-
           cesses (the parent hangs in(1,8) the first select(2,7,2 select_tut) for several seconds
           until the child emits any debugging output).

           I have not seen it on local drives, and can't reproduce it at will,
           unfortunately.  The symptom is a "bad file(1,n) descriptor in(1,8) select(2,7,2 select_tut)()"
           error(8,n), and, by turning on debugging, it's possible to see that
           select(2,7,2 select_tut)() is being called on a no longer open(2,3,n) file(1,n) descriptor that
           was returned from the _socket() routine in(1,8) Win32Helper.  There's a
           new confess() that checks for this ("PARENT_HANDLE no longer
           open(2,3,n)"), but I haven't been able to reproduce it (typically).

LIMITATIONS
       On Unix, requires a system that supports "waitpid( $pid, WNOHANG )" so
       it can tell if(3,n) a child process is still running.

       PTYs don't seem to be non-blocking on some versions of Solaris. Here's
       a test script contributed by Borislav Deianov <borislav@ensim.com> to
       see if(3,n) you have the problem.  If it dies, you have the problem.

          #!/usr/bin/perl

          use IPC::Run qw(run);
          use Fcntl;
          use IO::Pty;

          sub makecmd {
              return ['perl', '-e',
                      '<STDIN>, print "\n" x '.$_[0].'; while(<STDIN>){last if(3,n) /end/}'];
          }

          #pipe(2,8) R, W;
          #fcntl(W, F_SETFL, O_NONBLOCK);
          #while (syswrite(W, "\n", 1)) { $pipebuf++ };
          #print "pipe(2,8) buffer size is $pipebuf\n";
          my $pipebuf=4096;
          my $in(1,8) = "\n" x ($pipebuf * 2) . "end\n";
          my $out;

          $SIG{ALRM} = sub { die "Never completed!\n" } ;

          print "reading from scalar via pipe...";
          alarm(1,2)( 2 ) ;
          run(makecmd($pipebuf * 2), '<', \$in(1,8), '>', \$out);
          alarm(1,2)( 0 );
          print "done\n";

          print "reading from code via pipe... ";
          alarm(1,2)( 2 ) ;
          run(makecmd($pipebuf * 3), '<', sub { $t = $in(1,8); undef $in(1,8); $t}, '>', \$out);
          alarm(1,2)( 0 ) ;
          print "done\n";

          $pty = IO::Pty->new();
          $pty->blocking(0);
          $slave = $pty->slave();
          while ($pty->syswrite("\n", 1)) { $ptybuf++ };
          print "pty buffer size is $ptybuf\n";
          $in(1,8) = "\n" x ($ptybuf * 3) . "end\n";

          print "reading via pty... ";
          alarm(1,2)( 2 ) ;
          run(makecmd($ptybuf * 3), '<pty<', \$in(1,8), '>', \$out);
          alarm(1,2)(0);
          print "done\n";

       No support for ';', '&&', '||', '{ ... }', etc: use perl's, since run()
       returns TRUE when the command exits with a 0 result code.

       Does not provide shell-like string(3,n) interpolation.

       No support for "cd", "setenv", or "export": do these in(1,8) an init() sub

          run(
             \cmd,
                ...
                init => sub {
                   chdir $dir or die $! ;
                   $ENV{FOO}='BAR'
                }
          ) ;

       Timeout calculation does not allow absolute times, or specification of
       days, months, etc.

       WARNING: Function coprocesses ("run \&foo, ...") suffer from two limi-
       tations.  The first is that it is difficult to close(2,7,n) all filehandles
       the child inherits from the parent, since there is no way to scan all
       open(2,3,n) FILEHANDLEs in(1,8) Perl and it both painful and a bit dangerous to
       close(2,7,n) all open(2,3,n) file(1,n) descriptors with "POSIX::close()". Painful because
       we can't tell which fds are open(2,3,n) at the POSIX level, either, so we'd
       have to scan all possible fds and close(2,7,n) any that we don't want open(2,3,n)
       (normally "exec(3,n,1 builtins)()" closes any non-inheritable but we don't "exec(3,n,1 builtins)()" for
       &sub processes.

       The second problem is that Perl's DESTROY subs and other on-exit
       cleanup gets(3,n) run in(1,8) the child process.  If objects are instantiated in(1,8)
       the parent before the child is forked, the the DESTROY will get run
       once in(1,8) the parent and once in(1,8) the child.  When coprocess subs exit(3,n,1 builtins),
       POSIX::exit is called to work around this, but it means that objects
       that are still referred to at that time(1,2,n) are not cleaned up.  So setting
       package vars or closure vars to point to objects that rely on DESTROY
       to affect things outside the process (files, etc), will lead to bugs.

       I goofed on the syntax: "<pipe(2,8)" vs. "<pty<" and ">filename" are both
       oddities.

TODO
       Allow one harness to "adopt" another:
              $new_h = harness \@cmd2 ;
              $h->adopt( $new_h ) ;

       Close all filehandles not explicitly marked to stay open.
           The problem with this one is that there's no good way to scan all
           open(2,3,n) FILEHANDLEs in(1,8) Perl, yet you don't want child processes inher-
           iting handles willy-nilly.

INSPIRATION
       Well, select(2,7,2 select_tut)() and waitpid() badly needed wrapping, and open3() isn't
       open-minded enough for me.

       The shell-like API inspired by a message Russ Allbery sent to
       perl5-porters, which included:

          I've thought for some time(1,2,n) that it would be
          nice(1,2) to have a module that could handle full Bourne shell pipe(2,8) syntax
          internally, with fork and exec(3,n,1 builtins), without ever invoking a shell.  Something
          that you could give things like:

          pipeopen (PIPE, [ qw/cat file(1,n)/ ], '|', [ 'analyze', @args ], '>&3');

       Message ylln51p2b6.fsf@windlord.stanford.edu, on 2000/02/04.

AUTHOR
       Barrie Slaymaker <barries@slaysys.com>, with numerous suggestions by
       p5p.



perl v5.8.5                       2003-09-26                       IPC::Run(3)

References for this manual (incoming links)