in reply to Another variable scoping oddity
"my" variable $k masks earlier declaration in same scope at mytest.pl +line 9.
If you put your subroutine declaration and definition BEFORE the declaration of the first "my $k" you'll get the following error (under strictures):
Global symbol "$k" requires explicit package name at mytest.pl line 8. Global Symbol "$k" requires explicit package name at mytest.pl line 8. Execution of mytest.pl aborted due to compilation errors.
That is because $k is seen in the definition of the subroutine before it is declared by the first my $k; in the script.
So your code as it stands now is passing $k into the subroutine 'globally' (In this case I use that term to mean, "not through the parameter list"). And the first declaration of $k is masked by the second. And since the sub is being declared and defined chronologically after the second my, the declaration of the first my $k is masked and not seen by the sub.
Dave
|
|---|