in reply to improve ugly flow control
Last Update: This is wrong. See below for details.
Here's a somewhat exotic construction that I think reads well. Assumes that do_something() returns true on success.
or maybe better,log_failure() unless grep { exists($hash{$_}) && do_something($_) .. 1 } @options;
Untested, but I think it's nearly right.grep { exists($hash{$_}) && do_something($_) .. 1 } @options or log_failure();
The flipflop is true for zero or one keys and the initiating condition is not evaluated again after it is first true.
Update: Tested now, and it's not right. Trying to fix it.
Another Update: Is this a bug in perl's flipflop op? In perl-5.8.4
I'm using a variable $s there because bart++ tells me that flipflop can play games with $. if bare integers are presented to it.$ perl -Mstrict -we'my $s=1;for(qw/foo bar baz/){ print if /b/..$s }' barbaz$ $ perl -Mstrict -we'my $s=1;for(qw/foo bar baz/){ print if $s../b/ }' $
Final Update: There is no bug, except in my logic. Changing the right hand side of the flipflop to match anything (//) instead of fooling with constants, I find by printing the sequence numbers that the flipflop switches off ok, but switches on again next time around.
Silly mistake on my part.$ perl -e'for (qw/foo bar baz blah/){print int scalar(/b/..//)}' 0111$
Here is a version that works, but it still needs that pesky $success variable I was trying to get rid of.
$success must be checked first to short-circuit the rest of the processing.{ my $success; grep { ! $success && exists($hash{$_}) && do_something($_) .. ($success = 1) } @options or log_failure(); }
After Compline,
Zaxo
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: improve ugly flow control
by Aristotle (Chancellor) on Sep 19, 2004 at 08:01 UTC | |
Re^2: improve ugly flow control
by Aristotle (Chancellor) on Sep 19, 2004 at 23:08 UTC |