Problems? Is your data what you think it is? | |
PerlMonks |
converting from switch to given-whenby jmlynesjr (Deacon) |
on Sep 25, 2012 at 01:29 UTC ( [id://995476]=perlquestion: print w/replies, xml ) | Need Help?? |
jmlynesjr has asked for the wisdom of the Perl Monks concerning the following question: Monks: 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. Thanks in advance, James wxID_YES, wxID_NO, and wxID_CANCEL are wxPerl constants from use Wx qw(:everything). Original working construct
Replaced switch construct with given-when construct - fails, always takes Yes path
Working given-when construct
It also works if the constant is assigned to a scaler and then the scaler is used in the when clause. Update1: 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 when ([wxID_YES]) {....} 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. Update2:Thanks again to Athanasius and tobyink. The "why?" is now answered very clearly. I had tried when ((wxID_YES)) and when ({wxID_YES}). 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!
Back to
Seekers of Perl Wisdom
|
|