http://qs321.pair.com?node_id=1078496


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