<?xml version="1.0" encoding="windows-1252"?>
<node id="995476" title="converting from switch to given-when" created="2012-09-24 21:29:10" updated="2012-09-24 21:29:10">
<type id="115">
perlquestion</type>
<author id="982107">
jmlynesjr</author>
<data>
<field name="doctext">
&lt;p&gt;Monks:&lt;/p&gt;
&lt;p&gt;In cleaning up my More wxPerl Examples code in preparation to moving it to github, I came across several examples that use the switch construct. I want to update these to use the given-when construct. As seen below, I got a version to work, but there has to be a simpler/cleaner syntax. It seems to be something to do with (de)referencing the constant.&lt;/p&gt;

&lt;p&gt;Thanks in advance, James&lt;/p&gt;

&lt;p&gt;wxID_YES, wxID_NO, and wxID_CANCEL are wxPerl constants from &lt;code&gt;use Wx qw(:everything)&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Original working construct&lt;/p&gt;
&lt;code&gt;use switch;
switch ($selection) {
			case wxID_YES	  {$self-&gt;Wx::LogStatus ("You pressed:  \"Yes\" ")}
			case wxID_NO	  {$self-&gt;Wx::LogStatus ("You pressed:  \"No\" ")}
			case wxID_CANCEL  {$self-&gt;Wx::LogStatus ("You pressed:  \"Cancel\" ")}			
	}&lt;/code&gt;

&lt;p&gt;Replaced switch construct with given-when construct - fails, always takes Yes path&lt;/p&gt;

&lt;code&gt;given ($selection) {
			when (wxID_YES)	  {$self-&gt;Wx::LogStatus ("You pressed:  \"Yes\" ")}
			when (wxID_NO)	  {$self-&gt;Wx::LogStatus ("You pressed:  \"No\" ")}
			when (wxID_CANCEL)  {$self-&gt;Wx::LogStatus ("You pressed:  \"Cancel\" ")}			
	}&lt;/code&gt;

&lt;p&gt;Working given-when construct&lt;/p&gt;

&lt;code&gt;given ($selection) {
			when ($_ == wxID_YES)	  {$self-&gt;Wx::LogStatus ("You pressed:  \"Yes\" ")}
			when ($_ == wxID_NO)	  {$self-&gt;Wx::LogStatus ("You pressed:  \"No\" ")}
			when ($_ == wxID_CANCEL)  {$self-&gt;Wx::LogStatus ("You pressed:  \"Cancel\" ")}			
	}&lt;/code&gt;

&lt;p&gt;It also works if the constant is assigned to a scaler and then the scaler is used in the when clause.&lt;/p&gt;

&lt;code&gt;my $yes = wxID_YES;
when ($yes)   {....}&lt;/code&gt;

&lt;b&gt;Update1:&lt;/b&gt;

&lt;p&gt;Thanks to [Athanasius] for the great explanation and test code. I had seen the constant as subroutine construct, but didn't know how it worked in this case(hidden smart match). Thanks also to [tobyink]. The &lt;code&gt;when ([wxID_YES]) {....}&lt;/code&gt; also works (why?) and seems cleaner than what I had come up with. I will go with the bracket construct. Thanks also to Anonymous Monk for the reference to the bug report.&lt;/p&gt;

&lt;b&gt;Update2:&lt;/b&gt;

&lt;p&gt;Thanks again to [Athanasius] and [tobyink]. The "why?" is now answered very clearly. I had tried &lt;code&gt;when ((wxID_YES))&lt;/code&gt; and &lt;code&gt;when ({wxID_YES})&lt;/code&gt;. I guess one more try and I wouldn't have asked the question and I still wouldn't have known why it worked! There are a lot of good teachers out there willing to share their time and knowledge. Thanks!&lt;/p&gt;</field>
</data>
</node>
