Linux 在Perl中等待输入的定义时间段
怎么说 等待10秒,等待输入Linux 在Perl中等待输入的定义时间段,linux,perl,Linux,Perl,怎么说 等待10秒,等待输入 如果未识别输入 打印一些东西 在Perl中,您可以使用 使用严格; 使用警告; 子超时{ 我的($f,$sec)=@; 返回评估{ 本地$SIG{ALRM}=sub{die}; 警报(秒); $f->(); 报警(0); 1. }; } 我的$text; my$ok=超时(sub{$text=;},10); 如果($ok){ 打印“输入:$text”; } 否则{ 打印“发生超时\n”; } 和可以在超时的情况下读取 #!/usr/bin/env perl use
如果未识别输入
打印一些东西 在Perl中,您可以使用
使用严格;
使用警告;
子超时{
我的($f,$sec)=@;
返回评估{
本地$SIG{ALRM}=sub{die};
警报(秒);
$f->();
报警(0);
1.
};
}
我的$text;
my$ok=超时(sub{$text=;},10);
如果($ok){
打印“输入:$text”;
}
否则{
打印“发生超时\n”;
}
和可以在超时的情况下读取
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Select;
my $select = IO::Select->new();
$select->add( \*STDIN );
my $input = "NONE";
if ( $select->can_read(10) ) {
$input = <STDIN>;
}
print "Got input of $input\n";
#/usr/bin/env perl
严格使用;
使用警告;
使用IO::Select;
我的$select=IO::select->new();
$select->add(\*STDIN);
我的$input=“无”;
如果($select->can_read(10)){
$input=;
}
打印“已获得$input的输入\n”;
也许可以使用:Perl Cookbook描述了一个涉及和的解决方案。这可能会对您有所帮助。
#!/usr/bin/env perl
use strict;
use warnings;
use IO::Select;
my $select = IO::Select->new();
$select->add( \*STDIN );
my $input = "NONE";
if ( $select->can_read(10) ) {
$input = <STDIN>;
}
print "Got input of $input\n";