What is the difference between Carp / Croak, Cluck / Confess and verbose options?

I did not use Carp so much because I generally rolled on my own. However, in the spirit of complying with the Core modules, I am using it now. However, it seems that this is hardly better than warning / dying.

Also, what does cluck / confess / verbose even do? I ran this short script to figure out what the output looks like (because Carp docs don't do this). It looks exactly the same on any run (besides 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 script with package names, and that makes a difference. However, Carp still seems very simple in terms of registration information, and it does not support web-based logout. I think I'll look at others like CGI :: Carp, Log :: Output and Log :: Log4Perl.

+49
perl error-handling carp
01 Oct 2018-11-11T00:
source share
2 answers

The problem with your example is that all of your sub-sites are in the same package (default package: main ). This is not the case with Carp .

Carp is intended for use in modules. The reason is that when a module encounters a problem, this often happens because the calling module passed bad data to it. Therefore, instead of reporting the line in which the module detected a problem, it is usually more useful to report the line in which the module was called (from code outside the module). This is what the functions exported by Carp do.

There are two sets of yes / no options. A function can be fatal (e.g. die ) or non-fatal (e.g. warn ). It can only report the line on which the function was called, or it can report the full backtrace.

  Fatal Backtrace carp NN cluck NY croak YN confess YY 

The verbose option forces backtraces. That is, carp acts like cluck , and croak acts like confess . You can use this when you realize that you need additional information for debugging, but do not want to change the code to use confess .

+118
01 Oct 2018-11-11T00:
source share

Carp better than warn / die , because it displays the file and line of what is called the error throwing function, and not just where the error was caused. This can often be useful for libraries. (For example, a database library should probably throw errors indicating where the database call was in error, and not indicate a string within itself.)

Carp , cluck , croak and confess provide four combinations of parameters:

  • Carp : no fatal, no backtrace
  • cluck : not deadly, with backtrace
  • croak : fatal, without backtrace
  • confess : fatal, with backtrace
+21
Oct 01 '11 at 3:18
source share



All Articles