tags:

views:

119

answers:

2

I'm looking for the Perl equivalent to this Python code:

from sys import stdout
if stdout.isatty():
    print "yes"
else:
    print "no"
+5  A: 

Use the -t filetest operator.

print -t STDOUT ? "Yes\n" : "No\n"

Note that in Perl, STDOUT can be tied (essentially an overcomplicated overloaded object) so output to STDOUT may still reach a TTY even if its not directly attached to one.

Schwern
IO::Interactive shows the special cases that you may want to consider too.
brian d foy
How on Earth does this answer have a score of +5, while another (earlier!) post with *exactly* the same answer has a score of -1 ?
Evgeny
@Evgeny That answer was originally `print -t 1 ? "yes\n" : "no\n"`. The unnecessary use of a fileno bought it downvotes.
Schwern
+4  A: 

Use IO::interactive if you require STDOUT to actually be connected to the terminal, and not just being redirected to /dev/null/ or whatever.

Pedro Silva