perlsyn says that in the when clause of a given block, or should cause "the test" to be applied recursively to the first argument. What happens to the second, then? Shouldn't the implicit smart-matching magic apply to both operands, as with and? I ran into this strangeness when I used something like given ($x) {when ('a' or 'b') {foo();}}, thinking it would work like foo() if $x ~~ 'a' or $x ~~ 'b'. Instead, it appears to be mysteriously inconsistent. The following code:
sub test {my $x = shift; given ($x) {when ('a' or 'b') {say 'Y';} default {say 'N';}} given ($x) {when ('a' or $_ ~~ 'b') {say 'Y';} default {say 'N';}} given ($x) {when ($_ ~~ 'a' or 'b') {say 'Y';} default {say 'N';}}} test('a'); test('b');
prints:
Y Y Y N N Y
Can anyone explain this? I'm using a perl version 5.10.0 that I built for i686-linux-thread-multi-ld, if it makes a difference.
In reply to Odd behavior of given {when (... or ...) ...} by Polynomial
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |