如何解决以下错误“未定义的子例程和 main::resetCounters 调用于”?

发布于 2024-12-25 06:38:45 字数 890 浏览 5 评论 0原文

如何解决以下错误“未定义的子例程和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

神魇的王 2025-01-01 06:38:45

除非子程序带有括号,否则不需要原型。如果不包含括号则没有问题。代码如下:

#!/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 = ();
}

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:

#!/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 = ();
}
撧情箌佬 2025-01-01 06:38:45

我不能肯定地说这就是问题所在,但您可以查看 subs 用于预先声明函数的 pragma。

快速的一次......

#!/usr/bin/env perl

use strict;
use warnings;

use subs "myclear";

my $var = 1;

myclear;

print $var;

sub myclear () {
  $var = 0;
}

此外,由于这种过程命令很可能作为其自己的语句发生,因此它实际上不需要空原型,或根本不需要任何原型。

#!/usr/bin/env perl

use strict;
use warnings;

use subs "myclear";

my $var = 1;

myclear;

print $var;

sub myclear {
  $var = 0;
}

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

#!/usr/bin/env perl

use strict;
use warnings;

use subs "myclear";

my $var = 1;

myclear;

print $var;

sub myclear () {
  $var = 0;
}

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.

#!/usr/bin/env perl

use strict;
use warnings;

use subs "myclear";

my $var = 1;

myclear;

print $var;

sub myclear {
  $var = 0;
}
誰認得朕 2025-01-01 06:38:45

这很奇怪。

我最倾向于相信在定义resetCounters之前某些事情正在悄然失败,但是“严格”应该可以防止这种情况发生。

您尝试过使用&符号吗?

&resetCounters();

[编辑]

我唯一见过类似的地方是 CARP。

脚本中的某些内容无法编译,因此 BEGIN 语句无法编译,您最终会从中收到错误,而不是从失败的代码中收到错误。

use CGI::Carp qw(fatalsToBrowser set_message);

# HTML-format error reporter.  Comment out if script wont compile 
BEGIN
    { set_message( \&handle_errors ); }

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?

&resetCounters();

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

use CGI::Carp qw(fatalsToBrowser set_message);

# HTML-format error reporter.  Comment out if script wont compile 
BEGIN
    { set_message( \&handle_errors ); }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文