Is it possible to prevent direct undeclared function calls in perl?

Perl has limited support for checking static code, in particular, it can check whether we pass the appropriate number of arguments to a function. For example, this will result in an error:

use strict;
use warnings;
sub f($) {}
f(2, 3)

Too many arguments for main::f

If we try to call f too early, we get another useful warning:

use strict;
use warnings;
f(2, 3)
sub f($) {}

main::f() called too early to check prototype

This will result in an error:

use strict;
use warnings;
sub f($) {}
sub g() { f(2, 3) }

Too many arguments for main::f

And my question is, is it possible to get such a message for the following scenario:

use strict;
use warnings;
sub g() { f(2, 3) }
sub f($) {}

Because I do not receive any errors or warnings. It would be nice if I could prevent this code from compiling. Please inform.

+3
source share
3 answers

. , , , , .

, Method:: Signatures, , .

use Method::Signatures;

use strict;
use warnings;

func g() { f(2, 3) }

func f($foo) {
    print "f got $foo\n";
}

( , , , ), (, ), . , .

+2

", " ( "Perl Best Practices" ) , , , " , , ."

+11

Yes, you can by pre-declaring it before using it:

use strict;
use warnings;
sub f ($); # prototype defined
sub g () {f (2, 3)}
sub f ($) {print "hi \ n"; }
+2
source

All Articles