[Chicago-talk] fun with Carp

Pete Krawczyk mongers at bsod.net
Tue Jul 31 10:01:40 PDT 2007

Subject: [Chicago-talk] fun with Carp
From: Mike Fragassi <frag at ripco.com>
Date: Tue, 31 Jul 2007 11:37:58 -0500 (CDT)

}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::').

If you don't care about the function arguments upstream, Sub::UpLevel will 
do what you want:

--- ctest.pl    2007-07-31 11:52:00.909062760 -0500
+++ foo.pl      2007-07-31 11:54:38.338129904 -0500
@@ -1,4 +1,5 @@
 use Carp;
+use Sub::Uplevel;

    or quit_gracefully("attempt failed; quitting");
@@ -18,5 +19,5 @@
 sub quit {
    my $error_msg = shift;
    # ... do some other stuff here, and then, the coup de grace:
-   Carp::croak($error_msg);
+   uplevel 2, sub { Carp::croak($error_msg) };

$ perl foo.pl
attempt failed; quitting at foo.pl line 22
        main::quit_gracefully() called at foo.pl line 4

As for the main::, you'll always have that in this instance because main:: 
is the package from which the function was called.  In your module, I 
would expect it 

I'm not sure why Sub::Uplevel's overloaded CORE::GLOBAL::caller loses 
function arguments, but I'm also not looking into it right now.

-Pete K
Pete Krawczyk
    Chicago Perl Mongers
    mongers at bsod dot net

More information about the Chicago-talk mailing list