coreolyn has asked for the wisdom of the Perl Monks concerning the following question:

I'm going through some finer details of Perl and I came across the availability of an optional continue block available with use of a while conditional structure.

The examples I've found for its use go like this:

From Programming Perl 3rd ed.

while (my $line = <STDIN>) { $line = lc + $line; } continue { print $line; #still visible } # $line now out of scope here

From Elements of Programming with Perl

my $count = 0; while ( $count < 10 ) { print "$count\n"; } continue { $count++; }

The first example is nice for showing that the scope extends over the entire structure, and the second is nice for showing how to turn a while into an awkward looking for loop. I should point out that all references to the continue block explicitly say that it is a seldom used construct.

My curiosity is two-fold:

    In light of previous discussions showing that for loops are not as quick as while loops (sorry couldn't find the darn disscussion/benchmarks); Would utilization of continue'd blocked while loops be a more efficient substitue for for constructs?
    My other question deals with just plain curiosity of how/why/if others have situations where they find the continue block particularly convenient.

coreolyn

Replies are listed 'Best First'.
Re: while 's 'continue' block
by merlyn (Sage) on Jan 04, 2001 at 21:33 UTC
    I use continue on a naked block when the code to get to the "next" item needs to be easily accessible:
    { blah blah; blah blah; last if $some_exit_condition; blah blah; blah blah; next if $some_early_next_condition; blah blah; blah blah; last; # or redo, if near-infinite loop } continue { blah blah; blah blah; redo; }
    So here, the continue block contains the "get to the next item" code in a clean location. Leaving out that last "redo" means I could use it as a "finally" clause... no matter what exit I take via "next" inside the block, it's gonna run that code.

    Sorry for the abstract code here. A real example is still gelling in my head. If pressed, I show it.

    -- Randal L. Schwartz, Perl hacker

      No need I see what your saying. This could clean up many little messy whiles that I construct. Thanks!

      coreolyn

      This isn't a USE but it is pretty cool (though it is semi-obfuscated):
      #!/usr/bin/perl -w use strict; my @a = ("\nr,kcelrPeH an AuJsthotre "); { my $a = pop @a; if ( 1 < length $a ) { push @a, substr($a,0,length($a)/2), scalar(reverse substr($a,length($a)/2)); next; } print $a; } continue { redo if @a; }

      I've occasionally used continue and the naked block trick to convert recursion into a stack queue sorta like above.

      --
      $you = new YOU;
      honk() if $you->love(perl)

(tye)Re: while 's 'continue' block
by tye (Sage) on Jan 04, 2001 at 21:54 UTC

    In C I've been know to stuff way too much code in the condition expression [the part bewteen ( and )] of a while. A continue in Perl can often allow much of that code to be moved to a better place.

    But I still don't use it much nor see it used much.

    I think it was implemented to make for work and just wasn't hidden from us after it was implemented. (:

            - tye (but my friends call me "Tye")
Re: while 's 'continue' block
by turnstep (Parson) on Jan 04, 2001 at 21:28 UTC

    The only time I really use "continue" is when I am writing obfuscated code. Even then I dislike it, as it has 8 letters in a row that actually mean something, which I try to avoid in my obfuscation :).

    I see few uses for continue: things can always be written more clearly (IMO) with the appropriate while, for, or "naked" block.

Re: while 's 'continue' block
by I0 (Priest) on Jan 04, 2001 at 21:38 UTC
    continue blocks can be used to express a for loop in terms of a while loop
    it can also be used to express the -p command line switch

      I'm falling a little short of understanding how you're using the term 'express', but the -p switch is very cool!

      coreolyn
        quoth the 1st Camel, on pg. 96, and the 2nd Camel, on pg. 99: <it>
        for ($I = 1; $i < 10; $i++) { ... }
        is the same as
        $i = 1; while( $i < 10) { ... } continue { $i++; }
        Personally, I like the symmetry it gives the language.
        (if only it had an elswhile clause to complete the symmetry...)
Re: while 's 'continue' block
by Hot Pastrami (Monk) on Jan 04, 2001 at 21:35 UTC
    One use I could see for a continue block would if you had a particularly long block in the while() loop, AND you needed more than one statement executed every time, such as multiple counters. In such as instance the while/continue may be more readable than the alternatives, but it's a toss-up.

    Hot Pastrami