Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses


by Aristotle (Chancellor)
on Jun 08, 2006 at 12:26 UTC ( #554264=modulereview: print w/replies, xml ) Need Help??

Item Description: The way it should always have worked

Review Synopsis:

From the docs:

In case you just want to ward off all error messages from the module in which you “use Carp::Clan”, i.e., if you want to make all error messages or warnings to appear to originate from where your module was called (this is what you usually used to “use Carp;” for ;-)), instead of in your module itself (which is what you can do with a “die” or “warn” anyway), you do not need to provide a pattern, the module will automatically provide the correct one for you.

Before I discovered this module, I would play silly games with local $Carp::CarpLevel = $Carp::CarpLevel + 1 sprinkled all over the place. Not only was that annoying, it also hatefully causes Carp to emit verbose messages. Now I just use Carp::Clan and things work as I meant them to.

Of course, that’s not the module’s only use – but that alone makes the module worthwhile to use everywhere, even if you don’t have a “clan” of modules.

My only question is: why is this not part of the core? Why indeed doesn’t Carp itself work that way?

Replies are listed 'Best First'.
Re: Carp::Clan
by salva (Canon) on Jun 08, 2006 at 13:58 UTC
    Carp provides the @CARP_NOT feature for that.

      The documentation of that feature is too curt and the code is withstanding my attempts to understand it in a hurry. How does one employ that?

      Makeshifts last the longest.

        you just push there the names of the packages you want to skip when calling carp, croak, etc.

        For instance:

        #!/usr/bin/perl package Foo; sub foo { Bar::bar() } package Bar; use Carp; our @CARP_NOT = qw(Foo); sub bar { croak "hello" } package main; Foo::foo();
        hello at /tmp/ line 12
Re: Carp::Clan
by ambrus (Abbot) on Jun 08, 2006 at 18:58 UTC

    It's because it's not always the caller that's wrong. Sometimes it's better to see where the error appears in the module.

    Btw, ruby and I think python too typically gives a full stack trace on errors. That's the best thing to do I belive, because the interperter can't decide which function really "caused" the error.

      If a parameter fails to validate, then it’s the caller who is wrong. That doesn’t change, even if the parameter is only validated several internal subroutine calls deeper than where the module picked it up. If the validation code itself does happen to be at fault, you can still get a stack trace on demand by asking Carp to be verbose.

      Makeshifts last the longest.

Re: Carp::Clan
by diotalevi (Canon) on Jun 08, 2006 at 17:19 UTC

    I don't get it. Can you demonstrate the problem that this solves?

    ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

      my %handler; BEGIN { %handler = ( foo => sub { ref $_[0] eq 'ARRAY' or croak "foo key takes a hashre +f"; ... }, bar => sub { $_[0] =~ /wibble$/ or croak "bar key must end with 'w +ibble'"; ... }, ) } sub process_something { my %arg = @_; my @results; while( my ( $elem, $param ) = each %arg ) { push @results, $handler->{ $elem }->( $param ); } }

      croak will say the error was in process_something, when you really want it to be thrown for the where the call to process_something was.

      Makeshifts last the longest.

        croak will say the error was in process_something's caller if process_something was called from another package. For example,
        use strict; use warnings; package Module; use Carp qw( croak ); my %handler = ( foo => sub { ref $_[0] eq 'ARRAY' or croak "foo key takes a hashref +"; 1 }, bar => sub { $_[0] =~ /wibble$/ or croak "bar key must end with 'wi +bble'"; 1 }, ); sub process_something { my %arg = @_; my @results; while( my ( $elem, $param ) = each %arg ) { push @results, $handler{ $elem }( $param ); } } package main; Module::process_something( foo => 'a' ); # <-- line 27.


        foo key takes a hashref at line 27
Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: modulereview [id://554264]
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2023-03-22 20:14 GMT
Find Nodes?
    Voting Booth?
    Which type of climate do you prefer to live in?

    Results (60 votes). Check out past polls.