Why am I getting “called too early to check prototype” warnings in my Perl code?

前端 未结 5 724
你的背包
你的背包 2021-02-07 13:01

I have a Perl file like this:

use strict;
f1();

sub f3()
{ f2(); }

sub f1()
{}
sub f2()
{}

In short, f1 is called before it is d

5条回答
  •  难免孤独
    2021-02-07 13:48

    If you are going to call it with the parenthesis, why are you even using prototypes?

    sub f1(){ ... }
    
    f1();
    

    The only time I would use the empty prototype is for a subroutine that I want to work like a constant.

    sub PI(){ 3.14159 }
    
    print 'I like ', PI, ", don't you?\n";
    

    I would actually recommend against using Perl 5 prototypes, unless you want your subroutine to behave differently than it would otherwise.

    sub rad2deg($){ ... }
    
    say '6.2831 radians is equal to ', rad2deg 6.2831, " degrees, of course.\n";
    

    In this example, you would have to use parenthesis, if it didn't have a prototype. Otherwise it would have gotten an extra argument, and the last string would never get printed.

提交回复
热议问题