The solution that merlyn pointed me at is to use a #line directive like described at the very end of perlstyle. (Bug alert, in Perl 5.005 you need to have a blank line before the directive.)

You can even wrap this up in a function with error handling like this:

use Carp; sub my_eval { my ($pkg, $file, $line) = caller(); my $eval_str = qq(\n# line 1 "eval for file '$file' line $line"\n) . shift; if (wantarray()) { my @res = eval($eval_str); return $@ ? confess($@) : @res; } else { my $res = eval($eval_str); return $@ ? confess($@) : $res; } }
But do be warned, your eval takes place in the scope of my_eval, and not the scope of the function you call it from. (ie You cannot access lexicals.) Unless you somewhere explicitly preprocess your code before compiling it you can't solve this generically without inlining repeated logic.

Doing this in Perl generally requires some sophisticated hackery of the kind that gets you talked about. For the record this is exactly the kind of problem that Lisp macros are perfect for, factoring out repetitive logic in a generic way while keeping current scoping.


In reply to Re (tilly) 3: uninitialized value by tilly
in thread uninitialized value by Cine

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.