in reply to Testing whether a file handle is attached to STDOUT

As Corion points out, you can dup stdout to another filehandle, and then close stdout. And then your test would go wonky. Further, you can redirect stdout to a file, either programmatically in your code, or at the command line, myscript.pl > $filename - so what do you want to do in this case?

My suggestion is to avoid all of this as much as possible. Simply accept the -t test. Yes, it may be that $fh actually is STDERR, and -t will return true. Or $fh could be opened against /dev/tty, so you're still sending stuff to the terminal. Do you really want to tell the difference between myscript.pl (sending to stdout), myscript.pl -f /tmp/my.out (sending to a file), and myscript.pl > /tmp/my.out (same as previous one) and myscript.pl -f /dev/tty > /tmp/my.out (sending to the terminal, but stdout is a file)? I do have to say that I'd find it very odd that -t doesn't do what I want it to do.

Replies are listed 'Best First'.
Re^2: Testing whether a file handle is attached to STDOUT
by rovf (Priest) on Jun 16, 2011 at 14:01 UTC

    Actually, the -t test will be wrong in most cases, since for my application usually is not connected to a terminal.

    Your arguments are basically correct - thank you for pointing it out. Fortunately, in my case I have control over whether or not stdout will be dup'ed, so this is no problem.
    -- 
    Ronald Fischer <ynnor@mm.st>