rockyb has asked for the wisdom of the Perl Monks concerning the following question:

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__.

Replies are listed 'Best First'.
Re: changing __LINE__ and __FILE__ in eval
by tobyink (Canon) on Aug 05, 2012 at 16:21 UTC

    See perlsyn - under the heading "Plain Old Comments (Not!)".

    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

      Thanks! This is exactly what I wanted. You guys rock!

      My commit based on this information has been tested and applied. I leave it to others to rework perl5db along the same lines if that is of interest.

Re: changing __LINE__ and __FILE__ in eval
by BrowserUk (Patriarch) on Aug 05, 2012 at 18:06 UTC

    See Re: Re: Re: Line Numbers.


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

      Thanks also for the quick reply. Again, this is exactly what I wanted.