In Perl5,  given code like

    for (my $n = 0; $n < 10; ++$n) {.}

the control variable $n will be local to the for loop.  In the equivalent
Perl6 code
   loop my $n = 0; $n < 10; ++$n {.}

$n will not be local to the loop but will instead persist until the end of
enclosing block.  

   It would be nice if there were some easy way to mimic the Perl5 behavior
in Perl6.  In Perl6, the canonical way to make a variable local to a block
is by making it a parameter. I therefore suggest allowing the following
syntax:

    loop 0 -> $n; $n < 10; ++$n {...}

This will declare $n as a control variable to the loop and initialize it to
0.  Like any parameter, it will then be local to that block (assuming the
comparison and update portions of the loop statement are injected into the
block).  One subtlety is that $n should probably be treated as if it were
declared with the "is copy" attribute, so that it can be modified inside the
loop (by the update portion if nothing else), but does not alias a variable
it was initialized from.  

Joe Gottman



Reply via email to