in reply to Test whether STDOUT is connected to a file

Why not simply pass the filename of the file-to-be-written to the program, and let it do the file writing in any case?

On Linux, the -p file test can tell you if STDOUT is connected to a pipe, no idea if that works on windows too (Update: -t is probably more informative than -p).

Perl 6 - links to (nearly) everything that is Perl 6.
  • Comment on Re: Test whether STDOUT is connected to a file

Replies are listed 'Best First'.
Re^2: Test whether STDOUT is connected to a file
by rovf (Priest) on Aug 18, 2010 at 13:29 UTC
    Why not simply pass the filename of the file-to-be-written to the program, and let it do the file writing in any case?
    Because if I do this, I don't get any diagnostics about the error case. Note that I still *guess* what's going on, so for example the information "STDOUT in the error case is still connected to the same stream as in the parent" would be helpful.
    On Linux, the -p file test can tell you if STDOUT is connected to a pipe, no idea if that works on windows too (Update: -t is probably more informative than -p).
    There is an entry in perlfaq8 about this, which is not very encouraging:

    How do I find out if I'm running interactively or not? Good question. Sometimes "-t STDIN" and "-t STDOUT" can give clues, sometimes not. if (-t STDIN && -t STDOUT) { print "Now what? "; }

    -- 
    Ronald Fischer <ynnor@mm.st>
      Because if I do this, I don't get any diagnostics about the error case.

      You could redirect the STDERR to STDOUT in the called perl program, then you wouldn't need to rely upon a broken bourne-shell emulation under windows.