Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I tell if STDIN is connected to a terminal in Perl?

How can I tell if STDIN is connected to a terminal in Perl?

like image 422
tomdee Avatar asked Feb 09 '09 16:02

tomdee


2 Answers

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.

like image 50
Glen Solsberry Avatar answered Oct 20 '22 11:10

Glen Solsberry


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.

like image 28
brian d foy Avatar answered Oct 20 '22 12:10

brian d foy