in reply to Avoiding goto

Or

for my $foo (@bar) { { cond1 ? next : action1(); cond2 ? next : action2(); cond3 ? next : action3(); } action_fallthough(); }

See perldoc on next under "Note that a block by itself is semantically identical to" or perlsyn and search for "Sorry. You can always put another block inside".

Simple example:

for (1..10) { print "$_ is an "; { next if $_ % 2; print "even "; } print "integer\n"; }

Replies are listed 'Best First'.
Re^2: Avoiding goto
by ikegami (Patriarch) on Jun 11, 2010 at 00:25 UTC

    The down side is that last becomes equivalent to next (unless you use a label).

    $ perl -le'for (1..2) { { print; last; } cleanup; }' 1 2

    The up side is that it solves the following problem nicely:

    $ perl -Mstrict -e'for (@ARGV) { my $x; } continue { cleanup($x) }' Global symbol "$x" requires explicit package name at -e line 1. -e had compilation errors.