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

Hi all,

It is possible to determine if, at any point of time, we are executing withing an eval block by checking the value of $^S.

Does anyone know if it's possible to determine if we are a certain depth within nested evals? $^S remains 1 no matter what depth.

Thanks.

Replies are listed 'Best First'.
Re: Determining depth of eval nesting
by snoopy (Curate) on Apr 03, 2007 at 07:08 UTC
    The caller function might be you best bet. It can be used to examine the call stack. Nested evals have '(eval)' as the subroutine name:
    #!/usr/bin/perl use strict; use warnings; sub eval_depth { my $eval_depth = 0; my $frame = 0; while (my @caller_info = caller($frame++)) { if ($caller_info[3] eq '(eval)') { $eval_depth++; } } return $eval_depth; } print "no evals: ".eval_depth()."\n"; eval 'print "one eval :".eval_depth()."\n"'; eval q{ eval 'print "two evals:".eval_depth()."\n"'};
      Sorry for the late reply - been on hols.

      ++ This worked a charm - I figured it could be gotten from caller() but I didn't expect it to be this easy.

Re: Determining depth of eval nesting
by BrowserUk (Patriarch) on Apr 03, 2007 at 07:20 UTC

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

        ⠤⠤ ⠙⠊⠕⠞⠁⠇⠑⠧⠊

Re: Determining depth of eval nesting (patch)
by tye (Sage) on Apr 04, 2007 at 15:49 UTC

    Go ahead and submit a patch to p5p to make $^S increment for each nested eval. That'd be a nice improvement.

    - tye