为什么Perl在遗漏Try :: Tiny时没有为try/catch提供错误

kdu*_*ubs 3 perl

我正在移动一些代码并在一段代码上省略了'use Try :: Tiny'.当我运行它时,perl会运行两个代码块,因此捕获会触发,幸运的是让我看到错误.似乎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, <$@>";
    }

}
Run Code Online (Sandbox Code Playgroud)

tob*_*ink 13

这是间接对象​​语法咬你.

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

获取解释为:

(do { print("hi\n") })->try(
   (do { die("FATAL: this went wrong, <$@>") })->catch
)
Run Code Online (Sandbox Code Playgroud)

是的,真的.

有一个名为indirect的模块,可以在检测到间接方法调用时为您提供编译时警告.