in reply to Scope surprise

Further to Nkuvu's reply above:

To get a better idea of compile-time versus execution-time activity, enclose the second definition of the lexical variable in its own block, and make it a  BEGIN or  INIT block.

Now, the compiler is happy with the second variable (because it lives in its own lexical scope), and whenever  foo() is called, the second variable always has a well-defined value.

(Indeed, the second variable has a very well-defined value: because there is no 'mutator' function defined for it within its scope, it cannot possibly change during the execution of the script.)

>perl -wMstrict -le "my $scalar = 99; foo(); my $scalar = 42; sub foo { print qq{scalar in foo: $scalar} } " "my" variable $scalar masks earlier declaration in same scope at ... Use of uninitialized value in concatenation (.) or string at ... scalar in foo: >perl -wMstrict -le "my $scalar = 99; foo(); BEGIN { my $scalar = 42; sub foo { print qq{scalar in foo: $scalar} } } " scalar in foo: 42