What's the difference between Carp/Croak, Cluck/Confess, and verbose options?
Asked Answered
A

2

61

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.

Avocation answered 1/10, 2011 at 2:52 Comment(5)
Have you tried reading the documentation? It's better than trial and error.Stearic
I don't roll my own modules, but I do use Object Oriented programming which means I use packages. Even if all of your program is in a single file, but you define classes via package, you will find carp and croak handy. as pointed out by cmj.Clabo
@briandfoy Here's the documentation: The Carp routines are useful in your own modules because they act like die() or warn(), but with a message which is more likely to be useful to a user of your module. Not very useful unless you personally use packages and can see the difference.Clabo
@briandfoy I'm gonna say the Carp documents fail to indicate clearly whether cluck is fatal or not, at least in the first few paragraphs. Just one noobs experience.Memo
Asking what the functions do is a fair question, the documentation is very poor in this case. I've read it dozens of times and I still struggle to find the information I need. It appears not to have been improved for over 7 years.Courtroom
H
154

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 (like warn). It can report just the line where the function was called, or it can report a full backtrace.

         Fatal  Backtrace
carp       N        N
cluck      N        Y
croak      Y        N
confess    Y        Y

The verbose option forces backtraces on. That is, it makes carp act like cluck, and croak act like confess. You can use that when you realize that you need more debugging information, but don't want to change the code to use confess.

Haith answered 1/10, 2011 at 3:47 Comment(0)
V
26

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, and confess give you four combinations of options:

  • carp: not fatal, no backtrace
  • cluck: not fatal, with backtrace
  • croak: fatal, no backtrace
  • confess: fatal, with backtrace
Vessel answered 1/10, 2011 at 3:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.