Question

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 = ();
}
Was it helpful?

Solution 2

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

OTHER TIPS

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;
}

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 ); }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top