gpt4 book ai didi

perl - 在 perl 中尝试::Tiny 和 $SIG{__DIE__}?

转载 作者:行者123 更新时间:2023-12-03 16:54:37 25 4
gpt4 key购买 nike

是否可以在具有“重载”的 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com