in reply to Re^2: Confusion about BEGIN block
in thread Confusion about BEGIN block

The strict check has a compiletime component to it. BEGIN blocks are guaranteed to be executed prior to anything else in the script, but they are compiled alongside the rest of the script, as they are seen, as the script is compiled line by line. In your script, 'print' is seen and compiled before your BEGIN block, and though the BEGIN block is executed before your 'print', the deed is already done; the compiler with strictures turned on has noticed that you have referred to a variable that hasn't been seen/declared yet. Yes, BEGIN executes first, but it is seen by the compiler in sequential order as the script is parsed top to bottom.

Using the use vars pragma as I suggested solves your scoping issues, but it doesn't change the fact that at compiletime, with the use strict pragma in place, the compiler is seeing a variable getting used before it is seeing the variable being declared. That's where you need to change your order, or move the "our" to outside the BEGIN block.


Dave