Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

stdout

perl

tty

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

from sys import stdout
if stdout.isatty():
    print "yes"
else:
    print "no"
like image 630
bukzor Avatar asked Aug 18 '10 22:08

bukzor


2 Answers

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.

like image 69
Schwern Avatar answered Nov 03 '22 11:11

Schwern


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

like image 26
Pedro Silva Avatar answered Nov 03 '22 11:11

Pedro Silva