我还在学习Perl,我的任务是caller确定是否从eval更高级别调用子程序.我应该想出一些代码来测试它并打印Yes如果它来自eval或No如果不是.我找不到任何关于如何caller在网上使用的好例子,并且想知道是否有人对如何进行此操作有任何想法或建议.
你不应该使用调用者.参考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.
Run Code Online (Sandbox Code Playgroud)
至于为什么:
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)
Run Code Online (Sandbox Code Playgroud)
这是在我们甚至通过调用堆栈上的多次迭代以及针对堆栈级别运行字符串函数来调低调用者代码之前,假设我们将为所有边缘情况编写无错误代码等.
编写代码以使用调用程序来确定这是核心功能的完整重新实现.这就像问"我如何使用标量实现链表?" 答案应该是"使用阵列",而不是"这是怎么回事!"
用代码:
#!/usr/bin/perl -w
eval "test_eval();";
test_eval();
sub test_eval {
my ($e,$v,$a,$l,) = caller(0);
print "[$e], [$v], [$a], [$l]\n";
}
Run Code Online (Sandbox Code Playgroud)
我得到输出
[main], [(eval 1)], [1], [main::test_eval]
[main], [test.pl], [21], [main::test_eval]
Run Code Online (Sandbox Code Playgroud)
但如果我改变它来使用caller(1)然后我得到
[main], [test.pl], [19], [(eval)]
[], [], [], []
Run Code Online (Sandbox Code Playgroud)
以及一些关于未初始化值的警告。
这应该为您提供一个工作起点。请注意,该程序中索引 3 之后的数组中的值已被忽略,因为它们似乎与当前的问题无关,但请返回文档caller以了解这些值中的任何一个是否有用。
编辑:
鉴于另一个答案的讨论和内容,您可以提供一个像这样厚颜无耻的解决方案:
sub func_under_eval {
if (0) { # change this to 1 when the time is right
return (defined($^S) and $^S>0)?"Yes":"No";
}
else {
my @calls = caller(0);
my $back = 1;
while (defined($caller[0])) {
if (index("(eval", $caller[1] . $caller[3])>-1)
return "Yes";
@calls = caller($back++);
}
return "No";
}
}
Run Code Online (Sandbox Code Playgroud)