in reply to Re: is it possible to access eval() counter?
in thread is it possible to access eval() counter?

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