in reply to Re: print stack of "do" files on error
in thread print stack of "do" files on error

Thanks for answer Hauke,

but I now think I should discard "do" and use eval + #line trick suggested earlier. Because with "do" there is no way to distinguish an error (like file not found) from the script just returning undef (is this right?).

I also realized I need function call stack inside each file as well. I am now trying to come up with something combining caller and eval + #line.
  • Comment on Re^2: print stack of "do" files on error

Replies are listed 'Best First'.
Re^3: print stack of "do" files on error
by haukex (Archbishop) on Apr 05, 2016 at 22:43 UTC

    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