Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Given When Syntax

by kcott (Bishop)
on Mar 16, 2014 at 00:19 UTC ( #1078496=note: print w/replies, xml ) Need Help??


in reply to Given When Syntax

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

Replies are listed 'Best First'.
Re^2: Given When Syntax
by Deep_Plaid (Acolyte) on Mar 16, 2014 at 16:23 UTC

    Hi Ken. Thanks for taking the time for this detailed post . I was so caught up in things yesterday that I missed it and didn't respond sooner. It was very helpful and I learned a great deal - the more examples the better. Cheers, DP.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1078496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (6)
As of 2019-03-24 05:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    How do you Carpe diem?





    Results (114 votes). Check out past polls.

    Notices?