免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 3510 | 回复: 8
打印 上一主题 下一主题

怀疑 perl 的 alarm 在xp上有兼容性问题 (version: 5.8.8) [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-09-18 15:56 |只看该作者 |倒序浏览
perl 版本是 5.8.8
在xp home 版本上,alarm() 不会超时发送 ALRM 信号,主要代码逻辑如下, xp home 上无法正常运行, linux 工作正常

$SIG{ALRM} = \&timeout;
eval{
    alarm(10);
    ...
    alarm(0);
};
if( $@ =~ /xxx/ ) {
   print xxx;
}
sub timeout {
   print yyy;
}

网上查了下,发现 有报 win2003 上 alarm 有问题, 但没看到 xp home 的问题, 不知道大家有没有遇到过?

论坛徽章:
0
2 [报告]
发表于 2009-09-18 15:58 |只看该作者
嗯 windows下就不要用alarm了

论坛徽章:
0
3 [报告]
发表于 2009-09-18 16:19 |只看该作者
ok, 谢谢~
windows 很多郁闷的地方啊...
工作环境又必须是windows...
期待 perl 6 的表现~

论坛徽章:
0
4 [报告]
发表于 2009-09-18 22:14 |只看该作者
windows上的alarm会被io read给block掉的
这问题我已经碰过过
后来是用其他方法模拟alarm的
如果需要代码我可以提供给你

论坛徽章:
0
5 [报告]
发表于 2009-09-19 01:29 |只看该作者
原帖由 churchmice 于 2009-9-18 22:14 发表
windows上的alarm会被io read给block掉的
这问题我已经碰过过
后来是用其他方法模拟alarm的
如果需要代码我可以提供给你


什么方法?我想的是用线程模拟一下。

或者Windows下有什么能用的东西?

论坛徽章:
0
6 [报告]
发表于 2009-09-19 09:27 |只看该作者
原帖由 churchmice 于 2009-9-18 22:14 发表
windows上的alarm会被io read给block掉的
这问题我已经碰过过
后来是用其他方法模拟alarm的
如果需要代码我可以提供给你


当然需要了!因为我是需要解决等待超时输入的问题,后面使用 readkey( timeout ) 绕过的,但是要能模拟 alarm ,那就更好了!可以发代码的话,麻烦发到:dugu072#gmail.com

论坛徽章:
0
7 [报告]
发表于 2009-09-19 13:12 |只看该作者

  1. #Timeout read
  2. #This is a timeout read subroutine which implement windows and UNIX system differently based on $^O
  3. ##Please remember to add "use Config" at the top of your program
  4. ##my $res = time_out("timeout")
  5. ##The timeout is specify with "timeout" value, default to 3s, you can change it at your will
  6. ##If user input something within the specified time, the input is stored in $res
  7. ##else timedout, and a default value "No input" is returned, you can change the default value in the code
  8. sub timeout_read {
  9.     my $timeout = 5;    #Default to 5 s
  10.     $timeout = shift if @_;
  11.     my $tmpfile = "tmp";       #The name of the temporary created file
  12.     my $result  = "No input"
  13.       ;    #The default return value, you can change it to anything you want

  14.     #Depending on the operating system, treat the timeout problem accordingly
  15.     if ( $^O =~ /MSWin32/i ) {

  16.         #This is windows OS, so we could use Win32::Job to implement it
  17.         eval "use Win32::Job";
  18.         my $job = Win32::Job->new;

  19. #Create a job to read the user input and redirect the stdout to a file called tmp
  20.         $job->spawn(
  21.             $Config{perlpath},
  22.             q{perl -e "print scalar <STDIN>"},
  23.             { stdout => "tmp" },
  24.         );
  25.         my $status = $job->run($timeout);
  26.         if ($status) {    #User has input something
  27.             chomp( $result = qx /type $tmpfile/ );
  28.             unlink $tmpfile or warn "Fail to remove $tmpfile";
  29.         }
  30.     }
  31.     else {

  32.   #This is a UNIX like system, so could implement the timeout read in alarm call
  33.         my $tmp;
  34.         eval {
  35.             local $SIG{ALRM} = sub { die "alarm\n" };
  36.             alarm $timeout;
  37.             chomp( $tmp = <STDIN> );
  38.         };
  39.         if ($@) {
  40.             die $@ if $@ ne "alarm\n";
  41.         }
  42.         else {
  43.             $result = $tmp;
  44.         }
  45.     }

  46.     #Finally, reset the alarm
  47.     alarm 0;
  48.     return $result;
  49. }
复制代码


这是我以前写的一个超时阅读函数,在Linux和windows上都可以用
通过my $res = time_out("$timeout");调用
默认的超时时间是5s,如果没有输入,返回"No input",反之则返回用户输入的数据,你可以自己根据实际的应用情况照着修改,反正源代码都贴了

论坛徽章:
0
8 [报告]
发表于 2009-09-19 14:07 |只看该作者
OK, 谢谢,原来是 Win32::Job,之前不知道这个模块的,不错的东西,学习了!

顺便一提,如果是输入超时的话,个人觉得Term::ReadKey会更方便,win/linux都是一样用,还没有临时文件~
use Term::ReadKey;
ReadMode(4);
my $key = ReadKey(10);
ReadMode(0);

论坛徽章:
0
9 [报告]
发表于 2009-09-20 10:57 |只看该作者

回复 #8 dugu072 的帖子

非常感谢,亏我还码了这么多代码
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP