How can I mark a call as “unsafe” with Karp?

I have the same problem as in Unable to disable stack trace in Carp :: croak () for some reason . Since every call on the stack is considered “safe,” croak() prints a full stack trace each time. I would like to disable this for certain calls.

Here is an example:

 use Carp; sub this_may_fail { # Some code... croak "This call failed!"; } sub regular_code { this_may_fail(); } regular_code(); 

Both routines are in the same package, so this_may_fail automatically marked as safe. Is there any way to tell Karp that this_may_fail should be considered unsafe?

+6
source share
2 answers

This is regular_code , which is considered "safe" on this_may_fail . The check is based on the namespace, so to make it unsafe, you put this_may_fail in a different namespace.


Or write your own croaker.

 perl -e' use Carp qw( ); sub untrusting_croak { goto &Carp::croak if $Carp::Verbose; my @caller = caller(1); die(join("", @_)." at $caller[1] line $caller[2]\n"); } sub f { untrusting_croak("!!!"); } # Line 9 f(); # Line 11 ' !!! at -e line 11 
+3
source

Not particularly pretty, but instead:

 sub regular_code { ...; my $result = this_may_fail(@args); } 

You can use this ...

 sub regular_code { ...; my $result = do { my $sub = \&this_may_fail; package DUMMY; $sub->(@args) }; } 
+3
source

All Articles