在 perl 中因错误而死亡但没有堆栈跟踪的正确方法是什么?

发布于 2024-09-16 13:19:42 字数 602 浏览 9 评论 0原文

我正在编写一个 perl 脚本,在检查用户在命令行上提供的选项的部分中,我想退出并显示一个错误,解释选项有什么问题。在这种情况下,不需要堆栈跟踪与此错误消息一起出现。那么,出现错误消息但没有堆栈跟踪或调试信息的最佳方式是什么?

我尝试了以下方法:

die "Invalid options";

产生了

Invalid options at myscript.pl line 49.

然后,我尝试了

use Carp;
...
croak "Invalid options";

产生了

Invalid options at myscript.pl line 47
    main::prepare_output_directory() called at myscript.pl line 546

那么我怎么能只带着 Invalid options 而死呢?

额外问题:为什么 croak 告诉我错误在第 47 行,而对 croak 的实际调用在第 49 行?

I am writing a perl script, and in the part where I am checking the options that the user supplied on the command line, I want to exit with an error explaining what was wrong with the options. In this case, there is no need for a stack trace to go along with this error message. So what is the best way to die with an error message but no stack trace or debug information?

I've tried the following:

die "Invalid options";

which produces

Invalid options at myscript.pl line 49.

Then, I tried

use Carp;
...
croak "Invalid options";

which produces

Invalid options at myscript.pl line 47
    main::prepare_output_directory() called at myscript.pl line 546

So how can I just die with Invalid options and nothing else?

Bonus question: why does croak tell me that the error is at line 47, when the actual call to croak is on line 49?

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

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

发布评论

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

评论(1

神爱温柔 2024-09-23 13:19:42

只需在 die 字符串末尾添加一个换行符即可:

die "Invalid options\n";

这样可以防止将错误位置附加到您的消息中。

Just put a newline at the end of the die string:

die "Invalid options\n";

That will prevent the location of the error from being appended to your message.

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