On Fri, 2002-04-26 at 19:06, Allison Randal wrote: > On Fri, Apr 26, 2002 at 05:24:13PM -0400, Aaron Sherman wrote:
> > Of course it brings other less wholesome things to mind like "elsfor" > > and "elsloop" and "if ... elsfor" and "for ... elsif ... elsloop ... > > else", but why not? > > Urk. And why? > > Besides, I would expect an C<elsfor> to actually be a loop of it's own, > on the principle of "elsif = else + if" so "elsfor = else + for". Absolutely what I thought. "elsif" would be for "thing else if" where "elsfor" would be "thing else for-loop". Since you got this distinction right off, it sounds like an intuitive enough syntax. > Yes, but there's a difference with C<while> in that you wouldn't want an > "else" to execute anytime the C<while> came across a false value, but > only if the *first* value it came across was false (i.e. only if the > loop isn't going to execute at all). Otherwise, you'd just use a LAST > block (a parallel that I think argues for ELSE blocks). This is just going up a level in terms of what you're looking at. If I wrote this: if (!grep {$_ eq 'foo'} @bar) { ... } You wouldn't find it confusing that the block only executes if NO element of the array @bar is 'foo'. So, this should not be confusing either: $i=0; loop ;$i<$max;$i++ { ... } else { ... } In this case, if $max <= 0, then you will execute the else. > And C<for> and C<loop> are even more different. You could kind of > stretch the point and say that the evaluation of the second expression > is the false value, I'll give you that. But it still has the same > problem as C<while>. > > $i = $somevalue; # possibly set by user input to 5 or higher > ... > loop ; $i < 5; $i++ { > ... > } > > And where's the "false value" here? The "else" would be more of "no > values to iterate over". Again, it's just first derivative over time. You're not asking "is there a false value", you're asking "is the loop false". Just as we understand that an array in a conditional context is false if it is empty, so too is a loop false if it is "empty". This is a basic Perl concept, and it makes sense to promote it from static arrays to more dynamic loops. > > > 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). You're thinking of loops in a curious and convoluted way, Try this out: { loop-keyword [ conditional/declaration ] block { else-clauses ...} } The loop itself comprises an outer scope in which the conditionals or declarations exist. Then, each block comprises a sub-scope. In this way, there is no confusion, no special case, no violation of the rules. It becomes reasonable now to have: for @a -> $b { stuff using $b } elsfor @c -> $d { more stuff using $b and $d } else { yet further stuff with access to $b and $d } And what a neophyte programmer might expect about access to variables is all true.