On Fri, Apr 26, 2002 at 08:49:23AM +1000, Damian Conway wrote:
> Trey Harris wrote:
> 
> > So:
> > 
> > for $results.get_next() {
> >   FIRST { print "Results:<BR>"; }
> >   NEXT { print "<HR>"; }
> > } else {
> >   print "No results.";
> > }
> > 
> > Do I have that right?
> 
> Yes. Presuming Larry decides in favour of C<FIRST> and C<else>.

Hmmm... how about:

for $results.get_next() {
  print $_;
  LAST { print "Done."; }
  ELSE { print "No results."; }
}

The "else" of a loop construct isn't really the same as the "else" of an
C<if>. You can't use an C<elsif> for one thing. And the condition for
reaching the "else" is different too, it isn't always a "false value",
sometimes it's "no values to iterate over" or "condition met before
first execution of loop". This syntax makes the difference pretty
obvious, and fits in nicely with the other NAMED blocks ("Alphabet
Blocks" :).

I can also think of some advantages to having the "else" within the
scope of the loop. You might want your object to return a "false" value
from .get_next() (one that C<for> will see as non-iterate-able) but that
still contains some interesting properties that can be used by the
"else". If the "else" followed the loop this value wouldn't be in scope.
This presumes that the value would still be topicalized/aliased even
when it wasn't iteratable. Perhaps this makes the most sense with a
C<while>, where it is simply testing for truth.

  method get_next {
    ...
    return "xyz572" but false if $something;
    ...
  }

  ...

  while $result.get_next() -> $next {
    # do something with $next...
    ELSE {
      if $next eq "xyz572" {
        print "We defined this value, $next, as false for obscure purposes.";
        print "No loop was executed. Just wanted to let you know.";
      }
    }
  }

Hmmm... this leads me to wonder if we wouldn't perhaps have a use for
an "elsif" on loops afterall, for actions we want to execute only when
the the loop doesn't execute and another condition is true:

  for $result.get_next() -> $next {
        print $next;
    ELSEIF $result.weird() { print "I meant to do that."; }
  }

Allison

Reply via email to