I would like to be able to specify __LINE__ and __FILE__ values that get used when I run eval(). What's the best way to do this?

Here's why I want to do this. I've been writing a Perl Debugger. Often one wants to evaluate expressions inside the debugger the way the debugged program would. So suppose the program has in it the code: basename(__FILE__). Here __FILE_ refers to the program I am debugging, not say (eval 302).

One hacky way to do this would be to try to look for occurrances of __FILE__ and __LINE__ and and replace them with scalar variables that have the values I want. I write that this is hacky because the really good regular expression probably isn't a regular expression in the classic computer science sense: a good regular expression would understand whether say __FILE__ is inside some sort of quotes or regular expression pattern. And in Perl, there is more than one way to do these.

I've also considered creating a constant or a function for these, but since __FILE__ is handled by the parser this doesn't work.

Lastly, let me mention how this is handled in Ruby. There, the eval function takes a couple of optional additional parameters which specify what to use for __FILE__ and the starting value of __LINE__.


In reply to changing __LINE__ and __FILE__ in eval by rockyb

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.