kir has asked for the wisdom of the Perl Monks concerning the following question:

Hi!

I have a set of config files (written in perl) that can include each other. How can I get an error message with a line number for each including file? It should resemble C compiler errors:

included from a line 2: included from b line 5: error in err: Bareword "z" not allowed while "strict subs" in use at err line 25.
By now I've been able to get only this:
included from a: included from b: included from err: Bareword "z" not allowed while "strict subs" in use at err line 25.
I use the following module for "include" function:
package L; sub include { my $f = shift; my $r = do $f; unless (defined $r) { die "included from $f:\n$@" if $@; } } 1;
And config looks like this:
use L; ... L::include "a"; ... L::include "b";

My configs are many and each may be included many times, so it's hard to pinpoint an error without this.

UPDATE: there are also some function calls inside configs I need their line too in a trace

UPDATE: Finally I've got what I wanted. Includes are A -> B -> C -> D -> ERR where file ERR is malformed:

error loading 'ERR': Bareword "z" not allowed while "strict subs" in u +se at ERR line 5. stack trace: L::include called at D line 1 L::include called at C line 1 L::include called at B line 11 L::bar called at B line 7 L::foo called at B line 14 L::include called at A line 2

The downside is that the code (below) relies on the presence of 'stack trace' string. There must be a better solution.

package L; use warnings; use strict; use File::Slurp; sub include { my ($f) = @_; eval { my $e = eval qq(#line 1 "$f"\n) . read_file $f; die $@ if $@; }; if ($@) { die $@ if ref($@) ne '' or $@ =~ /stack trace:/s; my $max_depth = 30; my $err = "error loading '$f': $@stack trace:\n"; my $e = -1; my @stack; my $i; for ($i = 0 ; $i < $max_depth ; ++$i) { my ($package, $filename, $line, $subroutine) = caller($i); last unless $package; # removing two evals below every include splice(@stack, -2, 2) if @stack > 2 and $subroutine eq "L::include"; push(@stack, " $subroutine called at $filename line $lin +e"); } $err .= join("\n", @stack, ""); $err .= "...\n" if $i == $max_depth; die $err; } } 1;

Replies are listed 'Best First'.
Re: print stack of "do" files on error
by Corion (Patriarch) on Apr 03, 2016 at 19:36 UTC

    See perlsyn. If you prepend a #line comment in front of the file, Perl will adjust the error message accordingly:

    sub include { my($filename) = @_; open my $fh, '<', $filename or die "Couldn't read config file '$filename': $!"; my $content = do { local $/; <$fh> }; my $r = eval { join "\n", sprintf( qq(#line 1 "%s"', $filename ), $content }; ... }
Re: print stack of "do" files on error
by haukex (Archbishop) on Apr 03, 2016 at 20:58 UTC

    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

      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