Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Given When Syntax

by Laurent_R (Canon)
on Mar 15, 2014 at 16:39 UTC ( #1078466=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2019-09-20 18:05 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    The room is dark, and your next move is ...












    Results (267 votes). Check out past polls.

    Notices?