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;

In reply to 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.