File Coverage

blib/lib/Syntax/Keyword/Junction.pm
Criterion Covered Total %
statement 15 15 100.0
branch n/a
condition n/a
subroutine 8 8 100.0
pod 4 4 100.0
total 27 27 100.0


line stmt bran cond sub pod time code
1             package Syntax::Keyword::Junction;
2 11     11   1483903 use strict;
  11         28  
  11         530  
3 11     11   67 use warnings;
  11         21  
  11         837  
4 11     11   268 use 5.006;
  11         62  
5              
6             our $VERSION = '0.003009';
7              
8             require Syntax::Keyword::Junction::All;
9             require Syntax::Keyword::Junction::Any;
10             require Syntax::Keyword::Junction::None;
11             require Syntax::Keyword::Junction::One;
12              
13 11         146 use Sub::Exporter::Progressive -setup => {
14             exports => [qw( all any none one )],
15             groups => {
16             default => [qw( all any none one )],
17             # for the switch from Exporter
18             ALL => [qw( all any none one )],
19             },
20 11     11   6097 };
  11         16343  
21              
22 114     114 1 998821 sub all { Syntax::Keyword::Junction::All->new(@_) }
23 81     81 1 229960 sub any { Syntax::Keyword::Junction::Any->new(@_) }
24 87     87 1 288271 sub none { Syntax::Keyword::Junction::None->new(@_) }
25 102     102 1 232356 sub one { Syntax::Keyword::Junction::One->new(@_) }
26              
27             1;
28              
29             __END__
30              
31             =pod
32              
33             =encoding UTF-8
34              
35             =for :stopwords Arthur Axel "fREW" Schmidt Carl Franks
36              
37             =head1 NAME
38              
39             Syntax::Keyword::Junction - Comparisons against multiple values
40              
41             =head1 SYNOPSIS
42              
43             use Syntax::Keyword::Junction qw/ all any none one /;
44              
45             if (any(@grant) eq 'su') {
46             ...
47             }
48              
49             if (all($foo, $bar) >= 10) {
50             ...
51             }
52              
53             if (qr/^\d+$/ == all(@answers)) {
54             ...
55             }
56              
57             if (all(@input) <= scalar @limits) {
58             ...
59             }
60              
61             if (none(@pass) eq 'password') {
62             ...
63             }
64              
65             if (one(@answer) == 42) {
66             ...
67             }
68              
69             or if you want to rename an export, use L<Sub::Exporter> options:
70              
71             use Syntax::Keyword::Junction any => { -as => 'robot_any' };
72              
73             if (robot_any(@grant) eq 'su') {
74             ...
75             }
76              
77             =head1 DESCRIPTION
78              
79             This is a lightweight module which provides 'Junction' operators, the most
80             commonly used being C<any> and C<all>.
81              
82             Inspired by the Perl 6 design docs,
83             L<https://web.archive.org/web/20230922160729/https://raku.org/archive/doc/design/exe/E06.html#The%20Wonderful%20World%20of%20Junctions>.
84              
85             Provides a limited subset of the functionality of L<Quantum::Superpositions>,
86             see L</"SEE ALSO"> for comment.
87              
88             Notice in the L</SYNOPSIS> above, that if you want to match against a
89             regular expression, you must use C<==> or C<!=>. B<Not> C<=~> or C<!~>. You
90             must also use a regex object, such as C<qr/\d/>, not a plain regex such as
91             C</\d/>.
92              
93             =head1 SUBROUTINES
94              
95             =head2 all()
96              
97             Returns an object which overloads the following operators:
98              
99             '<', '<=', '>', '>=', '==', '!=',
100             'lt', 'le', 'gt', 'ge', 'eq', 'ne',
101             '~~'
102              
103             Returns true only if B<all> arguments test true according to the operator
104             used.
105              
106             =head2 any()
107              
108             Returns an object which overloads the following operators:
109              
110             '<', '<=', '>', '>=', '==', '!=',
111             'lt', 'le', 'gt', 'ge', 'eq', 'ne',
112             '~~'
113              
114             Returns true if B<any> argument tests true according to the operator used.
115              
116             =head2 none()
117              
118             Returns an object which overloads the following operators:
119              
120             '<', '<=', '>', '>=', '==', '!=',
121             'lt', 'le', 'gt', 'ge', 'eq', 'ne',
122             '~~'
123              
124             Returns true only if B<no> argument tests true according to the operator
125             used.
126              
127             =head2 one()
128              
129             Returns an object which overloads the following operators:
130              
131             '<', '<=', '>', '>=', '==', '!=',
132             'lt', 'le', 'gt', 'ge', 'eq', 'ne',
133             '~~'
134              
135             Returns true only if B<one and only one> argument tests true according to
136             the operator used.
137              
138             =head1 ALTERING JUNCTIONS
139              
140             You cannot alter junctions. Instead, you can create new junctions out of old
141             junctions. You can do this by calling the C<values> method on a junction.
142              
143             my $numbers = any(qw/1 2 3 4 5/);
144             print $numbers == 3 ? 'Yes' : 'No'; # Yes
145              
146             $numbers = any( grep { $_ != 3 } $numbers->values );
147             print $numbers == 3 ? 'Yes' : 'No'; # No
148              
149             You can also use the C<map> method:
150              
151             my $numbers = any(qw/1 2 3 4 5/);
152             my $prime = $numbers->map( \&is_prime );
153              
154             say for $prime->values; # prints 0, 1, 1, 0, 1
155              
156             =head1 EXPORT
157              
158             'all', 'any', 'none', 'one', as requested.
159              
160             All subroutines can be called by its fully qualified name, if you don't
161             want to export them.
162              
163             use Syntax::Keyword::Junction;
164              
165             if (Syntax::Keyword::Junction::any( @questions )) {
166             ...
167             }
168              
169             =head1 WARNING
170              
171             When comparing against a regular expression, you must remember to use a
172             regular expression object: C<qr/\d/> B<Not> C</d/>. You must also use either
173             C<==> or C<!=>. This is because C<=~> and C<!~> cannot be overridden.
174              
175             =head1 TO DO
176              
177             Add overloading for arithmetic operators, such that this works:
178              
179             $result = any(2,3,4) * 2;
180              
181             if ($result == 8) {...}
182              
183             =head1 SEE ALSO
184              
185             This module is actually a fork of L<Perl6::Junction> with very few
186             (initial) changes. The reason being that we want to avoid the
187             incendiary name containing Perl 6.
188              
189             L<Quantum::Superpositions> provides the same functionality as this, and
190             more. However, this module provides this limited functionality at a much
191             greater runtime speed, with my benchmarks showing between 500% and 6000%
192             improvement.
193              
194             L<https://web.archive.org/web/20230922160729/https://raku.org/archive/doc/design/exe/E06.html#The%20Wonderful%20World%20of%20Junctions> - "The Wonderful World of Junctions".
195              
196             =head1 BUGS
197              
198             Please report any bugs or feature requests on the bugtracker website
199             L<https://github.com/haarg/Syntax-Keyword-Junction/issues>
200              
201             When submitting a bug or request, please include a test-file or a
202             patch to an existing test-file that illustrates the bug or desired
203             feature.
204              
205             =head1 CONTRIBUTORS
206              
207             =for stopwords Arthur Axel 'fREW' Schmidt Carl Franks David Steinbrunner Graham Knop Maxime Soulé Michael Schout Olivier Mengué Paul Cochrane Ricardo Signes Rob Hoelz
208              
209             =over 4
210              
211             =item *
212              
213             Arthur Axel 'fREW' Schmidt <frioux@gmail.com>
214              
215             =item *
216              
217             Carl Franks <cpan@fireartist.com>
218              
219             =item *
220              
221             David Steinbrunner <dsteinbrunner@pobox.com>
222              
223             =item *
224              
225             Graham Knop <haarg@haarg.org>
226              
227             =item *
228              
229             Maxime Soulé <btik-cpan@scoubidou.com>
230              
231             =item *
232              
233             Michael Schout <schoutm@gmail.com>
234              
235             =item *
236              
237             Olivier Mengué <dolmen@cpan.org>
238              
239             =item *
240              
241             Paul Cochrane <paul.cochrane@posteo.de>
242              
243             =item *
244              
245             Ricardo Signes <rjbs@cpan.org>
246              
247             =item *
248              
249             Rob Hoelz <rob@hoelz.ro>
250              
251             =back
252              
253             =head1 AUTHORS
254              
255             =over 4
256              
257             =item *
258              
259             Arthur Axel "fREW" Schmidt <frioux+cpan@gmail.com>
260              
261             =item *
262              
263             Carl Franks
264              
265             =back
266              
267             =head1 COPYRIGHT AND LICENSE
268              
269             This software is copyright (c) 2024 by Arthur Axel "fREW" Schmidt.
270              
271             This is free software; you can redistribute it and/or modify it under
272             the same terms as the Perl 5 programming language system itself.
273              
274             =cut