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

> So, yeah, it's not very useful, especially given the scope issue.

Why? You can still declare a variable in the head-clause!

$\="\n"; while (my $x= < 1 2 3 4 >) { # using glob as iterator next if $x%2; print "$x: chacha" } continue { print "$x: rumba" } print $x;

output

1: rumba 2: chacha 2: rumba 3: rumba 4: chacha 4: rumba Use of uninitialized value $x in print at /home/lanx/tmp/continue.pl l +ine 8.

it's treating blocks consistently, which is a big plus in Perl! 8)

Cheers Rolf

Replies are listed 'Best First'.
Re^3: while(){}continue{}; Useful? (Consistent!)
by BrowserUk (Patriarch) on Mar 03, 2010 at 23:41 UTC

    This produces identical results, and IMO is far clearer and more intuative:

    $\="\n"; while (my $x= < 1 2 3 4 >) { # using glob as iterator unless( $x%2 ) { print "$x: chacha" } print "$x: rumba" } print $x;

    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.
      > This produces identical results, and IMO is far clearer and more intuative:

      Not with multiple nexts and code in between. Your more intuitive approach will result in nesting unless blocks for each next, instead of (mostly) linear code.

      Cheers Rolf

        No nesting required?

        LINE: while ( <STDIN> ) { unless( /^#/ # its a comment or /^$/ # or blank line ) { ... ## do whatever } $count++; ## and count them all }

        Update: Darn! Wrong place!


        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.
Re^3: while(){}continue{}; Useful? (Consistent!)
by Argel (Prior) on Mar 04, 2010 at 21:28 UTC
    Yeah, the Camel Book also uses while and continue to show how a for loop works. But still, its use seems rather limited, which is probably why it isn't used that often (the Camel Book even acknowledges that it rarely sees use).

    Elda Taluta; Sarks Sark; Ark Arks