Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

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

From the Perl documentation ( : 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 ( or, if worse come to worse, to the series of if/elsif.

In reply to Re: Given When Syntax by Laurent_R
in thread Given When Syntax by Deep_Plaid

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?

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

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (7)
As of 2022-08-11 12:44 GMT
Find Nodes?
    Voting Booth?

    No recent polls found