http://www.perlmonks.org?node_id=472070


in reply to Detecting interactive/non-interactive shell

As per brother mifflin above, from the Perl Cookbook 1st ed, pp 518, 519...
  hth,
  ybiC

Use -t to test STDIN and STDOUT:

sub I_am_interactive { return -t STDIN && -t STDOUT; }

If you're on a POSIX system, test process groups:

sub I_am_interactive { local *TTY; # local file handle open(TTY, "/dev/tty") or die "can't open /dev/tty: $!"; my $tpgrp = tcgetpgrp(fileno(TTY)); my $pgrp = getpgrp(); close TTY; return ($tpgrp == $pgrp); }