Try it again. I modified your 'exec' block to look like this:

open(STDERR, ">&STDOUT") if $redirect_stderr; exec("/usr/bin/nslookup", "-q=$query_type", $input, $nameserver) or die "exec: $!";

I also took out the 'splice' and ran your function like this:

print "Without:\n"; print "| $_\n" foreach nslookup('test', 'A'); print "\n"; $redirect_stderr++; print "With:\n"; print "| $_\n" foreach nslookup('test', 'A');

My output was this:

Without: Note: nslookup is deprecated and may be removed from future releases. Consider using the `dig' or `host' programs instead. Run nslookup wit +h the `-sil[ent]' option to prevent this message from appearing. | *** Invalid option: q=A | Server: ns.intranet | Address: 10.0.0.1#53 | | ** server can't find test: NXDOMAIN With: | Note: nslookup is deprecated and may be removed from future release +s. | Consider using the `dig' or `host' programs instead. Run nslookup w +ith | the `-sil[ent]' option to prevent this message from appearing. | *** Invalid option: q=A | Server: ns.intranet | Address: 10.0.0.1#53 | | ** server can't find test: NXDOMAIN

The 'deprecated' warning is sent out via STDERR, which is being captured in the second run. So it seems to be working perfectly for me.

Note that even though you're doing an exec, file descriptors 0, 1 and 2 (STDIN, STDOUT and STDERR) are inherited by the child process. (This is configurable with $^F; see perlvar.) Since all you're doing here is 'dup'ing 2 to be the same as 1, all 3 get passed as-is and work as you expect.

On a lesser note, if it is this warning you're trying to omit, is there any reason you're still coding your script to use 'nslookup' instead of, as it recommends, 'dig' or 'host'? The 'dig' tool is pretty easily parseable and I think intended for applications like this moreso than nslookup was. Just a thought..


In reply to Re: Re: Re: Capturing STDERR with - by Fastolfe
in thread 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.