> On 2010-07-30, at 4:57 pm, Aaron Sherman wrote: >> given False { when True { say "True" } when False { Say "False" } default { >> say "Dairy" } } >> I don't think it's unreasonable to expect the output to be "False". >> However, it actually outputs "True". Why? Well, because it's in the spec >> that way. So... why is it in the spec that way?
Well, if you want to do a boolean test, you'd probably use "if" instead; but something that already gives you a Bool, like "when time>$limit", is likely to be the result you want to test itself rather than comparing it against $_ (which is likely not to be a Bool). So Perl is trying to be helpful by doing something useful instead of making the useful thing much harder at the expense of something that isn't useful anyway. The catch is that I think that comparing against a boolean IS useful. The fact that this question keeps coming up, even on the p6l list, seems to demonstrate that the "helpful" way isn't completely natural or obvious (at least, not to everyone). On 2010-07-31, at 1:33 am, Moritz Lenz wrote: > sub test() { True }; > given 0 { when test() { say "OH NOEZ" } } > I don't think it's unreasonable to expect the output to be "OH NOEZ". It's not unreasonable, especially if that's what you expect. But it's even more reasonable to expect this to work: given $something { when True { say "That's the truth!" } when 42 { say "Good answer!" } when "viaduct" { say "You guessed the secret word!" } } In both these examples, the intent is fairly clear from the context. It's easier to forget that Bools behave differently from other types when you only have some variable that could be any type: if $guess ~~ $answer { say "Correct!" } # hope your question wasn't T/F! Maybe we can't please everyone, but we can at least try not to displease anyone. Perl is awfully clever at keeping your eaten ponies, and there is a way we can have both the "helpful" syntax and the "consistent" one: given $who-knows-what { when True { say "It's a true thing!" } when 42 { say "It's numbery!" } whenever timeout() { say "Who cares what you say, time's up!" } whenever $override { say "Whatever, switching to automatic override" } } This way (or something similar) is just as clear when reading something in context, but also makes it clear(er) when the context doesn't help (like 'when who-knows()') or when you reasonably expect more consistent matching. [Or do I mean "whenever"??] -David