Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

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

G'day Deep_Plaid,

I see you've already been advised about either: specifying a version (with use) which automatically loads the feature you want; or, specifying the feature by itself. For "switch", that's one of:

use 5.010;

or

use feature 'switch';

However, as you're using 5.18.1, you'll now get warnings about experimental features unless you also switch them off like this (as explained in "perl5180delta: New mechanism for experimental features"):

no if $] >= 5.018, warnings => "experimental::feature_name";

Furthermore, that's somewhat counter-intuitive because, if you used what you might think would be the correct way to to do this, i.e.

no if $] >= 5.018, warnings => "experimental::switch";

you'll get

Unknown warnings category 'experimental::switch' at ... BEGIN failed--compilation aborted at ...

What you actually need is:

no if $] >= 5.018, warnings => "experimental::smartmatch";

Without this, you'll get a warning message for every given and when in your code, i.e. for the code you posted:

given is experimental at ... when is experimental at ... when is experimental at ... when is experimental at ...

At this point, you may have decided not to use experimental features. They're certainly not a good idea in production code. Here's some additional information on both Switch and switch: "Re: switch statement".

Finally, here's a version of your code which generates no errors or warnings (I used 5.18.1 — the same version as you're running). In addition, while I see you've been provided with many alternatives to given/when, here's two more. [Note: this is just example code that includes no argument checking.]

#!/usr/bin/env perl -l use 5.010; use strict; use warnings; no if $] >= 5.018, warnings => "experimental::smartmatch"; print '*** test1() ***'; print "$_ is ", test1($_) for 0 .. 4; print '*** test2() ***'; print "$_ is ", test2($_) for 0 .. 4; print '*** test3() ***'; print "$_ is ", test3($_) for 0 .. 4; sub test1 { my ($var) = @_; my $i; given ($var) { when (1) { $i = 'One' } when (2) { $i = 'Two' } when (3) { $i = 'Three' } default { $i = 'Other' } } return $i; } sub test2 { my ($var) = @_; return $var == 1 ? 'One' : $var == 2 ? 'Two' : $var == 3 ? 'Three' : 'Other'; } sub test3 { state $word_for = {qw{1 One 2 Two 3 Three}}; return $word_for->{+shift} || 'Other'; }

Output:

*** test1() *** 0 is Other 1 is One 2 is Two 3 is Three 4 is Other *** test2() *** 0 is Other 1 is One 2 is Two 3 is Three 4 is Other *** test3() *** 0 is Other 1 is One 2 is Two 3 is Three 4 is Other

-- Ken


In reply to Re: Given When Syntax by kcott
in thread Given When Syntax by Deep_Plaid

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 about the Monastery: (7)
    As of 2019-10-15 16:57 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?