perl - 我将如何解决以下错误 "Undefined subroutine &main::resetCounters called at"?

标签 perl prototype subroutine subroutine-prototypes

我将如何解决以下错误“未定义的子例程 &main::resetCounters 调用于”?该子例程已经原型(prototype)化,但 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 = ();
}

最佳答案

除非子例程有括号,否则不需要原型(prototype)。如果不包括括号,则没有问题。代码如下所示:

#!/usr/bin/perl
use strict;
use warnings;

...

print "\nTimeStamp  TPS   MPS    MaxBat  AvgBat  MaxLat  AvgLat  StdLat  >5ms    %>5ms\n";
resetCounters();

...

sub resetCounters #No parentheses
# -----------------------------------------------------------
# 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 = ();
}

关于perl - 我将如何解决以下错误 "Undefined subroutine &main::resetCounters called at"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8748941/

相关文章:

javascript - 来自面向对象 javascript 的 stoyanov javascript 原型(prototype)练习

Javascript 原型(prototype)替换不起作用

perl - 带内联的子程序 perl

c++ - 标准库中的任何正则表达式语法是否支持 (?(DEFINE) 用于子模式引用?

javascript - 如果我在构造函数中使用 for 循环,私有(private)变量不是特定于对象的 : JavaScript

python - 出现错误“赋值前引用局部变量 - 如何修复?

Perl - 在文件中找到匹配项后插入行

regex - 如何使用正则表达式来验证 iSCSI 目标名称?

regex - 如何使用 Perl 正则表达式删除换行符?

perl - Firefox 不删除过期的 Perl CGI::Cookie cookie