On 30 October 2012 13:27, Damian Conway <span dir="ltr"><<a href="mailto:damian@conway.org" target="_blank">damian@conway.org</a>></span> wrote:<br><div class="gmail_quote"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">

Mathew Robertson asked:<br>
<div class="im"><br>
> What is the expected output of this:<br>
</div><div class="im">> my $x = 0;<br>
> given ($x) {<br>
>   when (true) { print "true".$/; }<br>
>   when (false) { print "false".$/; }<br>
>   default { print "unknown".$/; }<br>
> }<br>
<br>
</div>Expected outcome is: "true"<br>
<br>
As explained in perlsyn:<br>
<br>
      Most of the time, "when(EXPR)" is treated as an implicit smart match of<br>
       $_, i.e. "$_ ~~ EXPR". (See "Smart matching in detail" for more<br>
       information on smart matching.) But when EXPR is one of the below<br>
       exceptional cases, it is used directly as a boolean:<br>
<br>
       o   a subroutine or method call<br>
<br>
Yes, it's arguably broken and/or stupid. But it's as documented.<br>
<br>
Workaround:<br>
<br>
    my $TRUE  = 1;<br>
    my $FALSE = 0;<br>
<div class="im">    my $x = 0;<br>
    given ($x) {<br>
</div>        when ($TRUE) { print "true".$/; }<br>
        when ($FALSE) { print "false".$/; }<br>
        default { print "unknown".$/; }<br>
    }<br>
<span class="HOEnZb"><font color="#888888"><br>
Damian<br>
</font></span></blockquote></div><br><div>I found that passage in perlsyn, but it didn't explain the behaviour, aka: EXPR is an expression that is used as a boolean, so I assumed that it referred to the returned value.. cf: undef is boolean-false inside "if (...)" context and "0E0" is boolean-true inside "if (...)".</div>

<div><br></div><div>And thus is why I posted the alternative using "M" and "N"... which I expected to be boolean-true inside the conditional.</div><div><br></div><div>Indeed it is was documented, but I didn't understand the *magic*... :)</div>

<div><br></div><div>cheers,</div><div>Mathew</div>