在 perl 中因错误而死亡但没有堆栈跟踪的正确方法是什么?
我正在编写一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需在
die
字符串末尾添加一个换行符即可:这样可以防止将错误位置附加到您的消息中。
Just put a newline at the end of the
die
string:That will prevent the location of the error from being appended to your message.