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

user65457 picture user65457 · Nov 12, 2009 · Viewed 17.6k times · Source

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 defined. So, Perl throws a warning: "f1 called too early to check prototype". But same is the case with f2, the only diff being that it is called from inside another subroutine. It doesn't throw a warning for f2. Why?

What is the best way to resolve this issue?

  1. declare the subroutine before it is called
  2. call the sub like this: &f1();

Answer

Sinan Ünür picture Sinan Ünür · Nov 12, 2009

You can completely avoid this issue by not using prototypes in the first place:

use strict;

f1();

sub f3 { f2() }

sub f1 {}
sub f2 {}

Don't use prototypes unless you know why you are using them:

This is all very powerful, of course, and should be used only in moderation to make the world a better place.