blib/lib/Venus/Match.pm

filter: Statementstmt 96.1% 148 / 154 Branchbran 75.9% 41 / 54 Conditioncond 60.0% 9 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 42 / 42 Podpod 94.4% 17 / 18 totaltotal 88.4% 259 / 293 CC 33 SCAR 14.8
CC
33
Cov
89%
__ANON__
12.0
__ANON__
8.8
result
8.0
CRAP 4.4
« blib/lib/Venus/Json.pm ↑ Summary blib/lib/Venus/Random.pm »
1 package Venus::Match;
2  
3 3 ▶use 5.018;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 # IMPORTS
9  
10 3 ▶use Venus::Class 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 # INHERITS
15  
16 3 base 'Venus::Kind::Utility';
17  
18 # INTEGRATES
19  
20 3 with 'Venus::Role::Valuable';
21 3 with 'Venus::Role::Buildable';
22 3 with 'Venus::Role::Accessible';
23  
24 # ATTRIBUTES
25  
26 3 attr 'on_none';
27 3 attr 'on_only';
28 3 attr 'on_then';
29 3 attr 'on_when';
30  
31 # BUILDERS
32  
33 372 ▶sub build_self {
Subroutine build_self: called CC=5 SCAR=17.0
Pod undocumented
34 372 my ($self, $data) = @_;
35  
36 372 ▶ $self->on_none(sub{}) if !$self->on_none;
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_none 372 0 372
37 372 ▶ $self->on_only(sub{1}) if !$self->on_only;
Subroutine __ANON__: called CC=1 SCAR=0.2
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_only 372 0 372
38 372 ▶ $self->on_then([]) if !$self->on_then;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_then 372 0 372
39 372 ▶ $self->on_when([]) if !$self->on_when;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_when 372 0 372
40  
41 372 return $self;
42 }
43  
44 # METHODS
45  
46 4 ▶sub boolean {
Subroutine boolean: called CC=1 SCAR=0.0
Pod documented
47 4 my ($self) = @_;
48  
49 4 ▶ $self->then(sub{return true})->none(sub{return false});
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
50  
51 4 return $self;
52 }
53  
54 1 ▶sub clear {
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
55 1 my ($self) = @_;
56  
57 1 ▶ $self->on_none(sub{});
Subroutine __ANON__: called CC=1 SCAR=0.0
58 1 ▶ $self->on_only(sub{1});
Subroutine __ANON__: called CC=1 SCAR=0.0
59 1 $self->on_then([]);
60 1 $self->on_when([]);
61  
62 1 return $self;
63 }
64  
65 4 ▶sub data {
Subroutine data: called CC=2 SCAR=6.9
Pod documented
66 4 my ($self, $data) = @_;
67  
68 4 ▶ while(my($key, $value) = each(%$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 14 4 18
69 14 $self->just($key)->then($value);
70 }
71  
72 4 return $self;
73 }
74  
75 2 ▶sub defined {
Subroutine defined: called CC=1 SCAR=0.0
Pod documented
76 2 my ($self) = @_;
77  
78 2 ▶ $self->only(sub{CORE::defined($_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
79  
80 2 return $self;
81 }
82  
83 10 ▶sub expr {
Subroutine expr: called CC=1 SCAR=0.3
Pod documented
84 10 my ($self, $topic) = @_;
85  
86 7 ▶ $self->when(sub{
Subroutine __ANON__: called CC=9 SCAR=24.8
87 7 my $value = $self->value;
88  
89 7 ▶ if (!CORE::defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 7 7
90 0 return false;
91 }
92 7 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 7 7
Condition33%1/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
700
MC/DC0%0/2
builtin::blessed($value) and not overload::Overloaded($value)
builtin::blessed($value) not overload::Overloaded($value)
Truth table33%1/3
A and not(B)
A= builtin::blessed($value)B= not overload::Overloaded($value)
ABresult
0X0
100
111
93 0 return false;
94 }
95 7 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 7 7
Condition33%1/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
070
MC/DC0%0/2
not builtin::blessed($value) and ref $value
not builtin::blessed($value) ref $value
Truth table33%1/3
not(A) and B
A= not builtin::blessed($value)B= ref $value
ABresult
0X0
100
111
96 0 return false;
97 }
98 7 ▶ if (ref($topic) eq 'Regexp' && "$value" =~ qr/$topic/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $topic eq 'Regexp' and "$value" =~ qr/$topic/u) { } 5 2 7
elsif ("$value" eq "$topic") { } 1 1 2
Condition100%3/3
ref $topic eq "Regexp" and "$value" =~ qr/$topic/u
!ll&&!rl&&r
115
MC/DC100%2/2
ref $topic eq "Regexp" and "$value" =~ qr/$topic/u
ref $topic eq "Regexp" "$value" =~ qr/$topic/u
Truth table100%3/3
A and B
A= ref $topic eq "Regexp"B= "$value" =~ qr/$topic/u
ABresult
0X0
100
111
99 5 return true;
100 }
101 elsif ("$value" eq "$topic") {
102 1 return true;
103 }
104 else {
105 1 return false;
106 }
107 10 });
108  
109 10 return $self;
110 }
111  
112 711 ▶sub just {
Subroutine just: called CC=1 SCAR=0.3
Pod documented
113 711 my ($self, $topic) = @_;
114  
115 623 ▶ $self->when(sub{
Subroutine __ANON__: called CC=7 SCAR=21.8
116 623 my $value = $self->value;
117  
118 623 ▶ if (!CORE::defined($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 623 623
119 0 return false;
120 }
121 623 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 623 623
Condition66%2/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
61670
MC/DC0%0/2
builtin::blessed($value) and not overload::Overloaded($value)
builtin::blessed($value) not overload::Overloaded($value)
Truth table66%2/3
A and not(B)
A= builtin::blessed($value)B= not overload::Overloaded($value)
ABresult
0X0
100
111
122 0 return false;
123 }
124 623 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 623 623
Condition66%2/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
76160
MC/DC0%0/2
not builtin::blessed($value) and ref $value
not builtin::blessed($value) ref $value
Truth table66%2/3
not(A) and B
A= not builtin::blessed($value)B= ref $value
ABresult
0X0
100
111
125 0 return false;
126 }
127 623 ▶ if ("$value" eq "$topic") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" eq "$topic") { } 179 444 623
128 179 return true;
129 }
130 else {
131 444 return false;
132 }
133 711 });
134  
135 711 return $self;
136 }
137  
138 326 ▶sub none {
Subroutine none: called CC=2 SCAR=6.9
Pod documented
139 326 my ($self, $code) = @_;
140  
141 326 ▶ $self->on_none(UNIVERSAL::isa($code, 'CODE') ? $code : sub{$code});
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($code, 'CODE') ? : 322 4 326
142  
143 326 return $self;
144 }
145  
146 2 ▶sub object {
Subroutine object: called CC=1 SCAR=0.0
Pod documented
147 2 my ($self) = @_;
148  
149 2 require Scalar::Util;
150  
151 2 ▶ $self->only(sub{Scalar::Util::blessed($_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
152  
153 2 return $self;
154 }
155  
156 6 ▶sub only {
Subroutine only: called CC=1 SCAR=0.0
Pod documented
157 6 my ($self, $code) = @_;
158  
159 6 $self->on_only($code);
160  
161 6 return $self;
162 }
163  
164 359 ▶sub result {
Subroutine result: called CC=8 SCAR=20.8
Pod documented
165 359 my ($self, @args) = @_;
166  
167 359 ▶ $self->value($args[0]) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 11 348 359
168  
169 359 my $result;
170 359 my $matched = 0;
171 359 my $value = $self->value;
172  
173 359 local $_ = $value;
174 359 ▶ if (!$self->on_only->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->on_only->($value)) 3 356 359
175 3 local $_ = $value;
176 3 $result = $self->on_none->($value);
177 3 ▶ return wantarray ? ($result, $matched) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 3 3
178 }
179  
180 356 ▶ for (my $i = 0; $i < @{$self->on_when}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->on_when;}) 641 166 807
181 641 ▶ if ($self->on_when->[$i]->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_when->[$i]->($value)) 190 451 641
182 190 $result = $self->on_then->[$i]->($value);
183 190 $matched++;
184 190 last;
185 }
186 }
187  
188 356 ▶ if (!$matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 166 190 356
189 166 local $_ = $value;
190 166 $result = $self->on_none->($value);
191 }
192  
193 356 ▶ return wantarray ? ($result, $matched) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 355 356
194 }
195  
196 4 ▶sub take {
Subroutine take: called CC=1 SCAR=0.0
Pod documented
197 4 my ($self) = @_;
198  
199 4 ▶ $self->then(sub{return $_[0]})->none(sub{return undef});
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
200  
201 4 return $self;
202 }
203  
204 3 ▶sub test {
Subroutine test: called CC=4 SCAR=13.9
Pod documented
205 3 my ($self) = @_;
206  
207 3 my $matched = 0;
208  
209 3 my $value = $self->value;
210  
211 3 local $_ = $value;
212  
213 3 ▶ return $matched if !$self->on_only->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_only->($value) 0 3 3
214  
215 3 ▶ for (my $i = 0; $i < @{$self->on_when}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->on_when;}) 6 1 7
216 6 ▶ if ($self->on_when->[$i]->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_when->[$i]->($value)) 2 4 6
217 2 $matched++;
218 2 last;
219 }
220 }
221  
222 3 return $matched;
223 }
224  
225 744 ▶sub then {
Subroutine then: called CC=2 SCAR=6.9
Pod documented
226 744 my ($self, $code) = @_;
227  
228 744 my $next = $#{$self->on_when};
229  
230 744 ▶ $self->on_then->[$next] = UNIVERSAL::isa($code, 'CODE') ? $code : sub{$code};
Subroutine __ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
BranchTrueFalseTotal
UNIVERSAL::isa($code, 'CODE') ? : 33 711 744
231  
232 744 return $self;
233 }
234  
235 2 ▶sub type {
Subroutine type: called CC=1 SCAR=0.0
Pod documented
236 2 my ($self, $expr) = @_;
237  
238 2 require Venus::Type;
239  
240 2 ▶ $self->when(sub{Venus::Type->new->check($expr)->eval($_[0])});
Subroutine __ANON__: called CC=1 SCAR=0.0
241  
242 2 return $self;
243 }
244  
245 743 ▶sub when {
Subroutine when: called CC=2 SCAR=7.0
Pod documented
246 743 my ($self, $code, @args) = @_;
247  
248 743 ▶ my $next = (@{$self->on_when}-$#{$self->on_then}) > 1 ? -1 : @{$self->on_when};
Branch: 1 branch
BranchTrueFalseTotal
@{$self->on_when;} - $#{$self->on_then;} > 1 ? : 0 743 743
249  
250 647 ▶ $self->on_when->[$next] = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
251 647 (local $_ = $_[0])->$code(@args);
252 743 };
253  
254 743 return $self;
255 }
256  
257 6 ▶sub where {
Subroutine where: called CC=1 SCAR=0.0
Pod documented
258 6 my ($self) = @_;
259  
260 6 my $where = $self->new;
261  
262 6 ▶ $self->then(sub{$where->result(@_)});
Subroutine __ANON__: called CC=1 SCAR=0.0
263  
264 6 return $where;
265 }
266  
267 4 ▶sub yesno {
Subroutine yesno: called CC=1 SCAR=0.0
Pod documented
268 4 my ($self) = @_;
269  
270 4 ▶ $self->then(sub{return "yes"})->none(sub{return "no"});
Subroutine __ANON__: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=1 SCAR=0.0
271  
272 4 return $self;
273 }
274  
275 3 1;
276  
277  
278  
279 =head1 NAME
280  
281 Venus::Match - Match Class
282  
283 =cut
284  
285 =head1 ABSTRACT
286  
287 Match Class for Perl 5
288  
289 =cut
290  
291 =head1 SYNOPSIS
292  
293 package main;
294  
295 use Venus::Match;
296  
297 my $match = Venus::Match->new(5);
298  
299 $match->when(sub{$_ < 5})->then(sub{"< 5"});
300 $match->when(sub{$_ > 5})->then(sub{"> 5"});
301  
302 $match->none(sub{"?"});
303  
304 my $result = $match->result;
305  
306 # "?"
307  
308 =cut
309  
310 =head1 DESCRIPTION
311  
312 This package provides an object-oriented interface for complex pattern matching
313 operations on scalar values. See L<Venus::Gather> for operating on collections
314 of data, e.g. array references.
315  
316 =cut
317  
318 =head1 ATTRIBUTES
319  
320 This package has the following attributes:
321  
322 =cut
323  
324 =head2 on_none
325  
326 on_none(CodeRef)
327  
328 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{}>.
329  
330 =cut
331  
332 =head2 on_only
333  
334 on_only(CodeRef)
335  
336 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{1}>.
337  
338 =cut
339  
340 =head2 on_then
341  
342 on_then(ArrayRef[CodeRef])
343  
344 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
345  
346 =cut
347  
348 =head2 on_when
349  
350 on_when(ArrayRef[CodeRef])
351  
352 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
353  
354 =cut
355  
356 =head1 INHERITS
357  
358 This package inherits behaviors from:
359  
360 L<Venus::Kind::Utility>
361  
362 =cut
363  
364 =head1 INTEGRATES
365  
366 This package integrates behaviors from:
367  
368 L<Venus::Role::Accessible>
369  
370 L<Venus::Role::Buildable>
371  
372 L<Venus::Role::Valuable>
373  
374 =cut
375  
376 =head1 METHODS
377  
378 This package provides the following methods:
379  
380 =cut
381  
382 =head2 boolean
383  
384 boolean() (Venus::Match)
385  
386 The boolean method registers a L</then> condition which returns C<true> if the
387 item is matched, and registers a L</none> condition which returns C<false> if
388 there are no matches.
389  
390 I<Since C<4.15>>
391  
392 =over 4
393  
394 =item boolean example 1
395  
396 package main;
397  
398 use Venus::Match;
399  
400 my $match = Venus::Match->new(5);
401  
402 $match->just(5)->boolean;
403  
404 my $result = $match->result;
405  
406 # true
407  
408 =back
409  
410 =over 4
411  
412 =item boolean example 2
413  
414 package main;
415  
416 use Venus::Match;
417  
418 my $match = Venus::Match->new(6);
419  
420 $match->just(5)->boolean;
421  
422 my $result = $match->result;
423  
424 # false
425  
426 =back
427  
428 =over 4
429  
430 =item boolean example 3
431  
432 package main;
433  
434 use Venus::Match;
435  
436 my $match = Venus::Match->new(6);
437  
438 $match->just(5)->boolean;
439 $match->just(6)->boolean;
440  
441 my $result = $match->result;
442  
443 # true
444  
445 =back
446  
447 =cut
448  
449 =head2 clear
450  
451 clear() (Venus::Match)
452  
453 The clear method resets all match conditions and returns the invocant.
454  
455 I<Since C<1.23>>
456  
457 =over 4
458  
459 =item clear example 1
460  
461 # given: synopsis
462  
463 package main;
464  
465 my $clear = $match->clear;
466  
467 # bless(..., "Venus::Match")
468  
469 =back
470  
471 =cut
472  
473 =head2 data
474  
475 data(hashref $data) (Venus::Match)
476  
477 The data method takes a hashref (i.e. lookup table) and creates match
478 conditions and actions based on the keys and values found.
479  
480 I<Since C<0.07>>
481  
482 =over 4
483  
484 =item data example 1
485  
486 package main;
487  
488 use Venus::Match;
489  
490 my $match = Venus::Match->new('a');
491  
492 $match->data({
493 'a' => 'b',
494 'c' => 'd',
495 'e' => 'f',
496 'g' => 'h',
497 });
498  
499 my $result = $match->none('z')->result;
500  
501 # "b"
502  
503 =back
504  
505 =over 4
506  
507 =item data example 2
508  
509 package main;
510  
511 use Venus::Match;
512  
513 my $match = Venus::Match->new('x');
514  
515 $match->data({
516 'a' => 'b',
517 'c' => 'd',
518 'e' => 'f',
519 'g' => 'h',
520 });
521  
522 my $result = $match->none('z')->result;
523  
524 # "z"
525  
526 =back
527  
528 =cut
529  
530 =head2 defined
531  
532 defined() (Venus::Match)
533  
534 The defined method registers an L</only> condition which only allows matching
535 if the value presented is C<defined>.
536  
537 I<Since C<4.15>>
538  
539 =over 4
540  
541 =item defined example 1
542  
543 package main;
544  
545 use Venus::Match;
546  
547 my $match = Venus::Match->new->defined;
548  
549 $match->expr(qr/.*/)->then('okay');
550  
551 my $result = $match->result;
552  
553 # undef
554  
555 =back
556  
557 =over 4
558  
559 =item defined example 2
560  
561 package main;
562  
563 use Venus::Match;
564  
565 my $match = Venus::Match->new->defined;
566  
567 $match->expr(qr/.*/)->then('okay');
568  
569 my $result = $match->result('');
570  
571 # "okay"
572  
573 =back
574  
575 =cut
576  
577 =head2 expr
578  
579 expr(string | regexp $expr) (Venus::Match)
580  
581 The expr method registers a L</when> condition that check if the match value is
582 an exact string match of the C<$topic> if the topic is a string, or that it
583 matches against the topic if the topic is a regular expression.
584  
585 I<Since C<0.07>>
586  
587 =over 4
588  
589 =item expr example 1
590  
591 package main;
592  
593 use Venus::Match;
594  
595 my $match = Venus::Match->new('1901-01-01');
596  
597 $match->expr('1901-01-01')->then(sub{[split /-/]});
598  
599 my $result = $match->result;
600  
601 # ["1901", "01", "01"]
602  
603 =back
604  
605 =over 4
606  
607 =item expr example 2
608  
609 package main;
610  
611 use Venus::Match;
612  
613 my $match = Venus::Match->new('1901-01-01');
614  
615 $match->expr(qr/^1901-/)->then(sub{[split /-/]});
616  
617 my $result = $match->result;
618  
619 # ["1901", "01", "01"]
620  
621 =back
622  
623 =cut
624  
625 =head2 just
626  
627 just(string $topic) (Venus::Match)
628  
629 The just method registers a L</when> condition that check if the match value is
630 an exact string match of the C<$topic> provided.
631  
632 I<Since C<0.03>>
633  
634 =over 4
635  
636 =item just example 1
637  
638 package main;
639  
640 use Venus::Match;
641  
642 my $match = Venus::Match->new('a');
643  
644 $match->just('a')->then('a');
645 $match->just('b')->then('b');
646 $match->just('c')->then('c');
647  
648 my $result = $match->result;
649  
650 # "a"
651  
652 =back
653  
654 =over 4
655  
656 =item just example 2
657  
658 package main;
659  
660 use Venus::Match;
661 use Venus::String;
662  
663 my $match = Venus::Match->new(Venus::String->new('a'));
664  
665 $match->just('a')->then('a');
666 $match->just('b')->then('b');
667 $match->just('c')->then('c');
668  
669 my $result = $match->result;
670  
671 # "a"
672  
673 =back
674  
675 =over 4
676  
677 =item just example 3
678  
679 package main;
680  
681 use Venus::Match;
682 use Venus::String;
683  
684 my $match = Venus::Match->new(Venus::String->new('c'));
685  
686 $match->just('a')->then('a');
687 $match->just('b')->then('b');
688 $match->just('c')->then('c');
689  
690 my $result = $match->result;
691  
692 # "c"
693  
694 =back
695  
696 =over 4
697  
698 =item just example 4
699  
700 package main;
701  
702 use Venus::Match;
703  
704 my $match = Venus::Match->new(1.23);
705  
706 $match->just('1.230')->then('1.230');
707 $match->just(01.23)->then('123');
708 $match->just(1.230)->then(1.23);
709  
710 my $result = $match->result;
711  
712 # "1.23"
713  
714 =back
715  
716 =over 4
717  
718 =item just example 5
719  
720 package main;
721  
722 use Venus::Match;
723 use Venus::Number;
724  
725 my $match = Venus::Match->new(Venus::Number->new(1.23));
726  
727 $match->just('1.230')->then('1.230');
728 $match->just(01.23)->then('123');
729 $match->just(1.230)->then(1.23);
730  
731 my $result = $match->result;
732  
733 # "1.23"
734  
735 =back
736  
737 =over 4
738  
739 =item just example 6
740  
741 package main;
742  
743 use Venus::Match;
744 use Venus::Number;
745  
746 my $match = Venus::Match->new(1.23);
747  
748 $match->just(Venus::Number->new('1.230'))->then('1.230');
749 $match->just(Venus::Number->new(01.23))->then('123');
750 $match->just(Venus::Number->new(1.230))->then(1.23);
751  
752 my $result = $match->result;
753  
754 # "1.23"
755  
756 =back
757  
758 =cut
759  
760 =head2 new
761  
762 new(any @args) (Venus::Match)
763  
764 The new method constructs an instance of the package.
765  
766 I<Since C<4.15>>
767  
768 =over 4
769  
770 =item new example 1
771  
772 package main;
773  
774 use Venus::Match;
775  
776 my $new = Venus::Match->new;
777  
778 # bless(..., "Venus::Match")
779  
780 =back
781  
782 =over 4
783  
784 =item new example 2
785  
786 package main;
787  
788 use Venus::Match;
789  
790 my $new = Venus::Match->new(5);
791  
792 # bless(..., "Venus::Match")
793  
794 =back
795  
796 =over 4
797  
798 =item new example 3
799  
800 package main;
801  
802 use Venus::Match;
803  
804 my $new = Venus::Match->new(value => 5);
805  
806 # bless(..., "Venus::Match")
807  
808 =back
809  
810 =cut
811  
812 =head2 none
813  
814 none(any | coderef $code) (Venus::Match)
815  
816 The none method registers a special condition that returns a result only when
817 no other conditions have been matched.
818  
819 I<Since C<0.03>>
820  
821 =over 4
822  
823 =item none example 1
824  
825 package main;
826  
827 use Venus::Match;
828  
829 my $match = Venus::Match->new('z');
830  
831 $match->just('a')->then('a');
832 $match->just('b')->then('b');
833 $match->just('c')->then('c');
834  
835 $match->none('z');
836  
837 my $result = $match->result;
838  
839 # "z"
840  
841 =back
842  
843 =over 4
844  
845 =item none example 2
846  
847 package main;
848  
849 use Venus::Match;
850  
851 my $match = Venus::Match->new('z');
852  
853 $match->just('a')->then('a');
854 $match->just('b')->then('b');
855 $match->just('c')->then('c');
856  
857 $match->none(sub{"($_) not found"});
858  
859 my $result = $match->result;
860  
861 # "(z) not found"
862  
863 =back
864  
865 =cut
866  
867 =head2 object
868  
869 object() (Venus::Match)
870  
871 The object method registers an L</only> condition which only allows matching if
872 the value presented is an object.
873  
874 I<Since C<4.15>>
875  
876 =over 4
877  
878 =item object example 1
879  
880 package main;
881  
882 use Venus::Match;
883  
884 my $match = Venus::Match->new->object;
885  
886 $match->when('isa', 'main')->then('okay');
887  
888 my $result = $match->result;
889  
890 # undef
891  
892 =back
893  
894 =over 4
895  
896 =item object example 2
897  
898 package main;
899  
900 use Venus::Match;
901  
902 my $match = Venus::Match->new->object;
903  
904 $match->when('isa', 'main')->then('okay');
905  
906 my $result = $match->result(bless{});
907  
908 # "okay"
909  
910 =back
911  
912 =cut
913  
914 =head2 only
915  
916 only(coderef $code) (Venus::Match)
917  
918 The only method registers a special condition that only allows matching on the
919 match value only if the code provided returns truthy.
920  
921 I<Since C<0.03>>
922  
923 =over 4
924  
925 =item only example 1
926  
927 package main;
928  
929 use Venus::Match;
930  
931 my $match = Venus::Match->new(5);
932  
933 $match->only(sub{$_ != 5});
934  
935 $match->just(5)->then(5);
936 $match->just(6)->then(6);
937  
938 my $result = $match->result;
939  
940 # undef
941  
942 =back
943  
944 =over 4
945  
946 =item only example 2
947  
948 package main;
949  
950 use Venus::Match;
951  
952 my $match = Venus::Match->new(6);
953  
954 $match->only(sub{$_ != 5});
955  
956 $match->just(5)->then(5);
957 $match->just(6)->then(6);
958  
959 my $result = $match->result;
960  
961 # 6
962  
963 =back
964  
965 =cut
966  
967 =head2 result
968  
969 result(any $data) (any)
970  
971 The result method evaluates the registered conditions and returns the result of
972 the action (i.e. the L</then> code) or the special L</none> condition if there
973 were no matches. In list context, this method returns both the result and
974 whether or not a condition matched. Optionally, when passed an argument this
975 method assign the argument as the value/topic and then perform the operation.
976  
977 I<Since C<0.03>>
978  
979 =over 4
980  
981 =item result example 1
982  
983 package main;
984  
985 use Venus::Match;
986  
987 my $match = Venus::Match->new('a');
988  
989 $match->just('a')->then('a');
990 $match->just('b')->then('b');
991 $match->just('c')->then('c');
992  
993 my $result = $match->result;
994  
995 # "a"
996  
997 =back
998  
999 =over 4
1000  
1001 =item result example 2
1002  
1003 package main;
1004  
1005 use Venus::Match;
1006  
1007 my $match = Venus::Match->new('a');
1008  
1009 $match->just('a')->then('a');
1010 $match->just('b')->then('b');
1011 $match->just('c')->then('c');
1012  
1013 my ($result, $matched) = $match->result;
1014  
1015 # ("a", 1)
1016  
1017 =back
1018  
1019 =over 4
1020  
1021 =item result example 3
1022  
1023 package main;
1024  
1025 use Venus::Match;
1026  
1027 sub fibonacci {
1028 my ($n) = @_;
1029 my $match = Venus::Match->new($n)
1030 ->just(1)->then(1)
1031 ->just(2)->then(1)
1032 ->none(sub{fibonacci($n - 1) + fibonacci($n - 2)})
1033 ->result
1034 }
1035  
1036 my $result = [fibonacci(4), fibonacci(6), fibonacci(12)]
1037  
1038 # [3, 8, 144]
1039  
1040 =back
1041  
1042 =over 4
1043  
1044 =item result example 4
1045  
1046 package main;
1047  
1048 use Venus::Match;
1049  
1050 my $match = Venus::Match->new('a');
1051  
1052 $match->just('a')->then('a');
1053 $match->just('b')->then('b');
1054 $match->just('c')->then('c');
1055  
1056 my $result = $match->result('b');
1057  
1058 # "b"
1059  
1060 =back
1061  
1062 =over 4
1063  
1064 =item result example 5
1065  
1066 package main;
1067  
1068 use Venus::Match;
1069  
1070 my $match = Venus::Match->new('a');
1071  
1072 $match->just('a')->then('a');
1073 $match->just('b')->then('b');
1074 $match->just('c')->then('c');
1075  
1076 my $result = $match->result('z');
1077  
1078 # undef
1079  
1080 =back
1081  
1082 =cut
1083  
1084 =head2 take
1085  
1086 take() (Venus::Match)
1087  
1088 The take method registers a L</then> condition which returns (i.e. takes) the
1089 matched item as-is, and registers a L</none> condition which returns C<undef>
1090 if there are no matches.
1091  
1092 I<Since C<4.15>>
1093  
1094 =over 4
1095  
1096 =item take example 1
1097  
1098 package main;
1099  
1100 use Venus::Match;
1101  
1102 my $match = Venus::Match->new(5);
1103  
1104 $match->just(5)->take;
1105  
1106 my $result = $match->result;
1107  
1108 # 5
1109  
1110 =back
1111  
1112 =over 4
1113  
1114 =item take example 2
1115  
1116 package main;
1117  
1118 use Venus::Match;
1119  
1120 my $match = Venus::Match->new(6);
1121  
1122 $match->just(5)->take;
1123  
1124 my $result = $match->result;
1125  
1126 # undef
1127  
1128 =back
1129  
1130 =over 4
1131  
1132 =item take example 3
1133  
1134 package main;
1135  
1136 use Venus::Match;
1137  
1138 my $match = Venus::Match->new(6);
1139  
1140 $match->just(5)->take;
1141 $match->just(6)->take;
1142  
1143 my $result = $match->result;
1144  
1145 # 6
1146  
1147 =back
1148  
1149 =cut
1150  
1151 =head2 test
1152  
1153 test() (boolean)
1154  
1155 The test method evaluates the registered conditions and returns truthy if a
1156 match can be made, without executing any of the actions (i.e. the L</then>
1157 code) or the special L</none> condition.
1158  
1159 I<Since C<1.02>>
1160  
1161 =over 4
1162  
1163 =item test example 1
1164  
1165 package Match;
1166  
1167 use Venus::Match;
1168  
1169 our $TEST = 0;
1170  
1171 my $match = Venus::Match->new('a');
1172  
1173 $match->just('a')->then(sub{$TEST = 1});
1174 $match->just('b')->then(sub{$TEST = 2});
1175 $match->just('c')->then(sub{$TEST = 3});
1176  
1177 my $test = $match->test;
1178  
1179 # 1
1180  
1181 =back
1182  
1183 =over 4
1184  
1185 =item test example 2
1186  
1187 package Match;
1188  
1189 use Venus::Match;
1190  
1191 our $TEST = 0;
1192  
1193 my $match = Venus::Match->new('b');
1194  
1195 $match->just('a')->then(sub{$TEST = 1});
1196 $match->just('b')->then(sub{$TEST = 2});
1197 $match->just('c')->then(sub{$TEST = 3});
1198  
1199 my $test = $match->test;
1200  
1201 # 1
1202  
1203 =back
1204  
1205 =over 4
1206  
1207 =item test example 3
1208  
1209 package Match;
1210  
1211 use Venus::Match;
1212  
1213 our $TEST = 0;
1214  
1215 my $match = Venus::Match->new('z');
1216  
1217 $match->just('a')->then(sub{$TEST = 1});
1218 $match->just('b')->then(sub{$TEST = 2});
1219 $match->just('c')->then(sub{$TEST = 3});
1220  
1221 my $test = $match->test;
1222  
1223 # 0
1224  
1225 =back
1226  
1227 =cut
1228  
1229 =head2 then
1230  
1231 then(any | coderef $code) (Venus::Match)
1232  
1233 The then method registers an action to be executed if the corresponding match
1234 condition returns truthy.
1235  
1236 I<Since C<0.03>>
1237  
1238 =over 4
1239  
1240 =item then example 1
1241  
1242 package main;
1243  
1244 use Venus::Match;
1245  
1246 my $match = Venus::Match->new('b');
1247  
1248 $match->just('a');
1249 $match->then('a');
1250  
1251 $match->just('b');
1252 $match->then('b');
1253  
1254 my $result = $match->result;
1255  
1256 # "b"
1257  
1258 =back
1259  
1260 =over 4
1261  
1262 =item then example 2
1263  
1264 package main;
1265  
1266 use Venus::Match;
1267  
1268 my $match = Venus::Match->new('b');
1269  
1270 $match->just('a');
1271 $match->then('a');
1272  
1273 $match->just('b');
1274 $match->then('b');
1275 $match->then('x');
1276  
1277 my $result = $match->result;
1278  
1279 # "x"
1280  
1281 =back
1282  
1283 =cut
1284  
1285 =head2 type
1286  
1287 type(string $expr) (Venus::Match)
1288  
1289 The type method accepts a L<"type expression"|Venus::Type> and registers a
1290 L</when> condition which matches values conforming to the type expression
1291 specified.
1292  
1293 I<Since C<4.15>>
1294  
1295 =over 4
1296  
1297 =item type example 1
1298  
1299 package main;
1300  
1301 use Venus::Match;
1302  
1303 my $match = Venus::Match->new;
1304  
1305 $match->type('string')->then('okay');
1306  
1307 my $result = $match->result;
1308  
1309 # undef
1310  
1311 =back
1312  
1313 =over 4
1314  
1315 =item type example 2
1316  
1317 package main;
1318  
1319 use Venus::Match;
1320  
1321 my $match = Venus::Match->new;
1322  
1323 $match->type('string')->then('okay');
1324  
1325 my $result = $match->result('hello');
1326  
1327 # "okay"
1328  
1329 =back
1330  
1331 =cut
1332  
1333 =head2 when
1334  
1335 when(string | coderef $code, any @args) (Venus::Match)
1336  
1337 The when method registers a match condition that will be passed the match value
1338 during evaluation. If the match condition returns truthy the corresponding
1339 action will be used to return a result. If the match value is an object, this
1340 method can take a method name and arguments which will be used as a match
1341 condition.
1342  
1343 I<Since C<0.03>>
1344  
1345 =over 4
1346  
1347 =item when example 1
1348  
1349 package main;
1350  
1351 use Venus::Match;
1352  
1353 my $match = Venus::Match->new('a');
1354  
1355 $match->when(sub{$_ eq 'a'});
1356 $match->then('a');
1357  
1358 $match->when(sub{$_ eq 'b'});
1359 $match->then('b');
1360  
1361 $match->when(sub{$_ eq 'c'});
1362 $match->then('c');
1363  
1364 my $result = $match->result;
1365  
1366 # "a"
1367  
1368 =back
1369  
1370 =over 4
1371  
1372 =item when example 2
1373  
1374 package main;
1375  
1376 use Venus::Match;
1377 use Venus::What;
1378  
1379 my $match = Venus::Match->new(Venus::What->new(1)->deduce);
1380  
1381 $match->when('isa', 'Venus::Number');
1382 $match->then('Venus::Number');
1383  
1384 $match->when('isa', 'Venus::String');
1385 $match->then('Venus::String');
1386  
1387 my $result = $match->result;
1388  
1389 # "Venus::Number"
1390  
1391 =back
1392  
1393 =over 4
1394  
1395 =item when example 3
1396  
1397 package main;
1398  
1399 use Venus::Match;
1400 use Venus::What;
1401  
1402 my $match = Venus::Match->new(Venus::What->new('1')->deduce);
1403  
1404 $match->when('isa', 'Venus::Number');
1405 $match->then('Venus::Number');
1406  
1407 $match->when('isa', 'Venus::String');
1408 $match->then('Venus::String');
1409  
1410 my $result = $match->result;
1411  
1412 # "Venus::String"
1413  
1414 =back
1415  
1416 =cut
1417  
1418 =head2 where
1419  
1420 where() (Venus::Match)
1421  
1422 The where method registers an action as a sub-match operation, to be executed
1423 if the corresponding match condition returns truthy. This method returns the
1424 sub-match object.
1425  
1426 I<Since C<1.40>>
1427  
1428 =over 4
1429  
1430 =item where example 1
1431  
1432 package main;
1433  
1434 use Venus::Match;
1435  
1436 my $match = Venus::Match->new;
1437  
1438 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
1439  
1440 $submatch1->just('peach')->then('peach-123');
1441 $submatch1->just('patch')->then('patch-456');
1442 $submatch1->just('punch')->then('punch-789');
1443  
1444 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
1445  
1446 $submatch2->just('merch')->then('merch-123');
1447 $submatch2->just('march')->then('march-456');
1448 $submatch2->just('mouch')->then('mouch-789');
1449  
1450 my $result = $match->result('peach');
1451  
1452 # "peach-123"
1453  
1454 =back
1455  
1456 =over 4
1457  
1458 =item where example 2
1459  
1460 package main;
1461  
1462 use Venus::Match;
1463  
1464 my $match = Venus::Match->new;
1465  
1466 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
1467  
1468 $submatch1->just('peach')->then('peach-123');
1469 $submatch1->just('patch')->then('patch-456');
1470 $submatch1->just('punch')->then('punch-789');
1471  
1472 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
1473  
1474 $submatch2->just('merch')->then('merch-123');
1475 $submatch2->just('march')->then('march-456');
1476 $submatch2->just('mouch')->then('mouch-789');
1477  
1478 my $result = $match->result('march');
1479  
1480 # "march-456"
1481  
1482 =back
1483  
1484 =over 4
1485  
1486 =item where example 3
1487  
1488 package main;
1489  
1490 use Venus::Match;
1491  
1492 my $match = Venus::Match->new;
1493  
1494 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
1495  
1496 $submatch1->just('peach')->then('peach-123');
1497 $submatch1->just('patch')->then('patch-456');
1498 $submatch1->just('punch')->then('punch-789');
1499  
1500 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
1501  
1502 $submatch2->just('merch')->then('merch-123');
1503 $submatch2->just('march')->then('march-456');
1504 $submatch2->just('mouch')->then('mouch-789');
1505  
1506 my $result = $match->result('pirch');
1507  
1508 # undef
1509  
1510 =back
1511  
1512 =cut
1513  
1514 =head2 yesno
1515  
1516 yesno() (Venus::Match)
1517  
1518 The yesno method registers a L</then> condition which returns C<"yes"> if the
1519 item is matched, and registers a L</none> condition which returns C<"no"> if
1520 there are no matches.
1521  
1522 I<Since C<4.15>>
1523  
1524 =over 4
1525  
1526 =item yesno example 1
1527  
1528 package main;
1529  
1530 use Venus::Match;
1531  
1532 my $match = Venus::Match->new(5);
1533  
1534 $match->just(5)->yesno;
1535  
1536 my $result = $match->result;
1537  
1538 # "yes"
1539  
1540 =back
1541  
1542 =over 4
1543  
1544 =item yesno example 2
1545  
1546 package main;
1547  
1548 use Venus::Match;
1549  
1550 my $match = Venus::Match->new(6);
1551  
1552 $match->just(5)->yesno;
1553  
1554 my $result = $match->result;
1555  
1556 # "no"
1557  
1558 =back
1559  
1560 =over 4
1561  
1562 =item yesno example 3
1563  
1564 package main;
1565  
1566 use Venus::Match;
1567  
1568 my $match = Venus::Match->new(6);
1569  
1570 $match->just(5)->yesno;
1571 $match->just(6)->yesno;
1572  
1573 my $result = $match->result;
1574  
1575 # "yes"
1576  
1577 =back
1578  
1579 =cut
1580  
1581 =head1 AUTHORS
1582  
1583 Awncorp, C<awncorp@cpan.org>
1584  
1585 =cut
1586  
1587 =head1 LICENSE
1588  
1589 Copyright (C) 2022, Awncorp, C<awncorp@cpan.org>.
1590  
1591 This program is free software, you can redistribute it and/or modify it under
1592 the terms of the Apache license version 2.0.
1593  
1594 =cut
« blib/lib/Venus/Json.pm ↑ Summary blib/lib/Venus/Random.pm »