You can't rely on readline returning EOF or errors more than once. Fix:
OUTER: { for (;;) { defined( my $_ = <$fh> ) or last OUTER; if (/alpha/) { f(); last; } else { g(); } } while (<$fh>) { g(); } }
Update: The remainder of this post is wrong.
You can get rid of the duplicate g() by making the loops bottom tested.
OUTER: { defined( my $_ = <$fh> ) or last OUTER; for (;;) { if (/alpha/) { f(); last; } defined( $_ = <$fh> ) or last OUTER; } for (;;) { g(); defined( $_ = <$fh> ) or last OUTER; } }
You can squish it down a bit:
LOOP: { defined( my $_ = <$fh> ) or last; if (/alpha/) { f(); redo; } for (;;) { g(); defined( $_ = <$fh> ) or last; } }
Look at that, g() is not even duplicated anymore!!
In reply to Re^2: A way to avoid repeated conditional loops
by ikegami
in thread A way to avoid repeated conditional loops
by Deus Ex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |