是否有任何 Perl 模块可以阻止我向任何可能失败的内容添加“or die”?

发布于 2024-08-05 02:13:38 字数 537 浏览 3 评论 0原文

我正在编写运行各种外部命令以及可能失败的各种文件系统命令的代码。

有没有任何模块可以让我免去向可能失败的任何内容添加 or die 的麻烦?

我希望能够将以下内容包装在某些内容中,而不是:

mkdir $dirname or die "can't create directory $dirname";

system("some external command") or die "can run some external command";

我得到:

some_wrapper_code {
    mkdir $dirname;

    system("some external command");
}

这样,如果 mkdir 失败,它会告诉我失败发生在 mkdir 中>,如果 system 失败,它会告诉我故障发生在 system 中。

I'm writing code that runs all sorts of external commands as well as various filesystem commands that can fail.

Is there any module that can save me the trouble of adding or die to anything that can fail?

I'd like to be able to wrap the following in something so that instead of:

mkdir $dirname or die "can't create directory $dirname";

system("some external command") or die "can run some external command";

I get:

some_wrapper_code {
    mkdir $dirname;

    system("some external command");
}

such that if mkdir fails it'll tell me that the failure was in mkdir, and if system fails it'll tell me that the failure was in system.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

脱离于你 2024-08-12 02:13:38

是的。查看 CPAN 中的 autodie

来自文档:

autodie 编译指示提供了一种便捷的方法来替换通常返回的函数
失败时为 false ,而失败时则抛出异常。

autodie 编译指示具有词法范围,这意味着函数和子例程会随着
autodie 只会改变它们的行为,直到封闭块、文件或
评估

Yep. Check out autodie from CPAN.

From the docs:

The autodie pragma provides a convenient way to replace functions that normally return
false on failure with equivalents that throw an exception on failure.

The autodie pragma has lexical scope, meaning that functions and subroutines altered with
autodie will only change their behaviour until the end of the enclosing block, file, or
eval.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文