http://www.perlmonks.org?node_id=1078466


in reply to Given When Syntax

The switch (given/when) feature is an experimental feature that should probably be avoided.

From the Perl documentation ( http://perldoc.perl.org/perlsyn.html#Experimental-Details-on-given-and-when) : As previously mentioned, the "switch" feature is considered highly experimental; it is subject to change with little notice. In particular, when has tricky behaviours that are expected to change to become less tricky in the future. Do not rely upon its current (mis)implementation. Before Perl 5.18, given also had tricky behaviours that you should still beware of if your code must run on older versions of Perl.

It is probably not wise to use this feature for anything else than a one-off program that you know will never have to be used again in the future, as your syntax might break in the next Perl version.

Using an array is probably the simplest alternative for your very simple case:

my @array = qw /One Two Three/; my $var = 2; print $array[$var], "\n";
If your actual case is more complicated, then a hash might be the solution:
my %hash = ( 1 => One, 2 => Two, 3 => Three); my $var = 2; print "$hash{$var}\n";
For more complicated things, such as taking actions on the basis of the value of a variable, you might want to use a dispatch table (essentially a hash in which the values are references to subs). For example, the following is using a long list of if /elsif expressions to determine what series of actions to do depending on some parameters received as arguments:
my ($oper, $rest) = shift, join ' ', @_; my $stop = 'stop'; my $force_stop = 'force_stop'; # ... if ($oper eq $stop) { run_stop ($rest); cleanup(); } elsif ($oper eq $force_stop) { run_force_stop ($rest); cleanup(); } elsif ($oper eq 'start') { run_start ($rest); } elsif ($oper eq 'force_start') { run_stop ($rest); cleanup(); run_start ($rest); } ... else { die "Unknown operation: $oper \n"; }
You could replace this with the following dispatch table:
my %dispatch_table = ( stop => sub {run_stop($rest); cleanup();}, force_stop => sub {run_force_stop($rest); cleanup();}, start => /&run_start(@rest), force_start => sub {run_stop ($rest); cleanup() ; run_start ($res +t);}, );
and use it as follows:
my ($oper, $rest) = shift, join ' ', @_; if (defined $dispatch_table{$oper}) { $dispatch_table{$oper}->($rest); } else { die "Unknown operation: $oper \n"; }
If even that is not OK, then you can also go for either the for/if constructs referred to in the link provided by LanX above (http://www.perlmonks.org/?node_id=826710) or, if worse come to worse, to the series of if/elsif.

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

    Hi Laurent. These are all excellent examples - I think in my case I can get away with the array construct you demonstrated, but I will experiment to see if the hash is more appropriate. I also appreciate your advice on dealing with new features like this one - very good to know. Thanks so much for taking the time - this is great stuff! Cheers, DP.