People coming from other languages often ask how to use the switch case statements in Perl 5. In a nutshell, you always use if, elsif, and else. The longer answer is as follows:

Switch Case

Perl 5, the language itself does not have any statement similar to the switch and case statement of some other languages. (Neither does Python by the way.)

There is, or rather used to be, a standard module called Switch that could provide similar functionality, but reportedly it was buggy and it was removed from the core. That means recent version of Perl won't have it. If you read its documentation you'll see it recommends given/when.

Given When

In version 5.10 the keywords given and when were introduced trying to provide functionality similar to what case and switch are expected to do, but the design was copied from the design of Perl 6.

Unfortunately due to some misunderstandings the implementation had various issues and thus in a later version this functionality was marked as experimental which means if you use it, perl will give you warnings such as given is experimental and when is experimental. It also means its behaviour might change in the future or that it will be removed.

You can avoid those warnings if you add no warnings 'experimental'; to your code.

With that said, as far I as I know, the problems of the given/when statement are actually rooted in the ~~ operator which was called Smart Match, but which turned out to be not so smart. If you don't rely on the "magic" in that operator then you will be ok. So for example the following script should work without any issues:

examples/given_when.pl



use strict; use warnings; use 5.010; no warnings 'experimental'; my $num = rand(); given ($num) { when ($_ > 0.7) { say "$_ is larger than 0.7"; } when ($_ > 0.4) { say "$_ is larger than 0.4"; } default { say "$_ is something else"; } }

if elsif else

Probably the safest solution however is to go back to good old if, elsif, else statement as in the following example:

examples/if_elsif_else.pl



use strict; use warnings; use 5.010; my $num = rand(); if ($num > 0.7) { say "$num is larger than 0.7"; } elsif ($num > 0.4) { say "$num is larger than 0.4"; } else { say "$num is something else"; }