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:
By now I've been able to get only this: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.
I use the following module for "include" function:included from a: included from b: included from err: Bareword "z" not allowed while "strict subs" in use at err line 25.
And config looks like this:package L; sub include { my $f = shift; my $r = do $f; unless (defined $r) { die "included from $f:\n$@" if $@; } } 1;
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;
In reply to print stack of "do" files on error by kir
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |