1match::simple(3) User Contributed Perl Documentation match::simple(3)
2
3
4
6 match::simple - simplified clone of smartmatch operator
7
9 use v5.10;
10 use match::simple;
11
12 if ( $this |M| $that ) {
13 say "$this matches $that";
14 }
15
17 match::simple provides a simple match operator "|M|" that acts like a
18 sane subset of the (as of Perl 5.18) deprecated smart match operator.
19 Unlike smart match, the behaviour of the match is determined entirely
20 by the operand on the right hand side.
21
22 • If the right hand side is "undef", then there is only a match if
23 the left hand side is also "undef".
24
25 • If the right hand side is a non-reference, then the match is a
26 simple string match.
27
28 • If the right hand side is a reference to a regexp, then the left
29 hand is evaluated .
30
31 • If the right hand side is a code reference, then it is called in a
32 boolean context with the left hand side being passed as an
33 argument.
34
35 • If the right hand side is an object which provides a "MATCH"
36 method, then it this is called as a method, with the left hand side
37 being passed as an argument.
38
39 • If the right hand side is an object which overloads "~~", then this
40 will be used.
41
42 • If the right hand side is an arrayref, then the operator recurses
43 into the array, with the match succeeding if the left hand side
44 matches any array element.
45
46 • If any other value appears on the right hand side, the operator
47 will croak.
48
49 If you don't like the crazy Sub::Infix operator, you can alternatively
50 export a more normal function:
51
52 use v5.10;
53 use match::simple qw(match);
54
55 if ( match($this, $that) ) {
56 say "$this matches $that";
57 }
58
59 If you're making heavy use of this module, then this is probably your
60 best option, as it runs significantly faster.
61
62 XS Backend
63 If you install match::simple::XS, a faster XS-based implementation will
64 be used instead of the pure Perl functions. Depending on what sort of
65 match you are doing, this is likely to be several times faster. In
66 extreme cases, such as matching a string in an arrayref, it can be
67 twenty-five times faster, or more. However, where $that is a single
68 regexp, it's around 30% slower. Overall though, I think the
69 performance improvement is worthwhile.
70
71 If you want to take advantage of this speed up, use the "match"
72 function rather than the "|M|" operator. Otherwise all your gains will
73 be lost to the slow implementation of operator overloading.
74
75 The constant "match::simple::IMPLEMENTATION" tells you which backend is
76 currently in use.
77
78 Environment
79 Setting the "MATCH_SIMPLE_IMPLEMENTATION" environment variable to "PP"
80 encourages match::simple to use the pure Perl backend.
81
83 Please report any bugs to
84 <https://github.com/tobyink/p5-match-simple/issues>.
85
87 match::smart, match::simple::sugar.
88
89 This module uses Exporter::Tiny.
90
92 Toby Inkster <tobyink@cpan.org>.
93
95 This software is copyright (c) 2013-2014, 2017, 2022-2023 by Toby
96 Inkster.
97
98 This is free software; you can redistribute it and/or modify it under
99 the same terms as the Perl 5 programming language system itself.
100
102 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
103 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
104 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
105
106
107
108perl v5.38.0 2023-07-21 match::simple(3)