On Mon, Jan 17, 2011 at 4:44 AM, David Christensen < dpchr...@holgerdanske.com> wrote:
> module-authors: > > I'm not sure where to ask this question, so please refer me to a better > resource if there is one... > > > I'm trying to implement a centralized exception generation function, say > myconfess(), that I can use throughout my code. myconfess() will do some > things and then call Carp::confess(). I'd like the stack trace to appear to > come from the point where myconfess() was called, rather than from where > myconfess() calls Carp::confess(). > > > Carp includes two hashes which are supposed to enable this -- > %Carp::Internal and %Carp::CarpInternal. I've tried both, but can't seem to > get them working. $Carp::CarpLevel does seem to work. > > > Here's some demonstration code. The eval's around Carp::confess() are > there so that I can trap the exception, print it, and keep going: > > 2011-01-16 19:39:31 dpchrist@p43400e ~/sandbox > $ nl -b a carp-internal.pl > 1 #!/usr/bin/perl > 2 > 3 package Foo; > 4 use Carp; > 5 $Carp::Internal{ __PACKAGE__ }++; > 6 sub myconfess { > 7 eval { > 8 Carp::confess @_; > 9 }; > 10 print $@, "\n"; > 11 } > 12 > 13 package Bar; > 14 use Carp; > 15 sub myconfess { > 16 local $Carp::CarpLevel = 2; > 17 eval { > 18 Carp::confess @_; > 19 }; > 20 print $@, "\n"; > 21 } > 22 > 23 package Baz; > 24 use Carp; > 25 $Carp::CarpInternal{ __PACKAGE__ }++; > 26 sub myconfess { > 27 eval { > 28 Carp::confess @_; > 29 }; > 30 print $@, "\n"; > 31 } > 32 > 33 package main; > 34 sub run { > 35 Foo::myconfess('goodbye, cruel world!'); > 36 Bar::myconfess('goodbye, cruel world!'); > 37 Baz::myconfess('goodbye, cruel world!'); > 38 } > 39 run(); > The packages you declare "internal" are not what you think they are. Observe: sidhekin@bluebird[07:57:50]~$ perl -le '$t{ __PACKAGE__ }++; print keys %t;' __PACKAGE__ sidhekin@bluebird[07:57:52]~$ perl -le '$t{ +__PACKAGE__ }++; print keys %t;' main sidhekin@bluebird[07:57:53]~$ perl -le '$t{ (__PACKAGE__) }++; print keys %t;' main sidhekin@bluebird[07:57:54]~$ Careful with those barewords, now. ;-) -SK-