in reply to Re^2: J.A.P.H. with intentional eval failure
in thread J.A.P.H. with intentional eval failure

perldoc -f open has examples. Basically you duplicate STDOUT and STDERR to other filehandles before closing them. Then you duplicate the saved filehandles back to STDOUT and STDERR.
print "STDOUT #1\n";warn "STDERR #1\n"; open my $savestdout, '>&', \*STDOUT; print "STDOUT #2\n";warn "STDERR #2\n"; open my $savestderr, '>&', \*STDERR; print "STDOUT #3\n";warn "STDERR #3\n"; close STDOUT; print "STDOUT #4\n";warn "STDERR #4\n"; close STDERR; print "STDOUT #5\n";warn "STDERR #5\n"; open STDOUT, '>&', $savestdout; print "STDOUT #6\n";warn "STDERR #6\n"; open STDERR, '>&', $savestderr; print "STDOUT #7\n";warn "STDERR #7\n"; close $savestdout; print "STDOUT #8\n";warn "STDERR #8\n"; close $savestderr; print "STDOUT #9\n";warn "STDERR #9\n";
OK, this is bizarre. When I re-open STDOUT, I get output from warn even though STDERR is still closed. perldoc -f warn doesn't describe this behavior.

Replies are listed 'Best First'.
Re^4: J.A.P.H. with intentional eval failure
by ktross (Deacon) on Mar 02, 2005 at 13:47 UTC

    Thank you for the guidance. There is lots of good stuff in perldocs. Just shows how much of a noob I am to perl that I didn't go straight to perldoc, instead of searching aimlessly online (darn google!).

    I had to change the code you provided slightly to get it to run on my version of perl. (Adding parenthisis, moving quote locations, nothing major) I'm running perl v5.6.0 built for MSWin32-x86-multi-thread, but don't quote me on that. Anyways, here is the results I get.

    STDOUT #1 STDERR #1 STDOUT #2 STDERR #2 STDOUT #3 STDERR #3 STDERR #4 STDOUT #6 STDERR #6 STDOUT #7 STDERR #7 STDOUT #8 STDERR #8 STDOUT #9 STDERR #9

    Like you said, the line 'STDERR #6' shouldn't be printing....hmmm...