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.