In one of the last scripts I wrote, I needed a behavior similar to a switch statement behavior. A simple search of an equivalent in Perl led me to use Switch. At the beginning, all was fine and working, until everything just crashed with errors that are not very descriptive (it happened on a switch statement that had cases with regex, but strangely it didn't happen on other switch statements that are alike).
EDIT: the code that crashed was looking like this one:
switch ($var) {
case /pattern1/ {...}
case /pattern2/ {...}
...
else {...}
}
That led me to abandon the use of Switch.pm and search for an alternative.
I found given and for-when and of course there's always the straightforward and somewhat naive if-elsif-else.
- Why is
Switch.pmso unstable? - It seems
givenandfor-whenhave a similar structure, but I guess there's a difference (because both exist). What is it? - Is
if-elsif-elsesignificantly slower than the other options?