jdalbec has asked for the wisdom of the Perl Monks concerning the following question:

The "#4" warnings in the following code produce no output in Perl 5.6 because STDERR is not open. The "#4" warnings do produce output in Perl 5.8 even though STDERR is not open. Is this documented somewhere?
warn "#1: warning"; print "#1: STDOUT is open\n"; print STDERR "#1: STDERR is open\n"; warn "#1: warning"; open FILE, '>&STDOUT'; close STDOUT; warn "#2: warning"; print "#2: STDOUT is open\n"; print STDERR "#2: STDERR is open\n"; warn "#2: warning"; close STDERR; warn "#3: warning"; print "#3: STDOUT is open\n"; print STDERR "#3: STDERR is open\n"; warn "#3: warning"; open STDOUT, '>&FILE'; close FILE; warn "#4: warning"; print "#4: STDOUT is open\n"; print STDERR "#4: STDERR is open\n"; warn "#4: warning";

Replies are listed 'Best First'.
Re: Perl 5.8: warn sometimes writes to STDOUT?
by jpeg (Chaplain) on Mar 01, 2005 at 03:50 UTC
    With ActiveState perl 5.8.4 on Win and Slackware's 5.8.0 and 5.8.4, your code produces the same results: the warn statements go to main::STDOUT, and the print statement produces an error on main::STDOUT.

    From perldoc -f print:

    ... If FILEHANDLE is omitted, prints by default to standard output (or to the last selected output channel--see "select"). ...
    So I added some debugging statements like so:
    $fh = select; print("currently selected FILEHANDLE:$fh\n");
    which gives:
    #2: warning at C:\warntest.pl line 16. #4: warning at C:\warntest.pl line 28. currently selected FILEHANDLE: main::STDOUT #4: STDOUT is open currently selected FILEHANDLE: main::STDOUT print() on closed filehandle STDERR at C:\warntest.pl line 34. currently selected FILEHANDLE: main::STDOUT #4: warning at C:\warntest.pl line 37. currently selected FILEHANDLE: main::STDOUT
    Hope this helps.
Re: Perl 5.8: warn sometimes writes to STDOUT?
by chas (Priest) on Mar 01, 2005 at 03:13 UTC
    On the machine I'm using right now (Windows 98), the behaviour of your code seems identical using perl v5.6.1 and perl v5.8.0. (I checked the output to STDOUT by redirecting the output to a file using >. This doesn't seem to capture output to STDERR as far as I can tell.) However, I have noticed some that on some other machines (also using Windows 98, but possibly not the same edition - but the same perl), the behavior of output to STDOUT and STDERR seems different (using identical code, of course.) For example, backquotes act differently - on one machine the code $result=`dir` causes the directory listing to be displayed on the screen, but $result isn't set!
    I've always been perplexed about this. I am using binary makes of Perl that I downloaded via CPAN. I've wondered if the differences could be due to differences in how the various machines and/or OS editions implement shells; (there certainly are some differences when one goes to Windows XP.) This isn't a complete answer to your question, I know. If anyone has any comments about my remarks or the original poster's, I'd be very interested.
    chas
Re: Perl 5.8: warn sometimes writes to STDOUT?
by Ultra (Hermit) on Mar 01, 2005 at 15:41 UTC
    Running this on GNU/Linux, perl v5.8.5

    printing just what goes to STDERR
    05:33 PM george@gnistorica george $ perl 435258.pl 1> /dev/null #1: warning at 435258.pl line 9. #1: STDERR is open #1: warning at 435258.pl line 12. #2: warning at 435258.pl line 17. #2: STDERR is open #2: warning at 435258.pl line 20.

    printing just what goes to STDOUT

    05:33 PM george@gnistorica george $ perl 435258.pl 2> /dev/null #1: STDOUT is open #4: warning at 435258.pl line 32. #4: STDOUT is open #4: warning at 435258.pl line 35.

    So i assume that as soon you have opened STDOUT, warn uses it to spell out warnings.
    otherwise, print STDERR ... does nothing.
    Dodge This!
Re: Perl 5.8: warn sometimes writes to STDOUT?
by bluto (Curate) on Mar 01, 2005 at 23:29 UTC
    On Mac OS X with 5.8.1 and AIX with 5.8.5 I get the expected output.

    ### stdout #1: STDOUT is open #4: STDOUT is open ### stderr #1: warning at x.pl line 1. #1: STDERR is open #1: warning at x.pl line 4. #2: warning at x.pl line 9. #2: STDERR is open #2: warning at x.pl line 12.

    FWIW, normally in C it's not a good idea to print to a closed file handle, but I don't know what perl does about this wrt STDOUT/STDERR. The only workaround I can think of might be to reopen STDERR/STDOUT to /dev/null as needed rather than just close them.