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

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.

Replies are listed 'Best First'.
Re^4: while(){}continue{}; Useful?
by BrowserUk (Patriarch) on Mar 03, 2010 at 20:38 UTC
    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.

        That seems a very clumsy way of parsing to me. You're having to re-parse the same information multiple times. And the number of times will only grow as you add more cases.

        A given/when or if/elsif/.../else cascade seems far more appropriate:

        #!/usr/bin/perl use strict; use warnings; my $content; my $error; while( <DATA> ) { unless( s[^#include (.+)$][] ) { chomp; warn "Non-include line '$_' untouched\n"; $content .= $_ . "\n"; } else{ local $_ = $1; if( m[<math] ) { $content .= qq[import java.lang.Math;\n]; } elsif( m["stdafx.h"] ) { $content .= qq[#include "stdafx"\n]; } elsif( m["(.+)"] ) { open my $inc_handle, '<', $1 or warn "$1: $^E\n" and ++$error and next; local $/; # slurp $content .= <$inc_handle> . "\n"; } else { warn "Unhandled include $_\n"; $error++; } } } print "\nContent:\n'$content'\n"; die "$error errors encountered\n" if $error; __DATA__ #include "stdafx.h" #include <math.h> #include <stdio.h> // A comment #include "AlyLee.h" #include "Common.h"

        Produces:

        C:\test>junk49 Unhandled include <stdio.h> Non-include line '// A comment' untouched AlyLee.h: The system cannot find the file specified Common.h: The system cannot find the file specified Content: '#include "stdafx" import java.lang.Math; // A comment ' 3 errors encountered

        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.