Because Perl is a one-pass compiler
The reason why "Perl has a notion of an undefined function" is because it's a one-pass compiler. All else follows from this simple principle. That's why:
printf "it is '%s'\n", some_function();
is a syntactically legal statement as far as the compiler is concerned. This is easily verified via perl -c
to just compile but not run the code:
% perl -ce 'printf "it is '%s'\n", some_function()'
-e syntax OK
Sure, if you tried to run that, the interpreter will die because you tried to call an undefined subroutine, but that's not really the compiler's business. For further insight, you should examine the compiler's resulting parse tree using the B::Concise module:
% perl -MO=Concise,-exec -e 'printf "it is '%s'\n", some_function()'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark sM
4 <$> const(PV "it is %s\n") sM
5 <0> pushmark s
6 <$> gv(*some_function) s/EARLYCV
7 <1> entersub[t2] lKMS/LVINTRO,TARG,INARGS
8 <@> prtf vK
9 <@> leave[1 ref] vKP/REFC
-e syntax OK
Look specifically at opcode 6: gv(*some_function) s/EARLYCV
. That's telling you this was a coderef that was used before the compiler saw a definition for it.
The very same parse tree is obtained by placing the subroutine definition after the code that calls it:
% perl -MO=Concise,-exec -e 'printf "it is '%s'\n", some_function(); sub some_function { time }'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark sM
4 <$> const(PV "it is %s\n") sM
5 <0> pushmark s
6 <$> gv(*some_function) s/EARLYCV
7 <1> entersub[t2] lKMS/LVINTRO,TARG,INARGS
8 <@> prtf vK
9 <@> leave[1 ref] vKP/REFC
-e syntax OK
This is quite different when the compiler already knows what coderef that name is bound to at compile time, which you can effect by placing the definition before the code that calls it:
% perl -MO=Concise,-exec -e 'sub some_function { time } printf "it is '%s'\n", some_function()'
1 <0> enter
2 <;> nextstate(main 3 -e:1) v:{
3 <0> pushmark sM
4 <$> const(PV "it is %s\n") sM
5 <0> pushmark s
6 <$> gv(IV \&main::some_function) s
7 <1> entersub lKMS/LVINTRO,INARGS
8 <@> prtf vK
9 <@> leave[1 ref] vKP/REFC
-e syntax OK
Now look what has happened to opcode 6! It has become gv(IV \&main::some_function) s
. Now the interpreter won't have to look that coderef up at runtime. The compiler has already provided it.
If you declare the function before the compiler sees you use it, it still can't know what coderef that resolves to until runtime.
% perl -MO=Concise,-exec -e 'sub some_function; printf "it is '%s'\n", some_function(); sub some_function { time }'
1 <0> enter
2 <;> nextstate(main 1 -e:1) v:{
3 <0> pushmark sM
4 <$> const(PV "it is %s\n") sM
5 <0> pushmark s
6 <$> gv(*some_function) s
7 <1> entersub[t2] lKMS/LVINTRO,TARG,INARGS
8 <@> prtf vK
9 <@> leave[1 ref] vKP/REFC
-e syntax OK
Now opcode 6 reads gv(*some_function) s
, because the interpreter still has to look it up in the package symbol table to find the coderef. The compiler wasn't able to provide the coderef's address to the interpreter.
You might find this surprising, given that you yourself can clearly see the function definition yourself later on. But the compiler cannot.
Why not?
It's what I said at the beginning: because Perl is a one-pass compiler, that's why. That's the answer to your question.
All discussions about function prototypes, AUTOLOAD
intercepts, and method resolution are distractions that get lost in the weeds. They describe several interesting ramifications that follow naturally from this initial principle. While these are all perfectly valid — and valuable — observations, they ultimately fail to answer your question because they do not identify the unitary cause behind it all: the single-pass nature of the Perl compiler.