Hi kir,

Out of curiosity I played around with this a bit more (using Carp, Carp::Always, and Carp::Clan; I didn't get around to Devel::SimpleTrace or Acme::JavaTrace), but I discovered that none of these modules gave me enough control, there was always something off about the error messages. So I came to the same conclusion as you, that I'd have to build my own stack trace, and I found Devel::StackTrace. Here's what I came up with:

package L; use warnings; use strict; use Devel::StackTrace; our $DYING = 0; sub include { my $file = shift; unless (my $return = do $file) { my $e; if ($@) { $e = "Couldn't parse $file: $@" } elsif (!defined $return) { $e = "Couldn't do $file: $!\n" } elsif (!$return) { $e = "Couldn't run $file\n" } if ($e) { # don't add a new stack trace if we're already dying die $@ if $DYING++; die join '', $e, map {"\t".$_->as_string."\n"} Devel::StackTrace->new(ignore_package=>__PACKAGE__)->f +rames; } } } 1;

Output:

Couldn't parse X.pm: Blammo at X.pm line 9. L::include('X.pm') called at B.pm line 8 L::include('B.pm') called at A.pm line 9 A::load_b('foo') called at A.pm line 11 L::include('A.pm') called at test.pl line 8

(If you wanted to get a stack trace from inside the failing file, you'd have to install a handler there, too.)

Because with "do" there is no way to distinguish an error (like file not found) from the script just returning undef (is this right?).

The normal Perl convention is that files that are required/used end on a true value (often 1;). If your config files return a true value the same way, then you'll be able to distinguish those cases with the above code.

Hope this helps,
-- Hauke D


In reply to Re^3: print stack of "do" files on error by haukex
in thread print stack of "do" files on error by kir

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.