in reply to use of 'continue' block for while/until

jeepj,
I believe I have only used it once. Here is some code that does not use a continue block:
my $counter = 0; while ($counter < $limit) { my $item = shift @work_queue; my $thingy = some_function($item); if ($thingy ne 'PARTY') { ++$counter; next; } # more code if $thingy ne 'PARTY' ++$counter; # thanks ikegami }

Here is the same code with the continue block

my $counter = 0; while ($counter < $limit) { my $item = shift @work_queue; my $thingy = some_function($item); next if $thingy ne 'PARTY'; # more code if $thingy ne 'PARTY' } continue { ++$counter; }

Here is my general rule of thumb for when to use continue

It is a nice way of code re-use. Instead of doing

if ($condition1) { # statement 1 # statement 2 # statement 3 next; } # more code if ($condition2) { # statement 1 # statement 2 # statement 3 next; }
You can just do
next if $condition1; # more code next if $condition2;

Cheers - L~R

Replies are listed 'Best First'.
Re^2: use of 'continue' block for while/until
by ikegami (Patriarch) on Jun 11, 2008 at 14:01 UTC

    Here is the same code with the continue block

    No. Those two snippets are different. The first only does ++$counter when $thingy ne 'PARTY'. The second does ++$counter unconditionally.

    my $limit = 3; my @work_queue = qw( a PARTY b c d); sub some_function { $_[0] } ... print(scalar(@work_queue), "\n");

    Prints 1 for the first snippet and 2 for the second.

      ikegami,
      You are quite correct. The problem was in my code, not my intention. The first should have had a ++$counter at the bottom of the loop. I will update that now.

      Cheers - L~R