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

The Camel Book gives an example where you can increment a loop variable even when being interrupted by next (pg. 121):
LINE: while (<STDIN>) { next LINE if /^#/; # skip comments next line if /^$/; # skip blank lines ... } continue { $count++; }
So, yeah, it's not very useful, especially given the scope issue.

Elda Taluta; Sarks Sark; Ark Arks

Replies are listed 'Best First'.
Re^2: while(){}continue{}; Useful? (Consistent!)
by LanX (Saint) on Mar 03, 2010 at 23:11 UTC
    > 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

      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

      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