How can I tell if STDIN is connected to a terminal in Perl?
Asked
Active
Viewed 3,923 times
3 Answers
15
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.

Evan Carroll
- 78,363
- 46
- 261
- 468

Glen Solsberry
- 11,960
- 15
- 69
- 94
12
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
- 129,424
- 31
- 207
- 592
0
One solution would be to use tty:
[root@server] ~> tty
/dev/pts/0
[root@server] ~> echo y | tty
not a tty
But it is not very pretty...

Peter Mortensen
- 30,738
- 21
- 105
- 131

tomdee
- 2,319
- 5
- 25
- 39