How can I check (peek) STDIN for piped data in Perl without using select?

后端 未结 2 833
猫巷女王i
猫巷女王i 2021-02-14 14:42

I\'m trying to handle the possibility that no arguments and no piped data is passed to a Perl script. I\'m assuming that if there are no arguments then input is being piped via

2条回答
  •  太阳男子
    2021-02-14 15:27

    use POSIX 'isatty';
    if ( ! @ARGV && isatty(*STDIN) ) {
        die "usage: ...";
    }
    

    See: http://www.opengroup.org/onlinepubs/009695399/functions/isatty.html

    Note that select wouldn't be much help anyway, since it would produce false results if the piped info wasn't ready yet. Example:

    seq 100000|grep 99999|perl -we'$rin="";vec($rin,fileno(STDIN),1)=1;print 0+select($rin,"","",.01)'
    

提交回复
热议问题