如何检测stdout是否连接到Perl中的tty?

时间:2021-09-17 06:10:47

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

我正在寻找相当于这个Python代码的Perl:

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

2 个解决方案

#1


15  

Use the -t filetest operator.

使用-t filetest运算符。

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.

请注意,在Perl中,STDOUT可以绑定(本质上是一个过于复杂的重载对象),因此即使STDOUT没有直接连接到TTY,它仍然可以到达TTY。

#2


7  

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

如果您需要STDOUT实际连接到终端,请使用IO :: interactive,而不仅仅是重定向到/ dev / null /或其他。

#1


15  

Use the -t filetest operator.

使用-t filetest运算符。

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.

请注意,在Perl中,STDOUT可以绑定(本质上是一个过于复杂的重载对象),因此即使STDOUT没有直接连接到TTY,它仍然可以到达TTY。

#2


7  

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

如果您需要STDOUT实际连接到终端,请使用IO :: interactive,而不仅仅是重定向到/ dev / null /或其他。