I wasn't particularly enamoured with it, hence my "could be refactored" comment. The reason I didn't refactor it at the time was I couldn't see a nice way how to.
Noting your "personal preferences" emphasis, I hope you don't mind if I respond with my take on things?
And that repetition of 'do stuff' is a problem. In this case, having read a line at the top of the while loop, we need to
And the only way I know how to do that in perl (without artificial means like setting flags and/or double condition tests) is redo.
You said: I'd be happier with a do block, rather than a bare block., but that doesn't work:
#! perl -slw use strict; my $i =0; do{ print ++$i; redo if $i < 5; }; __END__ c:\test>junk2 1 Can't "redo" outside a loop block at c:\test\junk2.pl line 7.
You'd have to do
#! perl -slw use strict; my $i =0; do{{ print ++$i; redo if $i < 5; }}; __END__ c:\test>junk2 1 2 3 4 5
That is, embed a bare block within the do block, and that is redundant and very obscure.
You could adopt a Perl 6 like construct:
LOOP:{ ... redo LOOP; }
which could be construed as clearer. But frankly, redo in a bare block is a perfectly valid and useful construct and, I think, it is better to just become familiar with it than to obscure it. Indeed, it is actually the most flexible looping construct. It can be used to construct all many other looping constructs Perl has. Even the much decried but extremely flexible C-style for loop with its otherwise unique ability to vary multiple indexes concurrently.
## draw the diagonals for( my $x=0, my $y=0; $i < $xMax; $x++, $y++ ) { draw( $x, $y ); +} for( my $x=0, my $y=$yMax; $i < $xMax; $x++, $y-- ) { draw( $x, $y ); +}
It's a little used feature, but when you need it, you need it:
Hm. I'm not sure what the position of chomp has to do with the loop construct. The chomp has to follow the readline. The readline has to occur in the middle of the loop.
I'm still not happy with the construction I posted, but I haven't come up with a better one.
In reply to Re^3: Comment a block that match a keyword
by BrowserUk
in thread Comment a block that match a keyword
by yorkwu
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |