gpt4 book ai didi

perl - 为什么在省略 Try::Tiny 时 Perl 不给出 try/catch 错误

转载 作者:行者123 更新时间:2023-12-04 22:55:10 25 4
gpt4 key购买 nike

我正在移动一些代码,并在一段代码上省略了“use Try::Tiny”。当我运行它时,perl 会运行这两个代码块,所以 catch 会触发,幸运的是让我看到了错误。看来 try 和 catch 被用作标签。我认为标签后面需要一个冒号?为什么 perl 没有捕获这个?

这是代码:

#!/grid/common/bin/perl

use strict;
use warnings 'all';

foo();

sub foo {

try {
print("hi\n");
}
catch {
die "FATAL: this went wrong, <$@>";
}

}

最佳答案

这是indirect object syntax咬你。

try {
print("hi\n");
}
catch {
die "FATAL: this went wrong, <$@>";
}

被解释为:
(do { print("hi\n") })->try(
(do { die("FATAL: this went wrong, <$@>") })->catch
)

是的,真的。

有一个模块叫 indirect当检测到间接方法调用时,它会给你一个编译时警告。

关于perl - 为什么在省略 Try::Tiny 时 Perl 不给出 try/catch 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25879255/

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