I\'m new to Perl and want to know of a way to run an external command (call it prg) in the following scenarios:
prg, get its
In most cases you can use the qx// operator (or backticks). It interpolates strings and executes them with the shell, so you can use redirections.
To capture a command's STDOUT (STDERR is unaffected):
$output = `cmd`;
To capture a command's STDERR and STDOUT together:
$output = `cmd 2>&1`;
To capture a command's STDERR but discard its STDOUT (ordering is important here):
$output = `cmd 2>&1 1>/dev/null`;
To exchange a command's STDOUT and STDERR in order to capture the STDERR but leave its STDOUT to come out the old STDERR:
$output = `cmd 3>&1 1>&2 2>&3 3>&-`;
To read both a command's STDOUT and its STDERR separately, it's easiest to redirect them separately to files, and then read from those files when the program is done:
system("program args 1>program.stdout 2>program.stderr");