Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何测试STDIN是否有可读取的内容(Windows上的Perl)_Windows_Perl_Asynchronous_Filehandler - Fatal编程技术网

如何测试STDIN是否有可读取的内容(Windows上的Perl)

如何测试STDIN是否有可读取的内容(Windows上的Perl),windows,perl,asynchronous,filehandler,Windows,Perl,Asynchronous,Filehandler,我在Perl中遇到了一个问题。为了测试STDIN文件处理程序是否有要立即读取的内容,我想这样编程 while(1) { my ($l); if (TestCanRead(STDIN)) { $l = <STDIN>; HandleRead($l); } else { HandleNotRead(); } } 谁能告诉我如何编写函数ReadImmediate或T

我在Perl中遇到了一个问题。为了测试
STDIN
文件处理程序是否有要立即读取的内容,我想这样编程

while(1)
{
     my ($l);
     if (TestCanRead(STDIN))
     {
         $l = <STDIN>;
         HandleRead($l);
     }
     else
     {
         HandleNotRead();
     }
}
谁能告诉我如何编写函数ReadImmediateTestCanRead
在Windows系统上?谢谢。

不幸的是,我没有Windows环境可供测试,但Perl声称具有可移植性。因此,让我们假设Unix解决方案是有效的

您需要
选择
或在其周围添加包装。我通常使用
IO::Select
,如下所示:

use IO::File;
use IO::Select;

my $select = IO::Select->new( \*STDIN );

while (1) {
    if (my @ready_FHs = $select->can_read(0)) {
        foreach my $FH (@ready_FHs) {
            say $FH->getline();
        }
    } else {
        say "Nothing to do; napping";
        sleep 1;
    }
}
use IO::File;
use IO::Select;

my $select = IO::Select->new( \*STDIN );

while (1) {
    if (my @ready_FHs = $select->can_read(0)) {
        foreach my $FH (@ready_FHs) {
            say $FH->getline();
        }
    } else {
        say "Nothing to do; napping";
        sleep 1;
    }
}