in reply to BEGIN block and prototyped subroutines

The BEGIN block is executed at parse time, but only when the parser finds it - it works top-down so it finds the prototype definition after it has parsed the call, when it is too late.

There is an alternative, which uses a similar technique to C, and that is to declare the prototype before the call. For example:
use warnings; use strict; mysub('duff'); sub mysub() { print "mysub\n" }
Gives:
main::mysub() called too early to check prototype
But we add the declaration:
use warnings; use strict; sub mysub(); # mysub declaration mysub('duff'); sub mysub() { print "mysub\n" }
and we get:
Too many arguments for main::mysub
as expected.

Replies are listed 'Best First'.
Re^2: BEGIN block and prototyped subroutines
by hangon (Deacon) on Apr 18, 2010 at 08:12 UTC

    Ok thanks. Thinking about this and a little more research clears up some misconceptions I had about how the parse/compile/run cycle works.