in reply to how can I redirect the output of a program and get the exit code too?
Use backticks (or the qx// quoting mechanism) to launch the program and capture its output. You can obtain the exit code in the variable $?. It requires a bit of munging to pull out the three pieces of information it encodes. See perlvar for more information. The following code should help you get started.
#! /usr/bin/perl -w my @output = `@ARGV`; print "Error code is $?\nOutput is ", @output, "\n";
Run it with the name of the command (and its arguments) you want to execute. If the command you want to run requires its own redirections, or if you need to capture both the standard output and error streams, things get considerably trickier. It can be done albeit with a bit more code.
- another intruder with the mooring of the heat of the Perl
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how can I redirect the output of a program and get the exit code too?
by vikee (Sexton) on Jun 22, 2004 at 14:38 UTC | |
by ambrus (Abbot) on Jun 22, 2004 at 17:34 UTC | |
by vikee (Sexton) on Jun 23, 2004 at 08:26 UTC | |
by ambrus (Abbot) on Jun 23, 2004 at 09:09 UTC | |
by vikee (Sexton) on Jun 23, 2004 at 11:24 UTC | |
|