in reply to Re: Re: Capturing STDERR with -
in thread Capturing STDERR with -

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..

Replies are listed 'Best First'.
Re: Re: Re: Re: Capturing STDERR with -
by DrManhattan (Chaplain) on Dec 07, 2001 at 18:59 UTC
    Interesting. What operating system are you using? This doesn't work under Solaris 8. I added the open() statement to my script again and ran this:
    print "OUTPUT: $_" for nslookup("blah.", "A");
    Here's the output:
    *** ns1.coxmail.com can't find blah.: Non-existent host/domain OUTPUT: Server: ns1.coxmail.com OUTPUT: Address: 206.157.231.13 OUTPUT:
    STDERR still goes to my console.

    Regarding the splice, it's to get rid of the three lines you see output there (Server, Address, and a blank). 'host' and 'dig' do not ship with Solaris 8.

    -Matt

      Both Linux/2.4 (i686) and Solaris 7 (SunOS 5.6, Sparc) work as expected with the code and output I tried, under Perl 5.6.1.