WWW::Mechanize::GZip 触发 __DIE__ 信号...为什么?
我花了一段时间才找到代码中突然出现的问题,但 WWW::Mechanize::GZip 似乎以某种方式触发了我的 $SIG{DIE} 处理程序。考虑这段代码:
use strict;
use WWW::Mechanize::GZip;
$SIG{__DIE__} = sub {
print "WTF??? WHY IS THIS BEING TRIGGERED?\n";
};
my $mech = WWW::Mechanize::GZip->new();
$mech->get( 'http://ammoseek.com/' );
print $mech->content(), "\n";
知道为什么会发生这种情况吗?我能做些什么来预防它?
谢谢,
-迈克
It's taken me a while to track down a sudden problem with my code, but it appears that WWW::Mechanize::GZip is somehow triggering my $SIG{DIE} handler. Consider this code:
use strict;
use WWW::Mechanize::GZip;
$SIG{__DIE__} = sub {
print "WTF??? WHY IS THIS BEING TRIGGERED?\n";
};
my $mech = WWW::Mechanize::GZip->new();
$mech->get( 'http://ammoseek.com/' );
print $mech->content(), "\n";
Any idea why this would be happening? And what can I do to prevent it?
Thanks,
-Mike
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以通过让
$SIG{__DIE__}
处理程序打印来了解死亡原因/原因的详细信息:错误消息(处理程序中的
$_[0]
)处理程序中的堆栈跟踪(例如通过调用
Carp::cluck
) p>作为替代方案,使用 onerror => \&func 参数传递给 WWW::Mechanize::GZip 构造函数来创建自定义错误处理程序(假设错误不是来自 Compress::Zlib)。来自 WWW::Mechanize POD
由于 WWW::Mechanize::GZip 是 WWW::Mechanize 的直接子类,因此您可以在其中的构造函数中使用相同的参数。
You can find out details of what/how died by having your
$SIG{__DIE__}
handler print:Error message (
$_[0]
in the handler)Stack trace (e.g. by calling
Carp::cluck
) from within the handlerAs an alternative, use
onerror => \&func
parameter to WWW::Mechanize::GZip constructor to create custom error handler (assuming the error is not from Compress::Zlib). From WWW::Mechanize PODSince WWW::Mechanize::GZip is a direct subclass of WWW::Mechanize, you can use the same parameters to constructor in it.
假设没有真正的异常,即代码在没有
$SIG{__DIE__}
的情况下正常运行,那么该方法调用中的某处可能正在使用eval BLOCK
code> 捕获错误并从错误中恢复。使用eval BLOCK
作为异常处理程序的几个问题之一是它会触发$SIG{__DIE__}
,即使它实际上不应该触发。为了避免这种情况,请检查
$SIG{__DIE__}
中的 $^S 是否为 false。有关详细信息,请参阅 perlvar。
顺便说一句,您可以通过打印 @_ 来发现触发此异常的详细信息。
更好的是,除非您确实需要全局芯片处理程序,否则请使用
eval BLOCK
。看看 Try::Tiny 以获得更好的异常处理程序。
Assuming there isn't a real exception, that is, that the code runs normally without the
$SIG{__DIE__}
, then something, somewhere inside that method call is probably usingeval BLOCK
to trap and recover from an error. One of several problems with usingeval BLOCK
as an exception handler is that it triggers$SIG{__DIE__}
even though it really shouldn't.To avoid this, check that $^S is false in your
$SIG{__DIE__}
.See perlvar for more details.
BTW You can discover the details of the exception which is triggering this by printing @_.
Better yet, unless you really need a global die handler, use
eval BLOCK
.And have a look at Try::Tiny for a better exception handler.