in reply to Re: while(){}continue{}; Useful?
in thread while(){}continue{}; Useful?

Hm. Seems to me that

my $n = 10; while( $n ){ print $n--; next if $n%2; ## do other stuff here } continue{ sleep 1; }

Is exactly equivalent to:

my $n = 10; while( $n ){ print $n--; unless( $n%2 ) { ## do other stuff here } sleep 1; }

And far clearer. (Even if you prefer the if( not $n%2 ) form.)

There has to be a good use for it, I'm just not seeing it.


Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"I'd rather go naked than blow up my ass"

Replies are listed 'Best First'.
Re^3: while(){}continue{}; Useful?
by kennethk (Abbot) on Mar 03, 2010 at 18:07 UTC
    I think the concept is handling complex branching with early return in the main body and exporting operations required every time. How about something more along the lines of:
    #!/usr/bin/perl use strict; use warnings; my @array = qw(1 2 cat dog 1.3 7.2); for (@array) { if (/\./) { $_*=2; next; } if (/\d/) { $_*=4; next; } } continue { print; }

    Of course TIMTOWTDI and I'm trying to come up with a contrived example.

      I'm trying to come up with a contrived example.

      Yeah! I spent a while trying to contrive something useful too. But not only did I not find anything that couldn't be done without it. I didn't find anything that I could say was even slightly more preferable done with it.

      I really had forgotten it existed until it showed up the POD of IO::Socket::Multicast.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        I have now used this structure, and I'm curious what your take on the approach is - either if it changes your opinion of the structure or if you have a more aesthetically pleasing alternative. I'm writing what is essentially a preprocessor for some code autotranslation, and so I have a volatile content string that I process through a series of if (){next} blocks. I then check for the error condition in the continue block, which prevents the possibility of infinite loops and cleans up error handling. I've just started the project, but expect the number of conditional clauses to grow significantly and this strikes me as the most extensible framework.

        #!/usr/bin/perl use strict; use warnings; local $/; # slurp my $content = <DATA>; my $error; # Preprocess while (local ($_) = $content =~ /^#include\s(.*?)$/m) { if (/<math/) { # Math library $content =~ s/#include\s$_/import java.lang.Math;/; next; } if (/"stdafx.h"/) { # Autogen MS IDE header for project/system inc +ludes $content =~ s/#include\s$_//; next; } if (/"/) { # A yet unconsidered header file (my $filename) = /"(.*)"/; open my $inc_handle, '<', $filename or warn "File open fail $f +ilename: $!\n" and next; local $/; # slurp my $include = <$inc_handle>; $content =~ s/#include\s$_/$include/; next; } } continue { if ($content =~ s/#include\s$_//) { warn "Unhandled include $_\n"; $error++; } } die "$error errors encountered" if $error; __DATA__ #include "stdafx.h" #include <math.h> #include "AlyLee.h" #include "Common.h"
        Note that while lexical variables are defined at the block level (as AnomalousMonk pointed out), the localization in the while conditional holds through the continue block.