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}
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|