in reply to Re: Yet another config file editing programme : Tell me how to make it better !
in thread Yet another config file editing programme : Tell me how to make it better !
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 can certainly understand your frustration. This was a very sad affair for the Perl community. Though adding smart match into Perl was premature, and had to be backed out, at least P5P learned a valuable lesson.
Not wanting to gloat (no, really) but this didn't affect me, at all, because I've never been a fan of switch. Stronger, I've almost never used 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 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.
From Perl Best Practices I suggest you take a look at the Control Structures chapter, especially:
- 6.16 Value Switches - Use table look-up in preference to cascaded equality tests (item 78)
- 6.17 Tabular Ternaries - When producing a value, use tabular ternaries (item 79)
Though my advice in Perl is usually "just use a hash", as a last resort you could replace your switch with an if-elsif-elsif-else construct.
References Added Later
- Re: Modern exposition of "case" or Switch in perl 5.34? by kcott (2021)
- Modern exposition of "case" or Switch in perl 5.34? by symgryph (2021)
- Hash versus chain of elsifs by mldvx4 (2021)
- 'switch' and 'smartmatch' features deprecated in 5.38 by ibm1620 (2023)
- Perl named operators and builtin subs introduced since 5.6 by ikegami (2023)
- Re: using smartmatch for range matching by ikegami (2023) - the unfortunate smartmatch chaos resulted in the creation of policies to make new features experimental
- using smartmatch for range matching by perltux (2023) - is smartmatch really being deprecated? (yes, it will be gone in perl 5.42)
- Smartmatch is deprecated (perldiag)
- Switch depreciated? by ksublondie (2016) - Perl::Critic warns that Switch is deprecated
- Switch statements are bad? (StackOverflow)
- Should I use switch statements or long if else chains? (StackExchange)
- Endorsement from committer to submit TPF Grant Application to add given/when to Switch.pm by ait (Apr 1 2024) - oops, hippo notes it's already there ... April Fools?
On CPAN:
- Switch by Alexandr Ciornii (chorny?) - a switch statement for Perl, do not use if you can use given/when
- Switch::Plain by Lukas Mai - a simple switch statement for Perl (not a source filter, uses pluggable keywords)
- Syntax::Keyword::Match by Paul Evans - a match/case syntax for perl
- XS::Parse::Keyword by Paul Evans - XS functions to assist in parsing keyword syntax
|
---|