Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I determine if a subroutine is being called inside eval using caller?

Tags:

perl

I'm still learning Perl and I was tasked to use caller to determine if a subroutine is being called from an eval at any higher level. I am supposed to come up with some code to test this on and print Yes if its from an eval or No if its not. I cannot find any good examples on how to use caller on the web and was wondering if anyone had any ideas or suggestions on how to go about doing this.

like image 775
user3348452 Avatar asked Mar 04 '14 21:03

user3348452


1 Answers

You shouldn't be using caller for this. Refer to perlvar:

$EXCEPTIONS_BEING_CAUGHT
$^S
Current state of the interpreter.

    $^S         State
    ---------   -------------------------------------
    undef       Parsing module, eval, or main program
    true (1)    Executing an eval
    false (0)   Otherwise

The first state may happen in $SIG{__DIE__} and $SIG{__WARN__} handlers.
The English name $EXCEPTIONS_BEING_CAUGHT is slightly misleading, because the
undef value does not indicate whether exceptions are being caught, since 
compilation of the main program does not catch exceptions.

This variable was added in Perl 5.004.

As to why:

C:\Users\user>perl -MBenchmark -E "timethese(20000000, {'caller' => sub {caller()}, '$^S' => sub {$^S}})"
Benchmark: timing 20000000 iterations of $^S, caller...
       $^S:  0 wallclock secs ( 0.11 usr +  0.00 sys =  0.11 CPU) @ 183486238.53/s (n=20000000)
            (warning: too few iterations for a reliable count)
    caller:  1 wallclock secs ( 0.87 usr +  0.00 sys =  0.87 CPU) @ 22909507.45/s (n=20000000)

And this is before we even bog the caller code down with a number of iterations over the call stack and running string functions against stack levels, assuming we'll write bug-free code for all edge cases, etc.

Writing code to use caller to determine this is a complete re implementation of a core feature. It's like asking, "How do I use scalars to implement a linked list?" The answer should be "Use an array", not "Here's how!"

like image 97
Oesor Avatar answered Nov 15 '22 05:11

Oesor