Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

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":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

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

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (5)
    As of 2020-02-19 06:37 GMT
    Find Nodes?
      Voting Booth?
      What numbers are you going to focus on primarily in 2020?

      Results (80 votes). Check out past polls.