Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

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

use switch; switch ($selection) { case wxID_YES {$self->Wx::LogStatus ("You pressed: \ +"Yes\" ")} case wxID_NO {$self->Wx::LogStatus ("You pressed: \" +No\" ")} case wxID_CANCEL {$self->Wx::LogStatus ("You pressed: \" +Cancel\" ")} }

Replaced switch construct with given-when construct - fails, always takes Yes path

given ($selection) { when (wxID_YES) {$self->Wx::LogStatus ("You pressed: + \"Yes\" ")} when (wxID_NO) {$self->Wx::LogStatus ("You pressed: +\"No\" ")} when (wxID_CANCEL) {$self->Wx::LogStatus ("You pressed: +\"Cancel\" ")} }

Working given-when construct

given ($selection) { when ($_ == wxID_YES) {$self->Wx::LogStatus ("You pre +ssed: \"Yes\" ")} when ($_ == wxID_NO) {$self->Wx::LogStatus ("You pres +sed: \"No\" ")} when ($_ == wxID_CANCEL) {$self->Wx::LogStatus ("You pres +sed: \"Cancel\" ")} }

It also works if the constant is assigned to a scaler and then the scaler is used in the when clause.

my $yes = wxID_YES; when ($yes) {....}
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!


In reply to converting from switch to given-when by jmlynesjr

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others pondering the Monastery: (7)
    As of 2014-11-27 06:47 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My preferred Perl binaries come from:














      Results (180 votes), past polls