Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Unexpected behavior of continue in 5.10

by benizi (Hermit)
on Jan 19, 2008 at 18:03 UTC ( #663230=perlmeditation: print w/replies, xml ) Need Help??

I realize 5.10's switch statement is quite different from C/etc.'s. But, I've been doing most of my programming lately in Java (uggh) and C# (double uggh). So, I accidentally used 'continue' instead of 'next' in some Perl code, and man, it took a while for me to see what was going on. The short of it is that using 'continue' in a subroutine can affect a given/when that surrounds it. The following demonstrates a (much-simplified) version, with my level of surprise appropriately expressed. Just a reminder to everyone: 'continue' has nothing to do with 'continue' blocks.

#!/usr/bin/perl use strict; use warnings; use feature ':5.10'; sub no_saving_continue { for (shift) { say " continue[$_]" and continue if /a/; say " loop[$_]"; } } sub has_saving_continue { for (shift) { say " continue[$_]" and continue if /a/; } continue { say " loop[$_] (in continue)"; } } for ( [ 0, no_save => \&no_saving_continue ], [ 1, protect => \&has_saving_continue ], ) { my ($save, $label, $func) = @$_; for (qw{a b}) { say "\n==== forloop[$_] ($label) ===="; given ($_) { when (['a','b']) { say " {explicit $_"; $func->($_); say " explicit $_}"; } default { say " {default $_}", map " SURPRISE", grep $_, $save, +/a/; } } } } __END__ Output: ==== forloop[a] (no_save) ==== {explicit a continue[a] {default a} SURPRISE ==== forloop[b] (no_save) ==== {explicit b loop[b] explicit b} ==== forloop[a] (protect) ==== {explicit a continue[a] {default a} SURPRISE SURPRISE ==== forloop[b] (protect) ==== {explicit b loop[b] (in continue) explicit b}

Replies are listed 'Best First'.
Re: Unexpected behavior of continue in 5.10
by ysth (Canon) on Jan 20, 2008 at 02:30 UTC
    I would have expected a "Exiting subroutine via continue" warning. I hope that one is added soon.

    In the case where you have a loop inside a when block, I can see this still being a trap for the multilingual, though.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlmeditation [id://663230]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2022-08-17 18:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?