in reply to Scope of lexical variables in the main script

The interleaving of the definition of 'global' variables (either lexical or package) with the invocation of functions that access them has yet another tricky aspect.

Variables are defined at compile time, but not initialized until run time (unless explicitly initialized in something like a  BEGIN or  INIT block or through some other compile-time mechanism such as the use built-in). This leads to situations in which a function can access a variable that has been created and exists (as proven by the fact that the code runs under strictures), but has no defined value. Invocation of the same function at a different point in the code will find the function accessing a defined value. All the more reason to be very circumspect about the use of global variables.

>perl -wMstrict -le "func('before'); my $tricky = 'initialized at runtime'; func('after'); ;; sub func { printf qq{$_[0]: %s defined: }, defined($tricky) ? 'is' : 'NOT'; print qq{'$tricky'}; } " Use of uninitialized value $tricky in concatenation (.) or string ... before: NOT defined: '' after: is defined: 'initialized at runtime'