Note that in older versions of Perl you'll need a newline in front of the #line directive.

Note also that you can make the line number and file name actually match where the eval'd code is. For example, Algorithm::Loops' t/assert.t evals a bunch of code after __END__ but makes any fatal errors report the correct line number via:

# ... my $lineNum; sub SetLineNum { $lineNum= 1+(caller(0))[2]; } while( <DATA> ) { $lineNum++; # ... eval "\n#line $lineNum $0\n$_\n; 1" or die $@; # ... } # ... BEGIN { SetLineNum } __END__ # ...

More typical tricks just use __LINE__ (plus a constant) and __FILE__:

my $file= '"' . __FILE__ . '"'; eval join " ", "\n#line", 1+__LINE__, $file, "\n", <<END, "; 1" or die + $@; ... END

Though, you might want to do a bit extra work if you are on an operating system that allows obnoxious things like file names that contain double quotes.

- tye        


In reply to Re^2: is it possible to access eval() counter? (new#line) by tye
in thread is it possible to access eval() counter? by Anonymous Monk

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.