How can I solve the following error? Undefined routine & main :: resetCounters called in "?

How can I solve the following error? Undefined subroutine & main :: resetCounters called at "? The subroutine was prototyped, but still Perl complains. The following code is what I'm having problems 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 = (); } 
+4
source share
3 answers

A prototype is not required, unless the routine has parentheses. If you do not include parentheses, then there is no problem. The code will look like this:

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

I can’t say for sure that this is a problem, but you can look in subs pragma for preliminary use of your functions.

Fast passage...

 #!/usr/bin/env perl use strict; use warnings; use subs "myclear"; my $var = 1; myclear; print $var; sub myclear () { $var = 0; } 

In addition, since this procedural command is likely to be executed as its own operator, it really does not 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; } 
+1
source

This is strange.

I would be very inclined to believe that something fails before the resetCounters parameter is set, but then "strict" should prevent it.

Have you tried using ampersand?

 &resetCounters(); 

[EDIT]

The only place I've seen something like this is CARP.

Something in the script does not compile, so the BEGIN operator does not compile, and you end up getting an error from it, and not 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 ); } 
-1
source

Source: https://habr.com/ru/post/1389533/


All Articles