note
eyepopslikeamosquito
<P>
<blockquote>
<I>
I started using Switch, then I read in CPAN "do not use if you can use given/when".
Then when I used given/when, I ended up with warnings plus advice not to use given/when. Hmmmmmm.
As an occasional perl user, that is a little frustrating rewriting the same section of code 3x to do exactly the same thing.
I just need something that has case type functionality.
</I>
</blockquote>
</P>
<P>
I can certainly understand your frustration.
This was a very sad affair for the Perl community.
Though adding <a href="https://www.perl.com/article/p5p-update-smarter-match/">smart match into Perl</a> was premature,
and had to be backed out, at least P5P learned a valuable lesson.
</P>
<P>
Not wanting to gloat (no, really) but this didn't affect me, at all, because I've never been a fan of <C>switch</C>.
Stronger, I've almost never used [wp://Switch statement|Switch] in over 20 years of coding in C++ and Perl and always
queried its use during code reviews.
Though it's a bit extreme to call [wp://Switch statement|Switch] a code smell, cleaner alternatives, such as lookup-tables (hashes in Perl)
and polymorphism (in OO languages, such as C++ and Java) should be preferred.
</P>
<P>
From <a href="https://www.squirrel.nl/pub/PBP_refguide-1.02.00.pdf">Perl Best Practices</a> I suggest you take a look at
the Control Structures chapter, especially:
<ul>
<li> 6.16 Value Switches - Use table look-up in preference to cascaded equality tests (item 78)
<li> 6.17 Tabular Ternaries - When producing a value, use tabular ternaries (item 79)
</ul>
</P>
<P>
Though my advice in Perl is usually <I>"just use a hash"</I>,
as a last resort you could replace your <C>switch</C> with an <C>if-elsif-elsif-else</C> construct.
</P>
<P><B>References Added Later</B></P>
<P>
<ul>
<li> [id://11134610] by [kcott] (2021)
<li> [id://11134604] by [symgryph] (2021)
<li> [id://11139003] by [mldvx4] (2021)
</ul>
</P>
<P>
<ul>
<li> [id://11153110] by [ibm1620] (2023)
<li> [id://11154677] by [ikegami] (2023)
<li> [id://11155857] by [ikegami] (2023) - the unfortunate <C>smartmatch</C> chaos resulted in the creation of policies to make new features experimental
<li> [id://11155820] by [perltux] (2023) - is <C>smartmatch</C> really being deprecated? (yes, it will be gone in <C>perl 5.42</C>)
<li> <a href="https://perldoc.perl.org/perldiag#Smartmatch-is-deprecated">Smartmatch is deprecated</a> (perldiag)
</ul>
</P>
<P>
<ul>
<li> [id://1167020] by [ksublondie] (2016) - [mod://Perl::Critic] warns that <C>Switch</C> is deprecated
</ul>
</P>
<P>
<ul>
<li> <a href="https://stackoverflow.com/questions/4417070/switch-statements-are-bad">Switch statements are bad?</a> (StackOverflow)
<li> <a href="https://softwareengineering.stackexchange.com/questions/15820/should-i-use-switch-statements-or-long-if-else-chains">Should I use switch statements or long if else chains?</a> (StackExchange)
</ul>
</P>
11136353
11136390