当Perl发生不好的事情时,最好呱呱叫()还是死()? [英] Is it better to croak() or to die() when something bad happens in Perl?
问题描述
perlcritic 投诉,以下代码,一些样板DBI的东西工作得很好,应该 croak
而不是 die
perlcritic complaints that the following code, some boilerplate DBI stuff that works perfectly fine, should croak
instead of die
:
# Connect to database
my $db_handle = DBI->connect( $url, $user, $password ) or die $DBI::errstr;
所有这一切,在死亡似乎对我来说很好。
All this, while die seems to work fine for me.
我会想到一个武士Perl战士,呱呱呱呱。able able able when when when when。。。。。。笑话分开
I would think for a samurai Perl warrior, croak is less honorable than actually die when things go awry. Jokes apart
为什么我应该 croak
而不是 die
?
Why should I croak
instead of die
?
不注意的是什么后果 perlcritic的建议?
What are the consequences of not heeding perlcritic's advice?
推荐答案
从 http://www.perlmonks.org/?node_id=685452
当您或您的代码不正确时,您使用死机。当你的呼叫者没有做的时候,你会使用croak。死错误:$!表示错误发生在错误的行上。 croak错误:$!表示错误在调用者调用您的代码的行上。
You use die when the error is something you or your code didn't do right. You use croak when it's something your caller isn't doing right. die "error: $!" indicates the error is on the line where the error occured. croak "error: $!" indicates the error is on the line where the caller called your code.
在这种情况下,错误(DB的连接错误)没有任何关系的呼叫者和所有的事情做线连接,所以我会使用死
。
In this case, the error (connection error to DB) has nothing to do with the caller and everything to do with the line making the connection, so I would use die
.
这篇关于当Perl发生不好的事情时,最好呱呱叫()还是死()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!