如何解决以下错误“未定义的子例程和 main::resetCounters 调用于”?
如何解决以下错误“未定义的子例程和 main::resetCounters 调用于”?子例程已经原型化,但 Perl 仍然抱怨。以下代码是我遇到的问题:
#!/usr/bin/perl
use strict;
use warnings;
...
sub reportStats();
sub resetCounters(); #HERE IS THE PROTOTYPE
sub getUpperBusTimeStampAndBatchSize($);
sub toMs($);
sub tibTimeToMs();
sub calcStdDev();
...
print "\nTimeStamp TPS MPS MaxBat AvgBat MaxLat AvgLat StdLat >5ms %>5ms\n";
resetCounters(); #THIS IS THE LINE CONTAINING THE ERROR
...
sub resetCounters()
# -----------------------------------------------------------
# resets all metrics counters
# -----------------------------------------------------------
{
$tps = 0;
$mps = 0;
$batch = 0;
$maxBatch = 0;
$avgBatch = 0;
$latency = 0;
$latencySum = 0;
$maxLatency = 0;
$avgLatency = 0;
$overThreshold = 0;
$percentOver = 0;
$currentSecond = $second;
@latencies = ();
}
How would I solve the following error "Undefined subroutine &main::resetCounters called at"? The subroutine has been prototyped but still Perl complains. The following code is what I am having issues with:
#!/usr/bin/perl
use strict;
use warnings;
...
sub reportStats();
sub resetCounters(); #HERE IS THE PROTOTYPE
sub getUpperBusTimeStampAndBatchSize($);
sub toMs($);
sub tibTimeToMs();
sub calcStdDev();
...
print "\nTimeStamp TPS MPS MaxBat AvgBat MaxLat AvgLat StdLat >5ms %>5ms\n";
resetCounters(); #THIS IS THE LINE CONTAINING THE ERROR
...
sub resetCounters()
# -----------------------------------------------------------
# resets all metrics counters
# -----------------------------------------------------------
{
$tps = 0;
$mps = 0;
$batch = 0;
$maxBatch = 0;
$avgBatch = 0;
$latency = 0;
$latencySum = 0;
$maxLatency = 0;
$avgLatency = 0;
$overThreshold = 0;
$percentOver = 0;
$currentSecond = $second;
@latencies = ();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
除非子程序带有括号,否则不需要原型。如果不包含括号则没有问题。代码如下:
The prototype is not required except when the subroutine has parentheses. If you do not include parentheses then there is no issue. The code would look like:
我不能肯定地说这就是问题所在,但您可以查看
subs
用于预先声明函数的 pragma。快速的一次......
此外,由于这种过程命令很可能作为其自己的语句发生,因此它实际上不需要空原型,或根本不需要任何原型。
I can't say for sure that this is the problem but you might look into the
subs
pragma for predeclaring your functions.A quick one off ...
Further, since this kind of procedural command is likely to happen as its own statement, it really doesn't need a null prototype, or any prototype at all.
这很奇怪。
我最倾向于相信在定义resetCounters之前某些事情正在悄然失败,但是“严格”应该可以防止这种情况发生。
您尝试过使用&符号吗?
[编辑]
我唯一见过类似的地方是 CARP。
脚本中的某些内容无法编译,因此 BEGIN 语句无法编译,您最终会从中收到错误,而不是从失败的代码中收到错误。
That's bizarre.
I'd be most inclined to believe that something is failing silently before resetCounters is being defined, but then, "strict" should prevent that.
Have you tried using ampersand?
[EDIT]
The only place I've seen something similar is with CARP.
Something in the script doesn't compile, so the BEGIN statement doesn't compile and you end up getting an error from it, rather than from the code that failed.