Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

switch statement

by fionbarr (Friar)
on Oct 01, 2015 at 13:18 UTC ( [id://1143545]=perlquestion: print w/replies, xml ) Need Help??

fionbarr has asked for the wisdom of the Perl Monks concerning the following question:

when I try to install 'Switch' in ActiveState PPM it says 'do not use if you can use given/when'. When I use 'given/when' I get the message 'given is experimental' and the program will not compile. I do not want a pile of 'if' statements if possible. What to do?

Replies are listed 'Best First'.
Re: switch statement
by Corion (Patriarch) on Oct 01, 2015 at 13:29 UTC

    Use a dispatch table.

    There is plenty amount of documentation on how using Switch breaks your program in surprising ways on this site.

Re: switch statement
by Athanasius (Archbishop) on Oct 01, 2015 at 14:41 UTC

    Hello fionbarr,

    When I use 'given/when' I get the message 'given is experimental' and the program will not compile.

    Are you using a very old version of Perl? According to perlexperiment, the smartmatch feature was introduced in Perl 5.10.0, so unless your version is older than that, the program should compile OK — but emit warnings — as long as you remember to use feature 'switch':

    0:33 >perl -we "use feature 'switch'; my $c = 42; given ($c) { when ( +$_ < 50) { print 'Yes!' } }" given is experimental at -e line 1. when is experimental at -e line 1. Yes! 0:33 >

    The warnings can be turned off with this pragma:

    no warnings 'experimental::smartmatch';

    But if you’re going to use when, you should still replace the topicaliser given with its more prosaic alternative for, as explained by brian_d_foy in his article “Use for() instead of given().”

    However, even the use of when is not recommended for production code. For other switch options, begin with How-do-I-create-a-switch-or-case-statement of perlfaq7. And if a dispatch table doesn’t meet your particular needs, what’s wrong with if/elsif/.../else, anyway? :-)

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      I never saw that usage of 'no warnings'. Thanks to all who answered
Re: switch statement
by BillKSmith (Monsignor) on Oct 01, 2015 at 16:52 UTC
    In the case (no pun) that your intention is compute a single value, you can use the ternary operator:
    use strict; use warnings; my $score = 77; my $grade = $score >= 90 ? 'A' : $score >= 80 ? 'B' : $score >= 70 ? 'C' : $score >= 60 ? 'D' : 'F' ; print "$grade\n";
    Bill
Re: switch statement
by sundialsvc4 (Abbot) on Oct 01, 2015 at 15:35 UTC

    Dunno why the Perl language did not include some form of switch in any of its incarnations, but I also do not care for the various graft-on implementations that try to provide it.   Corion’ suggestion to use a dispatch table ... which is simply an array or hash of subrefs ... is a good one.   Otherwise, frankly, I would just knuckle down and make peace with if...elsif..elsif....   If you are careful to spin-off the various cases into separate subs, for clarity, it actually is about as sweet as the (syntactic ...) sugar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1143545]
Front-paged by Arunbear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2024-04-23 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found