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}

In reply to Unexpected behavior of continue in 5.10 by benizi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.