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();

2011-01-16 19:39:42 dpchrist@p43400e ~/sandbox
$ perl carp-internal.pl
goodbye, cruel world! at carp-internal.pl line 8
        eval {...} called at carp-internal.pl line 7
        Foo::myconfess('goodbye, cruel world!') called at carp-internal.pl line 
35
        main::run() called at carp-internal.pl line 39

goodbye, cruel world! at carp-internal.pl line 36
        main::run() called at carp-internal.pl line 39

goodbye, cruel world! at carp-internal.pl line 28
        eval {...} called at carp-internal.pl line 27
        Baz::myconfess('goodbye, cruel world!') called at carp-internal.pl line 
37
        main::run() called at carp-internal.pl line 39


I would like the output to be:

goodbye, cruel world! at carp-internal.pl line 35
        main::run() called at carp-internal.pl line 39

goodbye, cruel world! at carp-internal.pl line 36
        main::run() called at carp-internal.pl line 39

goodbye, cruel world! at carp-internal.pl line 37
        main::run() called at carp-internal.pl line 39


Any suggestions?


TIA,

David

Reply via email to