perl - 对于我正在 ping 的某些域,超时在 Perl 脚本中不起作用
问题描述
我无法弄清楚这里发生了什么。我的示例代码:
#!/usr/bin/perl
use strict;
use Net::Ping;
use Time::Out qw(timeout) ;
my $p = Net::Ping->new();
my $domain = lc("play9112.com");
print qq|Doing $domain \n|;
my $ping_result = timeout 3 => sub {
return $p->ping($domain,5);
} ;
unless ($ping_result) {
print qq|Don't seem to be able to grab $domain! \n|;
} else {
print qq|needs to delete...\n|; # delete is done below!
}
上面的代码适用于大约 99% 的情况。但是有一些域它只是锁定,并且不会“超时”!作为测试,而不是Time::Out
,我只用一个普通的eval {}
块来测试它:
my $TIMEOUT_IN_SECONDS = 5;
eval {
local $SIG{ALRM} = sub { die "alarm\n" };
alarm($TIMEOUT_IN_SECONDS);
$p->ping($domain,5);
# do stuff that might timeout.
alarm(0);
};
if ($@) {
# handle timeout condition.
}
这似乎有同样的问题。谁能指出我正确的方向?一定有一个怪癖,或者我错过了什么!
更新:我刚刚用 -d 尝试过并得到:
perl -d test-ping.cgi
Loading DB routines from perl5db.pl version 1.51
Editor support available.
Enter h or 'h h' for help, or 'man perldebug' for more help.
main::(test-ping.cgi:8): my $p = Net::Ping->new();
DB<1>
DB<1> n
main::(test-ping.cgi:11): my $domain = lc("play9112.com");
DB<1> n
main::(test-ping.cgi:13): print qq|Doing $domain \n|;
DB<1> n
Doing play9112.com
main::(test-ping.cgi:27): my $TIMEOUT_IN_SECONDS = 5;
DB<1>
main::(test-ping.cgi:28): eval {
DB<1>
main::(test-ping.cgi:29): local $SIG{ALRM} = sub { die "alarm\n" };
DB<1>
main::(test-ping.cgi:30): alarm($TIMEOUT_IN_SECONDS);
DB<1>
main::(test-ping.cgi:32): $p->ping($domain,5);
DB<1>
它在最后一部分之后挂起。所以它在 Net::Ping 中的东西就是这样做的:/
解决方案
推荐阅读
- c# - 触摸和画布元素位置不匹配
- python - 如何序列化具有多个深度为 1 的自引用字段的 django 模型
- f# - 平均 f# 中的 c# 对象列表,我的语法有什么问题
- javascript - 这个臭名昭著的网站会阻止 Chrome 开发工具吗?
- quicksort - 快速排序 - 空间复杂度 - 为什么它是 O(logN) 而不是 O(N)?
- mysql - 带有二级子排序的 OrderBy
- javascript - 从命令行暂停 Nodejs 服务器的执行
- ansible - 错误!在 Ansible 控制器上找不到 vars 文件 vars
- hive - 合并两列但在配置单元中具有不同的结构
- c# - C# - 生成这些真正的键值对的语法