in reply to variable based #line directives?

I think you can do the appropriate faking of #line numbers only through eval. #line directives are only evaluated at compile time of code, so they cannot use variables.

If you want to wrap/warp Test::More, you want to look at Test::Builder. It also tells you how to skip levels in reporting the error location - see caller.

Replies are listed 'Best First'.
Re^2: variable based #line directives?
by chorankates (Novice) on Aug 20, 2011 at 22:04 UTC

    thanks very much for suggesting i look at Test::Builder, was able to modify Test::Builder::Level in my wrapping module and i am now getting the correct line/script number.

    just for my edification, can you point me in the direction of some eval #line directive examples? all of the formats i tried before going to Test::Builder did not affect caller() at all. i tried (same code as originally posted, but adding this on line 13 of test.pl):

    • eval "#line $l $ld";
    • eval("#line $l $ld");
    • eval qq{#line $l $ld} or die $!;
    when i tried the last one, i got 'bad file descriptor', which i don't understand either..

      The #line directive takes hold only for the code compiled within the eval string. So you can only "set" the line number by doing

      my $code = <<CODE; #line 200 die "Hello world"; CODE eval $code; warn $@; __END__ Hello world at (eval 1) line 200.
      i got 'bad file descriptor', which i don't understand
      eval returns the value of the last evaluated expression. In your case, there is no expression, so eval returns undef, which triggers die. Use something like
      eval qq{#line $l $ld\n1;} or die "$!";
      $@ is more common (and meaningful) after eval than $! (see perlvar).