in reply to given-when last time through loop?

Every time through the loop the last say statement executes--except the last time through the loop. Why?

Your explicit call to continue inhibits the implicit call to break or next at the end of the when-block.

Also, "Learning Perl 5th" says there is an implicit break at the end of each when block, but I get errors if I do this:

It seems that if you use for instead of given as the outer control structure, it defaults to next instead of break.

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^2: given-when last time through loop?
by 7stud (Deacon) on Nov 19, 2009 at 14:10 UTC

    Hi,

    Thanks for the response.

    It seems that if you use for instead of given as the outer control structure, it defaults to next instead of break.

    Ok. I did read this in the perlsyn docs:

    On exit from the when block, there is an implicit next.

    ...but I found the 'on exit' language confusing. I thought they meant after the last when block. There should be a better example in the docs--that is a major change from using given-when on its own outside a loop.

    The docs could simply say, "Inside a for-loop, the when blocks end with an implicit next rather than an implicit break.

      I've now submitted this patch to the perl 5 porters, in the hope that it's a bit clearer:
      diff --git a/pod/perlsyn.pod b/pod/perlsyn.pod index d5fc4a7..4e1bc0a 100644 --- a/pod/perlsyn.pod +++ b/pod/perlsyn.pod @@ -679,7 +679,7 @@ string occurs in an array: } print "\@array contains $count copies of 'foo'\n"; -On exit from the C<when> block, there is an implicit C<next>. +At the end of all C<when> blocks, there is an implicit C<next>. You can override that with an explicit C<last> if you're only interested in the first match.

      So far I've found the porters very cooperative when it comes to doc patches, and I want to encourage everybody to submit patches when they find something that can be improved in the docs.

      Perl 6 - links to (nearly) everything that is Perl 6.
        That's wrong. It should be "At the end of every C<when> block"

        Forgive me for criticizing, but I don't think that highlights the change that occurs. Now the docs will have two contradictory statements: one statement will say there are implicit breaks, and this new statement will say there are implicit nexts. I think some readers may get confused and believe that one of them is a typo. I think, something like the following would highlight that a change occurs from implict breaks to implicit nexts:

        Inside a for-loop, a when block ends with an implicit next rather than an implicit break.