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.
In reply to Re: Testing whether a file handle is attached to STDOUT
by Tanktalus
in thread Testing whether a file handle is attached to STDOUT
by rovf
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |