Polynomial has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Odd behavior of given {when (... or ...) ...}
by ikegami (Patriarch) on Jun 22, 2008 at 15:41 UTC | |
by ysth (Canon) on Jun 22, 2008 at 20:51 UTC | |
|
Re: Odd behavior of given {when (... or ...) ...}
by massa (Hermit) on Jun 23, 2008 at 12:50 UTC | |
by Polynomial (Acolyte) on Jun 25, 2008 at 11:08 UTC | |
|
Re: Odd behavior of given {when (... or ...) ...}
by Anonymous Monk on Jun 23, 2008 at 08:43 UTC | |
by Polynomial (Acolyte) on Jun 23, 2008 at 12:03 UTC |