Abstract.pm

filter: Statementstmt 65.0% 13 / 20 Branchbran 0.0% 0 / 4 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 57.1% 4 / 7 Podpod 33.3% 1 / 3 totaltotal 52.9% 18 / 34 CC 3 SCAR 15.9
CC
3
Cov
54%
new
12.0
to_source
2.0
to_subref
2.0
CRAP 4.9
« Abstract/Util.pm ↑ Summary fix_tests.pl »
1 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 package Data::Filter::Abstract;
4  
5 # ABSTRACT: Generate Perl filter subs from data structures
6  
7 1 ▶use Data::Filter::Abstract::Util qw/:all/;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 use overload
10 1 ▶ '""' => "to_source",
Subroutine BEGIN: called CC=1 SCAR=0.0
11 '&{}' => "to_subref"
12 1 ;
13  
14 0 ▶sub new {
Subroutine new: not called CC=3 SCAR=24.8
Pod documented
15 0 ▶ my $class = ref $_[0] ? ref shift : shift;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
16  
17 0 my $source = simple_sub(@_);
18 0 my $coderef = eval sprintf 'sub { no warnings qw/uninitialized/; local $_ = shift; %s }', $source;
19  
20 0 ▶ die "error in $source : $@" unless ref $coderef eq "CODE";
Branch: 1 branch
BranchTrueFalseTotal
unless ref $coderef eq "CODE" 0 0 0
21 0 bless {
22 source => $source,
23 subref => $coderef,
24 }, $class;
25 }
26  
27 0 ▶sub to_source {
Subroutine to_source: not called CC=1 SCAR=6.9
Pod undocumented
28 return shift()->{source}
29 0 }
30  
31 0 ▶sub to_subref {
Subroutine to_subref: not called CC=1 SCAR=6.9
Pod undocumented
32 return shift()->{subref}
33 0 }
34  
35 1 1;
36  
37 =head1 NAME
38  
39 Data::Filter::Abstract - generate Perl filter subs from data structures
40  
41  
42 =head1 SYNOPSIS
43  
44 use Data::Filter::Abstract;
45  
46 my $filter = Data::Filter::Abstract->new({
47 foo => [ 1, "bar", qr/\d+/ ],
48 baz => { '>' => 2, '<' => 5 },
49 });
50  
51 # Use as a CODE reference
52 my $match = $filter->({ foo => 1, baz => 3 }); # true
53  
54 # Get the generated Perl expression as text
55 print "$filter\n";
56  
57 =head1 DESCRIPTION
58  
59 C<Data::Filter::Abstract> wraps the expression-generation DSL provided by
60 L<Data::Filter::Abstract::Util> and produces executable Perl predicates.
61  
62 It compiles a hash- or array-based filter description into:
63  
64 =over 4
65  
66 =item *
67  
68 A string expression suitable for embedding in a C<sub { ... }> block
69  
70 =item *
71  
72 A compiled coderef that can be called directly with a hash reference
73  
74 =back
75  
76 Objects overload stringification to return the expression and the code
77 dereference operator to return the coderef.
78  
79 =head1 CONSTRUCTION
80  
81 =head2 new
82  
83 my $filter = Data::Filter::Abstract->new($dsl);
84  
85 C<$dsl> is any input accepted by C<simple_sub> in
86 L<Data::Filter::Abstract::Util> (hash, array, operator hash, scalar, etc.).
87  
88 The constructor converts the input data structure into a Perl expression string and compiles the expression into a coderef, or dies if the generated coderef cannot be compiled.
89  
90 =head1 USAGE
91  
92 =head2 As a CODE reference
93  
94 The object can be used directly as a predicate:
95  
96 my $match = $filter->($row);
97  
98 =head2 Accessing the coderef
99  
100 my $coderef = $filter->to_subref;
101 $coderef->($row);
102  
103 =head2 Accessing the expression string
104  
105 my $expr = $filter->to_source;
106 print "$expr\n";
107  
108 =head1 OVERLOADING
109  
110 =over 4
111  
112 =item * Stringification
113  
114 Returns the generated expression.
115  
116 =item * Code dereference
117  
118 Returns the compiled coderef.
119  
120 =back
121  
122 =head1 CAVEAT
123  
124 This module uses C<eval> to compile Perl code. Embedded code references are executed verbatim.
125  
126 =head1 DATA STRUCTURE CONVERSION
127  
128 =head2 GENERAL SEMANTICS
129  
130 The data-structure-to-filter-sub attempts to mimic L<SQL::Abstract> as closely as possible.
131  
132 =over 4
133  
134 =item *
135  
136 All generated expressions assume C<$_> is a hash reference.
137  
138 =item *
139  
140 Field access is performed as C<$_->{field}>.
141  
142 =item *
143  
144 Hashes represent logical AND.
145  
146 =item *
147  
148 Arrays represent logical OR, unless explicitly overridden by logical operators.
149  
150 =item *
151  
152 Generated expressions are syntactically valid Perl code suitable for C<eval>.
153  
154 =back
155  
156 =head2 BASIC FILTER FORMS
157  
158 =head3 Scalar equality
159  
160 simple_sub(foo => "bar")
161  
162 Generates a string equality comparison:
163  
164 ($_->{foo} eq "bar")
165  
166 If the value looks like a number, numeric comparison is used:
167  
168 simple_sub(foo => 1)
169 # ($_->{foo} == 1)
170  
171 =head3 Regular expressions
172  
173 simple_sub(foo => qr/12/)
174 simple_sub(foo => qr/[a-z]/i)
175  
176 Generates regex match expressions:
177  
178 ($_->{foo} =~ qr/12/)
179 ($_->{foo} =~ qr/[a-z]/i)
180  
181 =head3 Hash form (implicit AND)
182  
183 simple_sub({ bar => 1, baz => "wq", boo => qr/12/ })
184  
185 Equivalent to:
186  
187 ($_->{bar} == 1)
188 && ($_->{baz} eq "wq")
189 && ($_->{boo} =~ qr/12/)
190  
191 C<simple_hash> behaves identically.
192  
193 =head2 ARRAY FORMS
194  
195 =head3 Simple OR array
196  
197 simple_sub(foo => [ 1, "wq", qr/12/ ])
198  
199 Generates a logical OR over the values:
200  
201 ($_->{foo} == 1)
202 || ($_->{foo} eq "wq")
203 || ($_->{foo} =~ qr/12/)
204  
205 =head3 Arrays of operator expressions
206  
207 simple_array(foo => [ { '==', 2 }, { '>', 5 } ])
208  
209 Generates:
210  
211 ($_->{foo} == 2) || ($_->{foo} > 5)
212  
213 =head2 LOGICAL ARRAYS
214  
215 Arrays may begin with a logical operator to control how elements are combined.
216  
217 =head3 AND logic
218  
219 simple_sub(foo => [ -and => { '==', 2 }, { '>=', 5 } ])
220  
221 Generates:
222  
223 ($_->{foo} == 2) && ($_->{foo} >= 5)
224  
225 =head3 OR logic
226  
227 simple_sub(foo => [ -or => { '==', 2 }, { '>=', 5 } ])
228  
229 Generates:
230  
231 ($_->{foo} == 2) || ($_->{foo} >= 5)
232  
233 =head2 OPERATOR HASHES (FUNCTION HASHES)
234  
235 A field value may be a hash mapping operators to values.
236  
237 simple_sub(foo => { '>' => 12, '<' => 23 })
238  
239 Generates:
240  
241 ($_->{foo} < 23) && ($_->{foo} > 12)
242  
243 The order of operators is not significant.
244  
245 =head3 Supported operators (as tested)
246  
247 =over 4
248  
249 =item * String operators
250  
251 eq ne lt le gt ge
252  
253 =item * Numeric operators
254  
255 == != < <= > >=
256  
257 =item * Regex operators
258  
259 =~ !~
260  
261 =back
262  
263 =head2 OPERATORS WITH ARRAY VALUES
264  
265 =head3 Equality with array
266  
267 simple_sub(status => { eq => [ 'assigned', 'in-progress', 'pending' ] })
268  
269 Generates an OR expression:
270  
271 ($_->{status} eq "assigned")
272 || ($_->{status} eq "in-progress")
273 || ($_->{status} eq "pending")
274  
275 Equivalent logic may also be expressed via a logical array:
276  
277 simple_sub(status => [
278 -or =>
279 { eq => 'assigned' },
280 { eq => 'in-progress' },
281 { eq => 'pending' },
282 ])
283  
284 =head3 Regex operators with arrays
285  
286 simple_sub(foo => { '=~' => [ qr/12/, qr/23/i ] })
287  
288 Generates:
289  
290 ($_->{foo} =~ qr/12/)
291 || ($_->{foo} =~ qr/23/i)
292  
293 Non-regex values are coerced to regexes:
294  
295 simple_sub(foo => { '=~' => [ "12", qr/23/i ] })
296  
297 =head2 UNDEF HANDLING
298  
299 simple_sub(user => undef)
300  
301 Generates:
302  
303 (! defined $_->{user})
304  
305 Operator hashes may also compare against undef, though some combinations are
306 known to be problematic (see tests marked as failing).
307  
308 =head2 FIELD-TO-FIELD COMPARISON (SCALAR REF)
309  
310 If the value is a scalar reference, it is interpreted as another field name.
311  
312 simple_sub(foo => \"bar")
313  
314 Generates a dynamic comparison:
315  
316 ( (looks_like_number($_->{foo}))
317 ? ($_->{foo} == $_->{bar})
318 : ($_->{foo} eq $_->{bar})
319 )
320  
321 This form may also appear inside logical arrays.
322  
323 =head2 CODE REFERENCES
324  
325 A code reference may be supplied as a filter.
326  
327 simple_sub(sub { $_->{foo}->{bar} > 5 })
328  
329 or
330  
331 simple_sub(foo => sub { $_->{foo}->{bar} > 5 })
332  
333 The code reference is deparsed and embedded verbatim:
334  
335 (sub { use strict; $_->{'foo'}{'bar'} > 5; })
336  
337 The field name is ignored when a code reference is supplied.
338  
339 =head2 MIXED COMPLEX EXPRESSIONS
340  
341 Arrays may contain mixed value types:
342  
343 simple_sub(foo => [
344 1,
345 "wq",
346 qr/12/,
347 sub { shift()->{foo}->{bar} > 5 }
348 ])
349  
350 Generates a logical OR over all components.
351  
352 =head1 SEE ALSO
353  
354 L<Data::Filter::Abstract>, L<SQL::Abstract>
355  
356 =head1 AUTHOR
357  
358 Simone Cesano <scesano@cpan.org>
359  
360 =head1 LICENSE
361  
362 This software is copyright (c) 2025 by Simone Cesano.
363  
364 This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
365  
366 =cut
367  
« Abstract/Util.pm ↑ Summary fix_tests.pl »