in reply to Determining depth of eval nesting

You might be able to derive the info by some judicious parsing of the return of caller and assignments to a (relatively) globals scoped variable:

eval qq[ print "1:$^S"; print caller(); eval q[ print "2:$^S"; print caller(); eval q/ print "3:$^S"; print caller(); / ] ];; 1:1 main (eval 55) 1 2:1 main (eval 56) 1 3:1 main (eval 57) 1

The "subname" element in the caller return text seems to reliably indicate the number of evals entered. By parsing and storing the current value prior to entering and eval, it would give you a rough guide.

It wouldn't distinguish between a 3rd level of nested eval, and the second of two evals serially nested within an outer one though.

Maybe you could override CORE::GLOBAL::eval and institute your own depth counter?


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.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re^2: Determining depth of eval nesting
by ferreira (Chaplain) on Apr 03, 2007 at 18:58 UTC
    Maybe you could override CORE::GLOBAL::eval and institute your own depth counter?

    In this case, you may find helpful the recently released overload::eval module.

    Update: As diotalevi pointed this module is only about eval STRING (and not eval BLOCK) and requires the latest devel Perl versions. So, maybe the module is not that helpful here.

      No you won't. overload::eval is only about string eval and you need to be using perl 5.9+ anyway. It overloads the real internal eval function for objects of a particular class. It could be adapted in a 5.6+ compatible way to overload all string eval uses everywhere. To overload the eval block form would require entirely different code and might not even be possible to do cleanly.

      ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊