> > > I can also think of some advantages to having the "else" within the
> > > scope of the loop.
> >
> >     while alllines("/etc/passwd") -> $_ {
> >             ...
> >     } else {
> >             die "/etc/passwd: $_";
> >     }
>
> But the aliased value, $_, is restricted to the scope of the C<while>'s
> block, it isn't going to be accessible in the block associated with the
> C<else> (you would be getting some $_ from an outer scope).

This is the only major reason not to adopt a 'else' on loops, imo.

Two solutions to the problem of accessing 'what' returned false are:

1) don't allow it.
2) Alias the value of the while/loop/if conditional into a special
variable.

while( blah() ) {
  ..
} else { print $COND; }

It's ugly, but it works, and doesn't break the holy scoping rules. And
given how little you're going to want to know what kind of false value as
returned, I don't think it's that bad. Someone might want to come up with
a better variable name, however.

Just trying to contribute something new to the discussion that hasn't been
mentioned a dozen times already... :)

Mike Lambert

Reply via email to