1match::simple::sugar(3)User Contributed Perl Documentatiomnatch::simple::sugar(3)
2
3
4
6 match::simple::sugar - a few extras for match::simple
7
9 This module provides a "given"/"when" substitute for match::simple.
10
11 use match::simple::sugar;
12
13 for ( $var ) {
14 when 'foo', then { ... };
15 when 'bar', 'baz', then { ... };
16 ...; # otherwise
17 }
18
19 It also provides a function for numeric matching (because match::simple
20 always assumes you want stringy matching if the right-hand-side is a
21 defined non-reference value).
22
23 use match::simple::sugar;
24
25 for ( $var ) {
26 when numeric 0, then { ... };
27 when numeric 1, then { ... };
28 ...; # otherwise
29 }
30
32 This module exports three functions "when", "then", and "numeric", and
33 also re-exports "match" from match::simple.
34
35 "when" and "then"
36 The "when" and "then" functions are intended to be used together,
37 inside a "for ( SCALAR ) { ... }" block. The block acts as a
38 topicalizer (it sets $_) and also a control-flow mechanism ("when" can
39 use "next" to jump out of it). Any other use of "when" and "then" is
40 unsupported.
41
42 "when( @values, $then )"
43
44 The "when" function accepts a list of values, followed by a special
45 $then argument.
46
47 If $_ matches (according to the definition in match::simple) any of the
48 values, then the $then argument will be executed, and "when" will use
49 the Perl built-in "next" keyword to jump out of the surrounding "for"
50 block.
51
52 "then { ... }"
53
54 The "then" function takes a block of code and returns an object
55 suitable for use as "when"'s $then argument.
56
57 In the current implementation, the block of code should not inspect @_
58 or "wantarray", and should not use the "return", "next", "last", or
59 "redo" keywords. (If you set any of the "PERL_STRICT",
60 "EXTENDED_TESTING", "AUTHOR_TESTING", or "RELEASE_TESTING" environment
61 variables to true, then match::simple::sugar will try to enforce this!
62 This is intended to catch faulty "then" blocks when running your test
63 suite.)
64
65 "numeric"
66 The "numeric" function accepts a number and returns a blessed object
67 which has a "MATCH" method. The "MATCH" method returns true if it is
68 called with a single defined non-referece scalar that is numerically
69 equal to the original number passed to "numeric". Example:
70
71 numeric( '5.0' )->MATCH( '5.000' ); # true
72
73 This is intended for use in cases like:
74
75 if ( match $var, numeric 1 ) {
76 ...;
77 }
78
80 Please report any bugs to
81 <https://github.com/tobyink/p5-match-simple/issues>.
82
84 match::simple.
85
86 This module uses Exporter::Tiny.
87
89 Toby Inkster <tobyink@cpan.org>.
90
91 This module is inspired by a talk I gave to Boston.PM <https://boston-
92 pm.github.io/>.
93
95 This software is copyright (c) 2023 by Toby Inkster.
96
97 This is free software; you can redistribute it and/or modify it under
98 the same terms as the Perl 5 programming language system itself.
99
101 THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR IMPLIED
102 WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
103 MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
104
105
106
107perl v5.38.0 2023-07-21 match::simple::sugar(3)