in reply to Re^5: STDERR Restore after redirect
in thread STDERR Restore after redirect

Is this not a test for a file handle?

open my $oldSTDOUT, ">&STDOUT"; open OLDERR, ">&",\*STDERR; open(STDOUT, ">&", $oldSTDOUT) or warn("Can't open STDOUT"); + open(STDERR, ">&OLDERR") or warn("Can't open STDERR");

Neither published a warn

Replies are listed 'Best First'.
Re^7: STDERR Restore after redirect
by AnomalousMonk (Archbishop) on Apr 28, 2018 at 21:38 UTC
    Is this not a test for a file handle?

    I would say no. Neither of these statements indicate failure in any way:
        open my $oldSTDOUT, ">&STDOUT";
        open OLDERR, ">&",\*STDERR;
    AFAIU, both of these statements indicate failure of file handle restoration if it occurs, but give no indication that the handle was valid prior to restoration:
        open(STDOUT, ">&", $oldSTDOUT) or warn("Can't open STDOUT");
        open(STDERR, ">&OLDERR") or warn("Can't open STDERR");

    I still don't really understand the basic problem. Maybe you can find something in the following:

    c:\@Work\Perl\monks>perl -wMstrict -le "open oldSTDOUT, '>&', \*STDOUT or warn qq{duping STDOUT: $!}; print oldSTDOUT 'hi there everybody'; print STDOUT '... and welcome'; print '... to the show.'; if (defined *oldSTDOUT) { warn 'A: *oldSTDOUT is defined'; } ;; close STDOUT or warn qq{closing STDOUT: $!}; print STDOUT 'how now'; print '... brown cow.'; if (defined *STDOUT) { warn 'B: *STDOUT is defined'; } ;; open STDOUT, '>&', \*oldSTDOUT or warn qq{re-opening STDOUT: $!}; print oldSTDOUT 'aloha'; print STDOUT '... sweet dreams'; print '... and goodbye.'; if (defined *oldSTDOUT) { warn 'C: *oldSTDOUT is defined'; } if (defined *STDOUT) { warn 'D: *STDOUT is defined'; } " hi there everybody ... and welcome ... to the show. A: *oldSTDOUT is defined at -e line 1. print() on closed filehandle STDOUT at -e line 1. print() on closed filehandle STDOUT at -e line 1. B: *STDOUT is defined at -e line 1. aloha ... sweet dreams ... and goodbye. C: *oldSTDOUT is defined at -e line 1. D: *STDOUT is defined at -e line 1.


    Give a man a fish:  <%-{-{-{-<