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


in reply to Re^2: Checking for STDIN
in thread Checking for STDIN

What do you mean?!

Save this code to a file and test it.

#!/usr/bin/perl -wl use strict; open my $fh, '>', '/tmp/test.log' or die $!; print $fh 'STDIN ', ( -t STDIN ? 'IS' : 'is NOT' ), ' available' or di +e $!; close $fh or die $!;

Replies are listed 'Best First'.
Re^4: Checking for STDIN
by dbmathis (Scribe) on Jul 30, 2008 at 02:09 UTC
    In a cron job -t would evaluate to false which in my case would mean my script would think info is being piped to it when I have a commandline option -logfile <file> being used to specify input.
      You seem to confuse the meaning of STDIN. In "filter" mode, STDIN won't be the terminal, e.g. echo 'foo' | ./test.pl
        I am sorry but the method you specified does not work for what i am trying to do in cron.

        Please refer to: http://www.unix.com.ua/orelly/perl/cookbook/ch15_03.htm

        The -t operator tells whether the filehandle or file is a tty device. Such devices are signs of interactive use. This only tells you whether your program has been redirected. Running your program from the shell and redirecting STDIN and STDOUT makes the -t version of I_am_interactive return false. Called from cron, I_am_interactive also returns false.

        The POSIX test tells you whether your program has exclusive control over its tty. A program whose input and output has been redirected still can control its tty if it wants to, so the POSIX version of I_am_interactive returns true. A program run from cron has no tty, so I_am_interactive returns false.