Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2022-08-10 11:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?