Carp/Croak、Cluck/Confess 和详细选项之间有什么区别?
我没有经常使用 Carp,因为我通常都是自己开发的。然而,本着与核心模块保持一致的精神,我现在正在使用它。然而,它似乎只比 warn/die 好不了多少。
此外,cluck/confess/verbose 还能做什么?我运行了这个简短的脚本来了解输出的样子(因为 Carp 文档不这样做)。在任何运行中它看起来都完全相同(除了随机字符串之外)。
#!/usr/bin/perl
package Warning;
sub warning {
warn "warn";
}
package CWarn;
use Carp qw(carp cluck);
sub cwarn {
int(rand(2)) ? carp "carp" : cluck "cluck";
}
package Fatal;
use Carp qw(confess croak);
sub fatal {
int(rand(2)) ? confess "confess" : croak "croak";
}
package Loop;
use v5.10;
sub loop {
say '=' x 80;
Warning::warning();
CWarn::cwarn();
loop() unless ($c++ > 10);
Fatal::fatal();
}
package main;
Warning::warning();
CWarn::cwarn();
Loop::loop();
更新:用包名称更新了脚本,它确实有所不同。然而,Carp 在日志信息方面似乎仍然非常基础,并且不支持 Web 输出。我想我会看看其他的,比如 CGI::Carp、Log::Output 和 Log::Log4Perl。
I haven't used Carp all that much because I've generally rolled my own. However, in the spirit of keeping with Core modules, I'm using it now. However, it seems like it's barely better than warn/die.
Furthermore, what does cluck/confess/verbose even do? I've ran this short script to get an idea of the output looks like (because the Carp docs don't do it). It looks exactly the same on any run (besides the random strings).
#!/usr/bin/perl
package Warning;
sub warning {
warn "warn";
}
package CWarn;
use Carp qw(carp cluck);
sub cwarn {
int(rand(2)) ? carp "carp" : cluck "cluck";
}
package Fatal;
use Carp qw(confess croak);
sub fatal {
int(rand(2)) ? confess "confess" : croak "croak";
}
package Loop;
use v5.10;
sub loop {
say '=' x 80;
Warning::warning();
CWarn::cwarn();
loop() unless ($c++ > 10);
Fatal::fatal();
}
package main;
Warning::warning();
CWarn::cwarn();
Loop::loop();
UPDATE: Updated the script with package names and it does make a difference. However, Carp still seems to be very basic in terms of logging information, and it doesn't support web output. I guess I'll look at other ones like CGI::Carp, Log::Output, and Log::Log4Perl.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的示例的问题在于您的所有子程序都在同一个包中(默认包:
main
)。这不是 Carp 设计的用例。Carp 旨在用于模块中。原因是,当模块遇到问题时,通常是因为模块的调用者向其传递了错误的数据。因此,报告模块被调用的行(从模块外部的代码)通常更有用,而不是报告模块发现问题的行。这就是 Carp 导出的函数的作用。
有 2 组是/否选项。该函数可以是致命的(例如
die
)或非致命的(就像警告
)。它可以仅报告调用函数的行,也可以报告完整的回溯。详细选项强制回溯。也就是说,它使
carp
表现得像cluck
,而croak
表现得像confess
。当您意识到需要更多调试信息,但又不想更改代码以使用confess
时,可以使用它。The problem with your example is that all your subs are in the same package (the default package:
main
). That's not the use case that Carp was designed for.Carp is intended to be used in modules. The reason is that when a module encounters a problem, it's often because the module's caller passed it bad data. Therefore, instead of reporting the line where the module discovered the problem, it's usually more useful to report the line where the module was called (from code outside the module). That's what the functions exported by Carp do.
There are 2 sets of yes/no options. The function can be fatal (like
die
) or nonfatal (likewarn
). It can report just the line where the function was called, or it can report a full backtrace.The verbose option forces backtraces on. That is, it makes
carp
act likecluck
, andcroak
act likeconfess
. You can use that when you realize that you need more debugging information, but don't want to change the code to useconfess
.Carp 比 警告/死亡它将显示调用函数抛出错误的文件和行,而不仅仅是抛出错误的位置。这对于图书馆来说通常很有用。 (例如,数据库库可能应该抛出错误,指示错误的数据库调用在哪里,而不是指示其自身内的一行。)
carp
、cluck
、croak
和confess
为您提供四种选项组合:carp
:非致命,无回溯cluck
:非致命,有回溯confess
:致命的,有回溯Carp is better than warn/die in that it will display the file and line of what called the function throwing an error, rather than simply where the error was thrown. This can often be useful for libraries. (For instance, a database library should probably throw errors indicating where the erroneous database call is, rather than indicating a line within itself.)
carp
,cluck
,croak
, andconfess
give you four combinations of options:carp
: not fatal, no backtracecluck
: not fatal, with backtracecroak
: fatal, no backtraceconfess
: fatal, with backtrace