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

if(!defined('STDOUT')){ ... }

The strings  'STDOUT' and  'STDERR' are literal strings and not file handles. Strings are always defined. Even '' (the empty string) and '0', both of which are false, are both defined.

I'm not sure of the best way to test if a file handle is functional, nor am I quite sure what "functional" means in this context.


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

Replies are listed 'Best First'.
Re^6: STDERR Restore after redirect
by tultalk (Monk) on Apr 28, 2018 at 20:13 UTC

    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

      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:  <%-{-{-{-<