Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: Should I use carp/croak or warn/die

by haukex (Archbishop)
on May 30, 2018 at 15:08 UTC ( [id://1215468]=note: print w/replies, xml ) Need Help??


in reply to Should I use carp/croak or warn/die

Note that Carp doesn't automatically include a stack trace, that's only if you do perl -MCarp=verbose script.pl or $Carp::Verbose=1;, or you explicitly use confess. I like to use Carp in my modules because it reports the error as coming from the user's code, rather than the module itself, which makes more sense for many error cases (like incorrect parameters). When it's an internal error that I don't expect the user to see (at least not often), I'll sometimes use confess to get the full stack trace.

$ cat Foo.pm package Foo; use warnings; use strict; use Carp; sub one { die "Bad call to one"; } sub two { carp "Bad call to two"; } sub three { confess "Some error in three"; } 1; $ perl -I. -MFoo -le 'Foo::one' Bad call to one at Foo.pm line 5. $ perl -I. -MFoo -le 'Foo::two' Bad call to two at -e line 1. $ perl -I. -MFoo -le 'Foo::three' Some error in three at Foo.pm line 7. Foo::three() called at -e line 1

Replies are listed 'Best First'.
Re^2: Should I use carp/croak or warn/die
by SBECK (Chaplain) on May 30, 2018 at 15:27 UTC
    Thank you. I think you've just convinced me to use Carp, at least in some instances.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (3)
As of 2024-04-19 01:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found