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.

tshepang
  • 12,111
  • 21
  • 91
  • 136
SineSwiper
  • 2,046
  • 2
  • 18
  • 22
  • 5
    Have you tried reading the documentation? It's better than trial and error. – brian d foy Oct 01 '11 at 14:27
  • 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](http://stackoverflow.com/a/7618022/368630). – David W. Apr 08 '13 at 14:28
  • 3
    @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. – David W. Apr 08 '13 at 14:30
  • 2
    @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. – Leonard Nov 26 '13 at 20:12
  • 1
    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. – Will Sheppard Jun 28 '19 at 14:17

2 Answers2

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.

cjm
  • 61,471
  • 9
  • 126
  • 175
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
Francisco Zarabozo
  • 3,676
  • 2
  • 28
  • 54