If I have a block like the following:
{
say $myVar;
my $myVar=1;
}
I get the expected error:
Variable '$myVar' is not declared
However in a similar fashion with a sub
{
test();
my sub test() {
say "Hello";
}
}
This runs without error and prints:
Hello
Both $myVar and test are not visible outside of enclosing blocks, so in that sense they are both lexically scoped.
The sub must have its declaration 'hoisted' to the top of the block as test is defined and usable before its position in the code. I can't find a reference to back this up however.
What prompted this question is looking at lexical scoped my subs in perl, which gives an 'undefined subroutine' error in the perl version of the second case above. In my understanding of lexical scope, this is what I would expect.
I use this without thought... write some test code, later wrap it up into a sub declared at a bottom of my file and call the sub from earlier in the file. Everything works!
It does leads to the question: Are perl6 subs really lexically scoped in this sense?