How can I tell if STDIN is connected to a terminal in Perl?
+8
A:
if (-t STDIN) {
# stdin is connected
} else {
# stdin is not connected
}
I usually use this in conjunction with -t STDOUT, to find out if I'm running from an interactive shell, or from cron, to enable more output.
gms8994
2009-02-09 16:14:42
A:
One solution would be to use tty:
[root@server] ~> tty
/dev/pts/0
[root@server] ~> echo y | tty
not a tty
But not very pretty...
tomdee
2009-02-09 16:16:02
This isn't portable either.
Mr. Muskrat
2009-02-09 16:26:04
+6
A:
You might also be interested in IO::Interactive to figure out if Perl thinks it is interacting with a user. Simply being connected to a tty doesn't mean the user is going to see what you do.
brian d foy
2009-02-09 17:21:34