in reply to Re: Forward-referenceing subs
in thread Forward-referenceing subs
You should note that your proposed solution (using Perl in a C-like fashion with a main() function) does not actually aid the compiler in any way.
The compiler compiles all tokens in sequence from the first to the last. Certain constructs, such as 'BEGIN{...}' or 'use ...;' are executed as soon as they finish compiling, and cause the compiler to recurse, while other constructs such as "sub NAME ..." generate symbol table entries as soon as they finish compiling.
Your code defines the symbol 'main' (perhaps confusing, given that the fully qualified name is 'main::main'), uses the symbols 'get_input' and 'say_hello'. Then defines the symbol 'get_input' and 'say_hello'. At the very end, the compiler terminates and the execution phase begins. The first executable statement is 'main()'.
Definately, choosing to always invoke subroutines using () over using list operator syntax, will avoid this particular problem. The problem that it does not avoid, is the situation where a subroutine is invoked, but later prototyped:
a(@a); sub a ( \@ ) { my($array_reference) = @_; }
If -w is not used, Perl is perfectly happy to execute the above code generating undesirable results ($array_reference is assigned the first value in @a). If -w is specified, Perl generates the warning message:
main::a() called too early to check prototype at ...Moral of the story: If you use prototypes at all, make sure to always pre-declare your subroutines using "sub NAME (PROTOTYPE);". However, if you have read about the pitfalls regarding prototypes, you may decide that it is reasonable to completely avoid using prototypes at all, in which case, always using () when invoking subroutines will eliminate the problem described by the original poster (list operator not recognized).
Secondary moral of the story: -w can be extremely useful.
|
---|