Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why doesn't my continue execute the next when block in Perl 5.10?

When I run this:

use feature ':5.10';
$x=1;
given ($x) {
    when(1) {
        say '1';
        $x = 2;
        continue;
    }
    when (2) {
        say '2';
    }
}

This should print both 1 and 2, but it only prints 1. Am I missing something?

EDIT:

I have added $x = 2 and it still prints only "1"

like image 703
JoelFan Avatar asked Dec 02 '25 10:12

JoelFan


1 Answers

See the perlsyn man page:

given(EXPR) will assign the value of EXPR to $_ within the lexical scope of the block

This code outputs 1 and 2:

use feature ':5.10';
$x=1;
given ($x) {
    when(1) {
        say '1';
        $_ = 2;
        continue;
    }
    when (2) {
        say '2';
    }
}
like image 65
mikegrb Avatar answered Dec 05 '25 16:12

mikegrb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!