我正在使用带有TCP服务器的select。我想将STDIN添加到选择文件句柄集。
#!/usr/bin/perl
use IO::Select;
use IO::Socket::INET;
my $sock = IO::Socket::INET->new(LocalPort => $serv_listen_port, Proto => 'tcp', List en=> 1);
my $s = IO::Select->new();
$s->add(\*STDIN); #want to be responsive to user input (allow me to type commands for example)
$s->add($sock);
@readytoread=$s->can_read(1); #timeout = 1sec
foreach $readable (@readytoread) {
if ($readable==$sock) {
#This was a listen request, I accept and add new client here
}
if ($readable == STDIN){ #what to do on this line?
#This is user typing input into server on terminal
}
}
在这里的代码中需要第4行到最后一行的帮助。
答案 0 :(得分:5)
$readable->fileno == fileno STDIN
或者,如果您对此感到满意,fileno STDIN
为零,您可以直接查看。
答案 1 :(得分:2)
can_read
会返回传递给add
的确切值,因此您只需使用
$readable == \*STDIN