yary not.com-at-gmail.com |Perl 6| wrote:
I was wondering why the perl5 example didn't work in p6- $_ is a
contextual variable,


 so why doesn't the body of "odd" get its $_ value
from grep in something like this:
sub odd_a { $_ % 2}
If you make it a formally declared "sub", then you have to set up your local $_ yourself. If you wrote it as a bare block, then using $_ in that block would be enough to make it your parameter.

If you don't declare it, I'm not sure if you get a local one implicitly or if it makes a closure from the lexical scope containing the sub. Either way, it's not the dynamic scope.


sub odd_b { $*_ % 2}
Sayith S02, "Note that |$*_| will always see the |$_| in the current scope, not the caller's scope"
That implies that it is declared for you whether you use or not.

sub odd_c { $CONTEXT::_ % 2 }

I'd prefer CALLER to CONTEXT, but in this case they should be the same.

say grep &odd_a, 0..6
(calls "say" 7 times with an uninitialized value. Same with &odd_b, &odd_c)

So the answer to "

why the perl5 example didn't work" is "more use of lexical scoping rules."

--John

Reply via email to