A basic Unix process starts life with a default place to
read data from the outside world (STDIN), a default place
to write data (STDOUT), a basic place to write errors to
(STDERR), and some parent process who hopefully will be
interested in how you died and will catch your return code
(0 by default unless you
exit or
die).
Perl has many ways of starting processes. The most common
are the following:
- system: This launches a process which will use your
STDIN, STDOUT, STDERR, and returns you the return code. In
the process the special variables $! and $? will be set.
- Backticks: This launches a process which will use your
STDIN and STDERR, but which will return STDOUT to you and
throw away the return code.
- open(CMD, "| $cmd"): Open a process which
you can write to the STDIN of, which uses your STDOUT and
STDERR. The return of open is the process ID. You can
collect the return with wait or waitpid.
- open(CMD, "$cmd |"): Open a process which
you can read STDOUT from, which uses your STDOUT and
STDERR. Essentially the same as backticks but you can
start processing the data interactively. (This can matter
a lot if the command returns large amounts of data.) The
return of open is the process ID. You can
collect the return with wait or waitpid.
This should suffice for most purposes. (See
perlipc for
more on inter-process communication.) But from time to
time you need more control. That is where IPC::Open3
becomes useful.
IPC::Open3 exports a function, open3. This allows
you to start a command and choose what it gets for STDIN,
STDOUT, and STDERR. For instance you might hand it for
STDIN a filehandle reading from /dev/null to supress any
attempts on its part to interact. You might want to keep
track of STDERR for error reporting. Anything you want.
As with open, open3 returns the process ID. When the
program should have exited you can then call wait or
waitpid, check that you collected the right child, then
look at $? for the return value.
For an example of real use of this, see Run commands in parallel.
Another useful example that would make a good exercise is to
write a function to run a process and surpresses all output
unless the return-code is not zero in which case it prints
an error message with information on the command, the return
code, and what was printed to STDERR. (I use something
like this for cron jobs.)
In reply to IPC::Open3
by tilly
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.