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
- 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?
- Re: Endorsement from committer to submit TPF Grant Application to add given/when to Switch.pm by TheDamian suggests it's not April Fools (we'll see at TPC, June 2024)
- The Once and Future Perl (youtube 1:31 hr) by Damian Conway, TPRC 2024, Las Vegas (1:08:25: use feature switch given/when, use v5.40, use feature 'switch', use feature 'smartmatch' but going away in perl v5.42 but his new Multi::Dispatch is better and migration from old smartmatch is straightforward, new module Switch::Back, 1:16:30, smartmatch: 46 rules down to 6 rules, use builtins qw<true false>)
- The Perl and Raku Conference - Las Vegas, NV 2024 Playlists (youtube)
On CPAN:
- Switch::Back by DCONWAY - given/when for a post-given/when Perl (Jun 27 2024)
- Switch::Right by DCONWAY - Switch and smartmatch done right this time (Jun 30 2024)
- Multi::Dispatch by DCONWAY - Multiple dispatch for Perl subs and methods (Jul 08 2024)
- Object::Pad by Paul Evans - a simple syntax for lexical field-based objects (Jul 15 2024)
- 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)
- Switch::Perlish by Dan Brook - a Perlish implementation of the switch statement
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
|
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.