3
我正在使用select与TCP服务器。我想将STDIN添加到选择的文件句柄集中。perl - 检查文件句柄是否为std
#!/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到最后一行。
有人能解释的使用/背后的故事'\ * STDIN'?整个字符串是一种常量?为什么是反斜杠?它逃避了星号吗? – 2011-03-07 17:50:07
@Steven Lu:STDIN是一个全局文件句柄。因为它没有sigil,所以不能直接在任何地方使用它,否则Perl会将其解释为字符串或函数调用。星号是typeglob sigil(typeglobs可以被认为包含文件句柄以及其他变量类型),并且反斜杠会引用该类型块。 'IO :: Select'知道它需要一个文件句柄,因此它可以在需要时解引用它。查看'perldata'的“Typeglobs和filehandles”部分以获得更全面的视图。 – 2011-03-07 20:05:34