in reply to print stack of "do" files on error

Hi kir,

One way to do this is with caller. Also I've included the error checking suggested in do.

sub include { my $file = shift; unless (my $return = do $file) { my (undef, $callerfile, $callerline) = caller; my $from = "included from $callerfile line $callerline"; die "Couldn't parse $file, $from:\n$@" if $@; die "Couldn't do $file, $from: $!" unless defined $return; die "Couldn't run $file, $from" unless $return; } }

Using some test code in which test.pl uses include on A.pm, which in turn includes B.pm, then X.pm with a die, I get this output:

Couldn't parse A.pm, included from test.pl line 8: Couldn't parse B.pm, included from A.pm line 7: Couldn't parse X.pm, included from B.pm line 7: Blammo at X.pm line 6.

Hope this helps,
-- Hauke D

Replies are listed 'Best First'.
Re^2: print stack of "do" files on error
by kir (Initiate) on Apr 04, 2016 at 05:04 UTC

    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.

      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