in reply to Control Structures
The "loop-and-a-half" problem has already been mentioned, but I still haven't seen anything that looks really good (though the redo aproach may be the cleanest).
Something I've frequently found myself doing is wanting a three-way control structure for greater than, less than, and equal to. Sure there are ways to do it, but none of them really feel clean.
orgiven fork { case $_ < 0 { #error } case $_ > 0 { #parent } default { #child } }
But it's the sort of thing that feels like it could be simpler. A better example might be checking two values against each other, where it feels unnatural to need a separate variable or compare them multiple times.if ((my $pid = fork()) < 0){ #error } elsif ($pid > 0) { #parent } else { $child }
(Yes, I know that can be done with draw_color(qw( blue red green )[get_the_boundary_x() <=> the_user_provided_x()], but that's just ugly and confusing).if (get_the_boundary_x() < the_user_provided_x()){ draw_color("green"); elsif (get_the_boundary_x() == the_user_provided_x()){ draw_color("blue"); else { draw_color("red"); }
merlyn's looking for a good idiom: return this if this is true shows another question without a (really good, or at least completely natural) answer. The suggested if (my $ret = thatroutine()){return $ret} feels ugly due to the synthetic variable $ret.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Control Structures
by merlyn (Sage) on May 10, 2005 at 17:10 UTC |