in reply to using the feature 'switch' in loops

In your example, the last breaks out of the inner for ($a), like it's supposed to. If you use given (which is actually the "switch" statement, not for, even though it can be used), the last does exit the outer foreach like you would expect.

given/when was introduced in Perl 5.10 (Dec 2007) and was retroactively marked experimental in Perl 5.16 (May 2012). While I personally liked given/when, I no longer use it because its implementation is now subject to change.

Replies are listed 'Best First'.
Re^2: using the feature 'switch' in loops
by martell (Hermit) on Jul 30, 2014 at 16:36 UTC

    The reason why I used 'for'instead of 'given' was because it was the first example in the documentation as mentioned in persyn, Switch statements. It says, I quote:

    ... The foreach is the non-experimental way to set a topicalizer. If you wish to use the highly experimental given , that could be written like this: ...
    .

    It was only after reading the specific page Switch that I realized that I was dealing with a masked loop statement. The perlsyn doesn't mention this behaviour or difference.

      You're correct about the documentation, at least for Perl v5.16 and up (in which the entire given/when/smartmatch feature is "experimental"), and Switch statements could mention the behaviour of last within a given for clarity.

      But last without a label always breaks out of the innermost loop, so I still don't find that behaviour surprising and your observation that "the 'last' statement in the switch statements controlled the flow within the switch statement" is not quite right, since in your examples you're not inside a "switch" (given), you're inside two for loops.

      The only argument one might make is that that behaviour is inconsistent and last should also break out of a given (but according to your original post you don't expect that behaviour).

      I don't understand what you mean about a "masked loop statement". Switch is deprecated and has nothing to do with Switch statements (given/when) except the shared name of the feature.

        After further reading I would agree that Switch Statements seems to imply that for is a "switch" statement, white actually a for used in this way is still just a for.

        P5P is always happy about doc patches :-)