Acme-CPANModules-SmartMatch
view release on metacpan or search on metacpan
In the end almost everyone agrees that smart matching is a bad fit for a
weakly typed language like Perl. The programmer needs to be explicit on
what type of operation should be done by specifying the appropriate
/operator/ (e.g. "==" vs "eq") instead of the operator deducing what
operation needs to be done depending on the operand, because in Perl the
operand's type is unclear. Mainly, a scalar can be a string, or a
number, or a bool, or all.
The roadmap to removal
In perl 5.18 (2013), 6 years after being introduced and used by
programmers without warning, smart match was declared as experimental,
which is weird if you think about it. You now have to add "use
experimental "smartmatch"" to silence the warning. What happens to the
"switch" statement then? Since it's tied to smart matching, it also gets
the same fate: became experimental in 5.18.
In perl 5.38 (2023) smart match is deprecated. You can no longer silence
the warning with "use experimental 'smartmatch'" and must replace the
use of smart match with something else.
Perl 5.42 (planned 2025) will finally remove smart match, resulting in a
syntax error if you still use it.
Modules
However, if you still miss smart matching, some modules have been
written to give you the same/similar feature.
match::smart (by TOBYINK, first released 2013, pure-perl) gives you a
similar behaviour to perl's own "~~". It can be used as the "|M|"
operator or as the match() function.
Switch::Back (by DCONWAY, first released in June 2024, pure-perl).
Written by one of the designers of Perl 6, Switch::Back offers roughly
the same feature set as the old "switch" and smartmatching. Although
there's no longer "~~", just the smartmatch() function. So basically
what match::smart offers, but 11 years later.
match::simple (by TOBYINK which is also the author of "match::smart",
first released in 2013, in the same distribution as "match::smart",
available in XS as well as pure-perl) offers a simplified version of
smart matching. It has 8 kinds of behaviors depending only on the
/right/ hand side.
Also see match::simple::sugar which gives you "when", "then", and
"numeric" for use in a for() statement as a switch/use alternative.
Switch::Right (by DCONWAY, first released in June 2024, pure-perl). Also
like TOBYINK with his duo of "match::smart" and "match::simple", DCONWAY
offers a companion to "Switch::Back", a simplified/reimagined version of
"switch" and smartmatching by simplifying the rules from 23 to just 6.
The rules still depend on a mix of left and right operands.
Personal take
I personally haven't used "switch" all that much in Perl, though I used
to use quite a bit of smartmatching in the 2010s, mostly the "$str ~~
@ary_of_strs" variant. I won't use "match::smart" or "Switch::Back" in
any practical code any time soon (or ever), but which between
"match::simple" and "Switch::Right" are the best compromise? I guess
we'll have to see. In the mean time, see my benchmark in
Bencher::ScenarioBundle::SmartMatch.
Other modules
Smart::Match (by LEONT, first released in 2011, pure-perl) offers a
bunch of functions related to matching. Probably too low-level to use if
you just want a smart match replacement.
ACME::CPANMODULES ENTRIES
match::smart
Author: TOBYINK <https://metacpan.org/author/TOBYINK>
Switch::Back
match::simple
Author: TOBYINK <https://metacpan.org/author/TOBYINK>
match::simple::sugar
Author: TOBYINK <https://metacpan.org/author/TOBYINK>
Switch::Right
Bencher::ScenarioBundle::SmartMatch
Smart::Match
Author: LEONT <https://metacpan.org/author/LEONT>
FAQ
What is an Acme::CPANModules::* module?
An Acme::CPANModules::* module, like this module, contains just a list
of module names that share a common characteristics. It is a way to
categorize modules and document CPAN. See Acme::CPANModules for more
details.
What are ways to use this Acme::CPANModules module?
Aside from reading this Acme::CPANModules module's POD documentation,
you can install all the listed modules (entries) using cpanm-cpanmodules
script (from App::cpanm::cpanmodules distribution):
% cpanm-cpanmodules -n SmartMatch
Alternatively you can use the cpanmodules CLI (from App::cpanmodules
distribution):
% cpanmodules ls-entries SmartMatch | cpanm -n
or Acme::CM::Get:
% perl -MAcme::CM::Get=SmartMatch -E'say $_->{module} for @{ $LIST->{entries} }' | cpanm -n
or directly:
% perl -MAcme::CPANModules::SmartMatch -E'say $_->{module} for @{ $Acme::CPANModules::SmartMatch::LIST->{entries} }' | cpanm -n
This Acme::CPANModules module also helps lcpan produce a more meaningful
result for "lcpan related-mods" command when it comes to finding related
modules for the modules listed in this Acme::CPANModules module. See
App::lcpan::Cmd::related_mods for more details on how "related modules"
are found.
HOMEPAGE
Please visit the project's homepage at
( run in 2.365 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )