Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

using the feature 'switch' in loops

by martell (Hermit)
on Jul 29, 2014 at 19:13 UTC ( [id://1095514]=perlquestion: print w/replies, xml ) Need Help??

martell has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

I was wondering for thoughts from you about the new 'switch' feature in combination with loop control statements like 'last' and 'next'.

When I first used the 'switch' statement, within a loop, I was suprised by the behaviour when used in combination with the last statement. Without prior thought I programmed:

use feature "switch"; foreach $a (1..2) { for ($a) { when (1) {print "1\n"; last;} when (2) {print "2\n"; last;} } }

This prints:

1 2

instead what I expected (in my case, the 'when' blocks are containing loop termination conditions I wanted to check before doing some work in the foreach block):

1

The code is easily adapted for my intention:

use feature "switch"; LOOP: foreach $a (1..2) { for ($a) { when (1) {print "1\n"; last LOOP;} when (2) {print "2\n"; last LOOP;} } }

I was wondering if this behaviour was also for other monks puzzling. I didn't expected that the 'last' statement in the switch statements controlled the flow within the switch statement. It easily solved of course and clearly mentionned in the docs. My fault for not reading them throughly.

But did you expected this behaviour when you used this feature for the first time?

Kind regards

Martell

Replies are listed 'Best First'.
Re: using the feature 'switch' in loops
by ikegami (Patriarch) on Jul 29, 2014 at 22:29 UTC

    I didn't expected that the 'last' statement in the switch statements

    There's no such thing as a switch statement. Did you mean a given statement? But you didn't use one??

    You have a pair of nested loops. last (without a label) exits the innermost loop.

Re: using the feature 'switch' in loops
by Anonymous Monk on Jul 29, 2014 at 19:37 UTC

    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.

      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.

Re: using the feature 'switch' in loops
by Anonymous Monk on Jul 29, 2014 at 19:34 UTC

    But did you expected this behaviour when you used this feature for the first time?

    Long time ago, when switch was first introduced, I decided never to bother with it :) I don't have expectations

    But if you use given/when then last controls the outer loop because given isn't a loop like foreach

    use feature "switch"; for my $i ( 'i','q' ){ given( $i ){ when( 'i' ){ print "i\n"; last; } when( 'q' ){ print "q\n"; last; } } } __END__ i
Re: using the feature 'switch' in loops
by Anonymous Monk on Jul 30, 2014 at 11:44 UTC
    Don't use "fee-churs." They're not.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095514]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having a coffee break in the Monastery: (4)
As of 2024-03-19 09:02 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found