DrManhattan has asked for the wisdom of the Perl Monks concerning the following question:
Is there an easy way I can capture STDERR from the child process as well as STDOUT? As I see it, these are my options:sub nslookup { my ($input, $query_type) = @_; # Fork a child process to exec nslookup, # and capture its output if (open(NSLOOKUP, "-|")) { # Parent process. Read output from child. my @output = <NSLOOKUP>; splice(@output, 0, 3); return @output; } else { # Child process. exec() nslookup exec("/usr/sbin/nslookup", "-q=$query_type", $input, $nameserver); } }
1) Rewrite with backticks:
I don't like this because it means I have to send user input into backticks. My whole point in open()ing "-|" was to feed the args into exec() as an array. I'm already scrubbing the input anyway, but better safe than sorry, eh?@output = `/usr/sbin/nslookup -q=$query_type $input $nameserver 2>&1`;
2) Rewrite using open3(). open3() looks evil.
3) Rewrite using Net::DNS instead of executing nslookup. This is the cleanest, most portable option, but it will take more time than this script is worth because I'll have to completely rewrite the output handling.
-Matt
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Capturing STDERR with -
by MZSanford (Curate) on Dec 05, 2001 at 20:41 UTC | |
|
Re: Capturing STDERR with -
by traveler (Parson) on Dec 05, 2001 at 20:52 UTC | |
|
Re: Capturing STDERR with -
by Fastolfe (Vicar) on Dec 06, 2001 at 01:14 UTC | |
by DrManhattan (Chaplain) on Dec 06, 2001 at 18:53 UTC | |
by Fastolfe (Vicar) on Dec 06, 2001 at 20:11 UTC | |
by DrManhattan (Chaplain) on Dec 07, 2001 at 18:59 UTC | |
by Fastolfe (Vicar) on Dec 08, 2001 at 03:00 UTC | |
|
Re: Capturing STDERR with -
by Caillte (Friar) on Dec 05, 2001 at 20:43 UTC | |
|
Re: Capturing STDERR with -
by IlyaM (Parson) on Dec 07, 2001 at 00:10 UTC | |
by DrManhattan (Chaplain) on Dec 07, 2001 at 19:02 UTC | |
by blakem (Monsignor) on Dec 08, 2001 at 01:12 UTC | |
by IlyaM (Parson) on Dec 07, 2001 at 19:15 UTC |