Michael G Schwern writes:
: On Sun, Jan 20, 2002 at 07:25:17PM -0500, Damian Conway wrote:
: > > How would you use an $x lexically scoped to the loop block?
: >
: > You can't...directly. Nor can a C<while> or C<if>. The new rule is that
: > to be lexical to a block it has to be declared in the block, or in the
: > block's parameter list.
: >
: > You'd need to use another layer of braces:
: >
: > do {
: > loop my $x=0; $x < 100; $x++ {
: > ...
: > }
: > }
:
: Hmmm. I understand the desire for lexical simplicity and all, but
: this seems like a Great Leap Backwards to 5.003.
:
: {
: my $foo;
: foreach $foo (@bar) {
: ...
: }
: }
:
: The C<foreach @bar -> $foo> is a good out for the common case, and
: I'll give that more complicated for loops will be uncommon enough so
: having a few block wrappers won't matter. But I'm worried about how
: will we replicate the current behavior of the common idiom:
:
: while( my $line = <FILE> ) {
: ...
: }
That still works fine--it's just that $line lives on after the while.
: Will it be:
:
: while <FILE> -> $line {
: ...
: }
:
: or do we have to start wrapping things up?
I don't think C<while> feeds its boolean to its block as a parameter,
so that probably doesn't work. But this would work fine:
for <FILE> -> $line {
...
}
:
: And then there's this one:
:
: if( my $foo = bar() ) {
: ...
: }
:
: how would that be written?
Just the same, only $foo lives on after the block.
I suppose we *could* say that scalar thingies like C<if> and C<while>
can feed their value to the following block as a parameter if you use
the -> notation. Then you could say
if bar() -> $foo { ... }
But that seems a little bizarre, and I don't know all the implications.
It would seem to imply that if you don't specify a binding for the value
it should default to
if bar() -> $_ { ... }
and I think that's probably not what people will expect. Boolean ops
like C<if> and C<while> should probably stay boolean, and not become
topicalizers like C<given> and C<for>. As it is, we're gonna have
a tricky time making sure lexical $_ propagates into closures in
an intuitive fashion.
Larry