[Chicago-talk] fun with Carp

Mike Fragassi frag at ripco.com
Tue Jul 31 09:37:58 PDT 2007


This is driving me nuts.  I've got a script that basically does the
following:

#--------------------------------------------------------------#
use Carp;

attempt_something()
   or quit_gracefully("attempt failed; quitting");

sub attempt_something {
   # Oops, the attempt fails:
   return undef;
}

sub quit_gracefully {
   my $error_msg = shift;
   # ... do some recovery operations here ...
   # ... and then call quit():
   quit($error_msg);
}

sub quit {
   my $error_msg = shift;
   # ... do some other stuff here, and then, the coup de grace:
   Carp::croak($error_msg);
}
#--------------------------------------------------------------#

Carp.pm, in its finite wisdom, gives me a full stack trace:

   attempt failed; quitting at /usr/local/upld/bin/ctest.pl line 25
         main::quit('attempt failed; quitting') called at /usr/local/upld/bin/ctest.pl line 20
         main::quit_gracefully('attempt failed; quitting') called at /usr/local/upld/bin/ctest.pl line 9

When all I really want is the _last_ line, indicating the original point
where the chain started:
   main::quit_gracefully('attempt failed; quitting') called at /usr/local/upld/bin/ctest.pl line 9
(ideally without the 'main::').

Replacing Carp::croak with "die Carp::shortmess" doesn't change 
anything.  Nor does adding $Carp::Verbose = 0 right before the croak().
Plain old die(), of course, gives me just the first line, which is 
useless.

I could "fix" it by jumping through some complicated hoops: wrapping the 
subs quit() and quit_gracefully() in a separate package, and then having 
to modify all the calls to Foo::quit_gracefully(), but that's kludgy, 
and I expect something as basic and core as Carp to DWIM.

I can't tell if there's something obvious I'm forgetting or missing, or 
if Carp's just so geared for OOP that it's completely incapable of DWIM 
in scripting uses like this.

Any suggestions?

-- Mike F.



More information about the Chicago-talk mailing list