Austin Hastings wrote:
David Storrs wrote:

On Thu, Jan 13, 2005 at 07:35:19PM -0500, Joe Gottman wrote:


  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.


Actually, I consider this a good thing.  There are lots of times when
I would LIKE my loop variable to persist and, in order to get that, I
need to do the following:

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

It's a minor ugliness, but it itches at me. Under the new Perl6
rules, I can easily have it either way. {for (my $n=0; $n<10; ++$n) {...}} # Local to loop
for (my $n=0; $n<10; ++$n) {...} # Persistent


--Dks




But there's no clean way to make some of them temporary and some persistent.

This seems like a legitimate place for "saying what you intend", viz:

for (my $n is longlasting = 0, $m = 1; ...) {...}

Albeit that's a lame example of how to do it.

What's not clean about

{
  loop my $n = 0; $n < 10; $n++ {
    ...
  }
}

? Works fine for me, shows the scope boundaries very clearly indeed, just the kind of thing a lot of languages are missing, IMO.

Of course, this example's really bad because it's much better written

for 0..9 {
  ...
}

In which case I assume that it only clobbers the topic inside the block, not outside it, as it's somewhat like

for 0..9 -> $_ {
  ...
}

To write it explicitly. Or am I barking up the wrong tree completely?

Reply via email to