File Coverage

blib/lib/Acme/CPANModules/SmartMatch.pm
Criterion Covered Total %
statement 9 9 100.0
branch n/a
condition n/a
subroutine 3 3 100.0
pod n/a
total 12 12 100.0


line stmt bran cond sub pod time code
1             package Acme::CPANModules::SmartMatch;
2              
3 1     1   368652 use strict;
  1         3  
  1         51  
4 1     1   7 use warnings;
  1         2  
  1         80  
5 1     1   669 use Acme::CPANModulesUtil::Misc;
  1         717  
  1         202  
6              
7             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8             our $DATE = '2024-07-01'; # DATE
9             our $DIST = 'Acme-CPANModules-SmartMatch'; # DIST
10             our $VERSION = '0.007'; # VERSION
11              
12             my $text = <<'MARKDOWN';
13             **About smart match**
14              
15             Smart matching, via the operator `~~`, was introduced in perl 5.10 (released
16             2007). It's inspired by Perl 6 (now called Raku)'s `given/when` and/or Ruby's
17             `case` and `===` operator that can "do the right/smart thing" in a `case`
18             statement. Smart matching was indeed introduced along the new `switch` in perl
19             5.10.
20              
21             What can smart match do? A whole lot. It can do string equality like `eq` if
22             given a string on the left hand side and a string on the right hand side. Or it
23             can do numeric equality like `==` when both sides are numbers. It can do regex
24             matching like `=~` if the left hand side is a scalar and the right hand side is
25             a regexp.
26              
27             But wait, there's (much) more. Interesting things begin when the left/right hand
28             side is an array/hash/code/object. `$str ~~ @ary_of_strs`, probably the most
29             common use-case for smart matching, can do value-in-array checking, equivalent
30             to `grep { $str eq $_ } @ary_of_strs` but with short-circuiting capability. Then
31             there's `$re ~~ @ary_of_strs` which can perform regex matching over the elements
32             of array. Now what about when the right hand side is an arrayref or hashref? Or
33             the left hand side? What if the array is an array of regexes instead? Or a mix
34             of other types?
35              
36             You need a full-page table as a reference of what will happen in smart matching,
37             depending on the combination of operands. Things got complex real fast.
38             Behaviors were changed from release to release, starting from 5.10.1. Then
39             nobody was sure what smart matching should or should not do exactly.
40              
41             In the end almost everyone agrees that smart matching is a bad fit for a weakly
42             typed language like Perl. The programmer needs to be explicit on what type of
43             operation should be done by specifying the appropriate /operator/ (e.g. `==` vs
44             `eq`) instead of the operator deducing what operation needs to be done depending
45             on the operand, because in Perl the operand's type is unclear. Mainly, a scalar
46             can be a string, or a number, or a bool, or all.
47              
48              
49             **The roadmap to removal**
50              
51             In perl 5.18 (2013), 6 years after being introduced and used by programmers
52             without warning, smart match was declared as experimental, which is weird if you
53             think about it. You now have to add `use experimental "smartmatch"` to silence
54             the warning. What happens to the `switch` statement then? Since it's tied to
55             smart matching, it also gets the same fate: became experimental in 5.18.
56              
57             In perl 5.38 (2023) smart match is deprecated. You can no longer silence the
58             warning with "use experimental 'smartmatch'" and must replace the use of smart
59             match with something else.
60              
61             Perl 5.42 (planned 2025) will finally remove smart match, resulting in a syntax
62             error if you still use it.
63              
64              
65             **Modules**
66              
67             However, if you still miss smart matching, some modules have been written to
68             give you the same/similar feature.
69              
70             (by TOBYINK, first released 2013, pure-perl) gives you a
71             similar behaviour to perl's own `~~`. It can be used as the `|M|` operator or as
72             the `match()` function.
73              
74             (by DCONWAY, first released in June 2024, pure-perl). Written
75             by one of the designers of Perl 6, Switch::Back offers roughly the same feature
76             set as the old `switch` and smartmatching. Although there's no longer `~~`, just
77             the `smartmatch()` function. So basically what offers, but 11
78             years later.
79              
80             (by TOBYINK which is also the author of `match::smart`, first
81             released in 2013, in the same distribution as `match::smart`, available in XS as
82             well as pure-perl) offers a simplified version of smart matching. It has 8 kinds
83             of behaviors depending only on the /right/ hand side.
84              
85             Also see which gives you `when`, `then`, and `numeric`
86             for use in a `for()` statement as a switch/use alternative.
87              
88             (by DCONWAY, first released in June 2024, pure-perl). Also
89             like TOBYINK with his duo of `match::smart` and `match::simple`, DCONWAY offers
90             a companion to `Switch::Back`, a simplified/reimagined version of `switch` and
91             smartmatching by simplifying the rules from 23 to just 6. The rules still depend
92             on a mix of left and right operands.
93              
94              
95             **Personal take**
96              
97             I personally haven't used `switch` all that much in Perl, though I used to use
98             quite a bit of smartmatching in the 2010s, mostly the `$str ~~ @ary_of_strs`
99             variant. I won't use `match::smart` or `Switch::Back` in any practical code any
100             time soon (or ever), but which between `match::simple` and `Switch::Right` are
101             the best compromise? I guess we'll have to see. In the mean time, see my
102             benchmark in .
103              
104              
105             **Other modules**
106              
107             (by LEONT, first released in 2011, pure-perl) offers a bunch
108             of functions related to matching. Probably too low-level to use if you just want
109             a smart match replacement.
110              
111             MARKDOWN
112              
113             our $LIST = {
114             summary => 'List of modules to do smart matching',
115             description => $text,
116             tags => ['task'],
117             };
118              
119             Acme::CPANModulesUtil::Misc::populate_entries_from_module_links_in_description;
120              
121             1;
122             # ABSTRACT: List of modules to do smart matching
123              
124             __END__