Luke Palmer wrote:
> I'd rather have an in-betweener block too. Loops like this are very
> common, and I hate doing "prefix" commas, if you know what I mean. I
> realize NEXT often used for cleanup, so maybe you could introduse Yet
> Another block, BETWEEN (or SQUEEZE).
>
> Or are we just going to have to deal with this fact?
We may just have to deal with the fact. Because there's a problem with
in-betweener blocks. Consider:
my $filename;
while get_next_filename($filename) {
FIRST { print "<list>\n" }
my $fh = open $_ or die "Can't open $filename";
NEXT { close $fh }
print extract_id($fh);
BETWEEN { print (-d $filename ?? "/," :: ",") }
LAST { print "</list>\n" }
}
This looks reasonable, but there's a bug.
The C<BETWEEN> block can't decide whether to execute until
it knows whether the loop is going to iterate again. And it can't
know *that* until it has evaluated the condition again. At which
point, the $filename variable has the wrong value. :-(
The example is a little contrived perhaps, but it might be a common
problem. For example, it happens here too:
loop ($n=$from; $n<$to; $n+=$skip) {
print $page[$n];
BETWEEN { print "Skipping to page $( $n+$skip )\n"; }
}
C<$n> is updated before the conditional test, but the C<BETWEEN>
block can't execute until after the conditional.
Don't get me wrong: I *like* the idea of a BETWEEN block.
I just think it's harder to get right (and *much* harder to explain)
than most people would expect.
Damian