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

There is no way I know of to (re)set the eval counter, but the usual trick I use to associate evals with the evalled code is to embed a #line comment in your eval() string:

perl -le "eval q!print $^O! for 1..10; eval qq!#line 99 foo.cfg\ndie!; +print $@"
gives
MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 MSWin32 Died at foo.cfg line 99.

Replies are listed 'Best First'.
Re^2: is it possible to access eval() counter? (new#line)
by tye (Sage) on Jun 30, 2006 at 18:11 UTC

    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