作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
是否可以在具有“重载”的 Perl 程序中使用 Try::Tiny $SIG{__DIE__}
?
例如,这个程序的期望输出是“caught it”:
#!/usr/bin/perl
use strict;
use warnings;
use diagnostics;
use Try::Tiny;
# here how to deal with try and catch
$SIG{__DIE__} =
sub {
print "died my way $_[0]"
};
my $rv;
try {die "its only a flesh wound"; }
catch {
$rv = undef;
print "caught it: $_ ";
};
感谢您的关注和任何建议!
最佳答案
$SIG{__DIE__}
的文档显示 $^S
被使用的原因:你几乎总是想使用 $^S
在 __DIE__
处理程序以避免您所询问的问题。
例如,
#!/usr/bin/perl
use strict;
use warnings;
use Try::Tiny;
$SIG{__DIE__} = sub {
return if $^S; # If in eval.
print(STDERR "Died: $_[0]"); # Send msg to STDERR, not STDOUT.
exit( $! || ( $? >> 8 ) || 255 ); # Emulate die().
};
try {
die("It's only a flesh wound.");
} catch {
print("Caught: $_");
};
die("More than a flesh wound");
print("done.\n");
产出
Caught: It's only a flesh wound. at a.pl line 14.
Died: More than a flesh wound at a.pl line 19.
关于perl - 在 perl 中尝试::Tiny 和 $SIG{__DIE__}?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66665161/
我有一段 Perl 代码,有点像下面这样(强烈简化): 有一些嵌套的子程序调用(实际上是方法),并且一些内部的执行它们自己的异常处理: sub outer { middle() } sub middl
我花了一段时间才找到我的代码突然出现的问题,但 WWW::Mechanize::GZip 似乎以某种方式触发了我的 $SIG{ 模具 } 处理程序。考虑这个代码: use strict; use WW
根据perldoc -f die ,其中记录了 $SIG{__DIE__} Although this feature was to be run only right before your pro
是否可以在具有“重载”的 Perl 程序中使用 Try::Tiny $SIG{__DIE__} ? 例如,这个程序的期望输出是“caught it”: #!/usr/bin/perl use stri
我是一名优秀的程序员,十分优秀!