I have a bit of code that looks like this:
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); } }
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:

1) Rewrite with backticks:

@output = `/usr/sbin/nslookup -q=$query_type $input $nameserver 2>&1`;
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?

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


In reply to Capturing STDERR with - by DrManhattan

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • 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:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.