bukzor
bukzor

Reputation: 38462

How do I detect if stdout is connected to a tty in Perl?

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

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

Upvotes: 19

Views: 5269

Answers (2)

Pedro Silva
Pedro Silva

Reputation: 4700

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

Upvotes: 11

Schwern
Schwern

Reputation: 164799

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.

Upvotes: 22

Related Questions