Venus/Match.pm

filter: Statementstmt 92.7% 90 / 97 Branchbran 71.7% 33 / 46 Conditioncond 53.3% 8 / 15 MC/DCmcdc 10.0% 1 / 10 Subroutinesub 95.2% 20 / 21 Podpod 88.8% 8 / 9 totaltotal 80.8% 160 / 198 CC 29 SCAR 18.4
CC
29
Cov
83%
__ANON__
15.6
__ANON__
8.8
result
7.0
CRAP 6.3
« Venus/Space.pm ↑ Summary Venus/Template.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 3 ▶use Venus::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 base 'Venus::Kind::Utility';
11  
12 3 with 'Venus::Role::Accessible';
13  
14 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 # ATTRIBUTES
17  
18 3 attr 'on_none';
19 3 attr 'on_only';
20 3 attr 'on_then';
21 3 attr 'on_when';
22  
23 # BUILDERS
24  
25 sub build_self {
26 334 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=5 SCAR=17.0
Pod undocumented
27  
28 334 ▶ $self->on_none(sub{}) if !$self->on_none;
Subroutine __ANON__: not called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_none 334 0 334
29 334 ▶ $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 334 0 334
30 334 ▶ $self->on_then([]) if !$self->on_then;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_then 334 0 334
31 334 ▶ $self->on_when([]) if !$self->on_when;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_when 334 0 334
32  
33 334 return $self;
34 }
35  
36 # METHODS
37  
38 sub data {
39 2 ▶ my ($self, $data) = @_;
Subroutine data: called CC=2 SCAR=6.9
Pod documented
40  
41 2 ▶ while(my($key, $value) = each(%$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 8 2 10
42 8 $self->just($key)->then($value);
43 }
44  
45 2 return $self;
46 }
47  
48 sub expr {
49 2 ▶ my ($self, $topic) = @_;
Subroutine expr: called CC=1 SCAR=0.6
Pod documented
50  
51 $self->when(sub{
52 2 ▶ my $value = $self->value;
Subroutine __ANON__: called CC=9 SCAR=27.5
53  
54 2 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 2 2
55 0 return 0;
56 }
57 2 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 2 2
Condition33%1/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
200
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
58 0 return 0;
59 }
60 2 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 2 2
Condition33%1/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
020
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
61 0 return 0;
62 }
63 2 ▶ if (ref($topic) eq 'Regexp' && "$value" =~ qr/$topic/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $topic eq 'Regexp' and "$value" =~ qr/$topic/u) { } 1 1 2
elsif ("$value" eq "$topic") { } 1 0 1
Condition66%2/3
ref $topic eq "Regexp" and "$value" =~ qr/$topic/u
!ll&&!rl&&r
101
MC/DC50%1/2
ref $topic eq "Regexp" and "$value" =~ qr/$topic/u
ref $topic eq "Regexp" "$value" =~ qr/$topic/u
Truth table66%2/3
A and B
A= ref $topic eq "Regexp"B= "$value" =~ qr/$topic/u
ABresult
0X0
100
111
64 1 return 1;
65 }
66 elsif ("$value" eq "$topic") {
67 1 return 1;
68 }
69 else {
70 0 return 0;
71 }
72 2 });
73  
74 2 return $self;
75 }
76  
77 sub just {
78 660 ▶ my ($self, $topic) = @_;
Subroutine just: called CC=1 SCAR=0.3
Pod documented
79  
80 $self->when(sub{
81 591 ▶ my $value = $self->value;
Subroutine __ANON__: called CC=7 SCAR=21.8
82  
83 591 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 591 591
84 0 return 0;
85 }
86 591 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 591 591
Condition66%2/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
58470
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
87 0 return 0;
88 }
89 591 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 591 591
Condition66%2/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
75840
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
90 0 return 0;
91 }
92 591 ▶ if ("$value" eq "$topic") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" eq "$topic") { } 167 424 591
93 167 return 1;
94 }
95 else {
96 424 return 0;
97 }
98 660 });
99  
100 660 return $self;
101 }
102  
103 sub none {
104 313 ▶ my ($self, $code) = @_;
Subroutine none: called CC=2 SCAR=6.9
Pod documented
105  
106 313 ▶ $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') ? : 309 4 313
107  
108 313 return $self;
109 }
110  
111 sub only {
112 2 ▶ my ($self, $code) = @_;
Subroutine only: called CC=1 SCAR=0.0
Pod documented
113  
114 2 $self->on_only($code);
115  
116 2 return $self;
117 }
118  
119 sub result {
120 330 ▶ my ($self) = @_;
Subroutine result: called CC=7 SCAR=19.5
Pod documented
121  
122 330 my $result;
123 330 my $matched = 0;
124  
125 330 my $value = $self->value;
126  
127 330 local $_ = $value;
128  
129 330 ▶ return wantarray ? ($result, $matched) : $result if !$self->on_only->($value);
Branch: 2 branches
BranchTrueFalseTotal
unless $self->on_only->($value) 1 329 330
wantarray ? : 0 1 1
130  
131 329 ▶ for (my $i = 0; $i < @{$self->on_when}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->on_when;}) 600 157 757
132 600 ▶ if ($self->on_when->[$i]->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_when->[$i]->($value)) 172 428 600
133 172 $result = $self->on_then->[$i]->($value);
134 172 $matched++;
135 172 last;
136 }
137 }
138  
139 329 ▶ if (!$matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 157 172 329
140 157 local $_ = $value;
141 157 $result = $self->on_none->($value);
142 }
143  
144 329 ▶ return wantarray ? ($result, $matched) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 328 329
145 }
146  
147 sub then {
148 673 ▶ my ($self, $code) = @_;
Subroutine then: called CC=2 SCAR=6.9
Pod documented
149  
150 673 my $next = $#{$self->on_when};
151  
152 673 ▶ $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') ? : 4 669 673
153  
154 673 return $self;
155 }
156  
157 sub when {
158 672 ▶ my ($self, $code, @args) = @_;
Subroutine when: called CC=2 SCAR=6.9
Pod documented
159  
160 672 ▶ 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 672 672
161  
162 $self->on_when->[$next] = sub {
163 600 ▶ local $_ = $self->value;
Subroutine __ANON__: called CC=1 SCAR=0.0
164 600 $self->value->$code(@args);
165 672 };
166  
167 672 return $self;
168 }
169  
170 3 1;
171  
172  
173  
174 =head1 NAME
175  
176 Venus::Match - Match Class
177  
178 =cut
179  
180 =head1 ABSTRACT
181  
182 Match Class for Perl 5
183  
184 =cut
185  
186 =head1 SYNOPSIS
187  
188 package main;
189  
190 use Venus::Match;
191  
192 my $match = Venus::Match->new(5);
193  
194 $match->when(sub{$_ < 5})->then(sub{"< 5"});
195 $match->when(sub{$_ > 5})->then(sub{"> 5"});
196  
197 $match->none(sub{"?"});
198  
199 my $result = $match->result;
200  
201 # "?"
202  
203 =cut
204  
205 =head1 DESCRIPTION
206  
207 This package provides an object-oriented interface for complex pattern matching
208 operations.
209  
210 =cut
211  
212 =head1 ATTRIBUTES
213  
214 This package has the following attributes:
215  
216 =cut
217  
218 =head2 on_none
219  
220 on_none(CodeRef)
221  
222 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{}>.
223  
224 =cut
225  
226 =head2 on_only
227  
228 on_only(CodeRef)
229  
230 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{1}>.
231  
232 =cut
233  
234 =head2 on_then
235  
236 on_then(ArrayRef[CodeRef])
237  
238 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
239  
240 =cut
241  
242 =head2 on_when
243  
244 on_when(ArrayRef[CodeRef])
245  
246 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
247  
248 =cut
249  
250 =head1 INHERITS
251  
252 This package inherits behaviors from:
253  
254 L<Venus::Kind::Utility>
255  
256 =cut
257  
258 =head1 INTEGRATES
259  
260 This package integrates behaviors from:
261  
262 L<Venus::Role::Accessible>
263  
264 =cut
265  
266 =head1 METHODS
267  
268 This package provides the following methods:
269  
270 =cut
271  
272 =head2 data
273  
274 data(HashRef $data) (Match)
275  
276 The data method takes a hashref (i.e. lookup table) and match conditions and
277 actions based on the keys and values found.
278  
279 I<Since C<0.07>>
280  
281 =over 4
282  
283 =item data example 1
284  
285 package main;
286  
287 use Venus::Match;
288  
289 my $match = Venus::Match->new('a');
290  
291 $match->data({
292 'a' => 'b',
293 'c' => 'd',
294 'e' => 'f',
295 'g' => 'h',
296 });
297  
298 my $result = $match->none('z')->result;
299  
300 # "b"
301  
302 =back
303  
304 =over 4
305  
306 =item data example 2
307  
308 package main;
309  
310 use Venus::Match;
311  
312 my $match = Venus::Match->new('x');
313  
314 $match->data({
315 'a' => 'b',
316 'c' => 'd',
317 'e' => 'f',
318 'g' => 'h',
319 });
320  
321 my $result = $match->none('z')->result;
322  
323 # "z"
324  
325 =back
326  
327 =cut
328  
329 =head2 expr
330  
331 expr(Str | RegexpRef $expr) (Match)
332  
333 The expr method registers a L</when> condition that check if the match value is
334 an exact string match of the C<$topic> if the topic is a string, or that it
335 matches against the topic if the topic is a regular expression.
336  
337 I<Since C<0.07>>
338  
339 =over 4
340  
341 =item expr example 1
342  
343 package main;
344  
345 use Venus::Match;
346  
347 my $match = Venus::Match->new('1901-01-01');
348  
349 $match->expr('1901-01-01')->then(sub{[split /-/]});
350  
351 my $result = $match->result;
352  
353 # ["1901", "01", "01"]
354  
355 =back
356  
357 =over 4
358  
359 =item expr example 2
360  
361 package main;
362  
363 use Venus::Match;
364  
365 my $match = Venus::Match->new('1901-01-01');
366  
367 $match->expr(qr/^1901-/)->then(sub{[split /-/]});
368  
369 my $result = $match->result;
370  
371 # ["1901", "01", "01"]
372  
373 =back
374  
375 =cut
376  
377 =head2 just
378  
379 just(Str $topic) (Match)
380  
381 The just method registers a L</when> condition that check if the match value is
382 an exact string match of the C<$topic> provided.
383  
384 I<Since C<0.03>>
385  
386 =over 4
387  
388 =item just example 1
389  
390 package main;
391  
392 use Venus::Match;
393  
394 my $match = Venus::Match->new('a');
395  
396 $match->just('a')->then('a');
397 $match->just('b')->then('b');
398 $match->just('c')->then('c');
399  
400 my $result = $match->result;
401  
402 # "a"
403  
404 =back
405  
406 =over 4
407  
408 =item just example 2
409  
410 package main;
411  
412 use Venus::Match;
413 use Venus::String;
414  
415 my $match = Venus::Match->new(Venus::String->new('a'));
416  
417 $match->just('a')->then('a');
418 $match->just('b')->then('b');
419 $match->just('c')->then('c');
420  
421 my $result = $match->result;
422  
423 # "a"
424  
425 =back
426  
427 =over 4
428  
429 =item just example 3
430  
431 package main;
432  
433 use Venus::Match;
434 use Venus::String;
435  
436 my $match = Venus::Match->new(Venus::String->new('c'));
437  
438 $match->just('a')->then('a');
439 $match->just('b')->then('b');
440 $match->just('c')->then('c');
441  
442 my $result = $match->result;
443  
444 # "c"
445  
446 =back
447  
448 =over 4
449  
450 =item just example 4
451  
452 package main;
453  
454 use Venus::Match;
455  
456 my $match = Venus::Match->new(1.23);
457  
458 $match->just('1.230')->then('1.230');
459 $match->just(01.23)->then('123');
460 $match->just(1.230)->then(1.23);
461  
462 my $result = $match->result;
463  
464 # "1.23"
465  
466 =back
467  
468 =over 4
469  
470 =item just example 5
471  
472 package main;
473  
474 use Venus::Match;
475 use Venus::Number;
476  
477 my $match = Venus::Match->new(Venus::Number->new(1.23));
478  
479 $match->just('1.230')->then('1.230');
480 $match->just(01.23)->then('123');
481 $match->just(1.230)->then(1.23);
482  
483 my $result = $match->result;
484  
485 # "1.23"
486  
487 =back
488  
489 =over 4
490  
491 =item just example 6
492  
493 package main;
494  
495 use Venus::Match;
496 use Venus::Number;
497  
498 my $match = Venus::Match->new(1.23);
499  
500 $match->just(Venus::Number->new('1.230'))->then('1.230');
501 $match->just(Venus::Number->new(01.23))->then('123');
502 $match->just(Venus::Number->new(1.230))->then(1.23);
503  
504 my $result = $match->result;
505  
506 # "1.23"
507  
508 =back
509  
510 =cut
511  
512 =head2 none
513  
514 none(CodeRef $code) (Match)
515  
516 The none method registers a special condition that returns a result only when
517 no other conditions have been matched.
518  
519 I<Since C<0.03>>
520  
521 =over 4
522  
523 =item none example 1
524  
525 package main;
526  
527 use Venus::Match;
528  
529 my $match = Venus::Match->new('z');
530  
531 $match->just('a')->then('a');
532 $match->just('b')->then('b');
533 $match->just('c')->then('c');
534  
535 $match->none('z');
536  
537 my $result = $match->result;
538  
539 # "z"
540  
541 =back
542  
543 =over 4
544  
545 =item none example 2
546  
547 package main;
548  
549 use Venus::Match;
550  
551 my $match = Venus::Match->new('z');
552  
553 $match->just('a')->then('a');
554 $match->just('b')->then('b');
555 $match->just('c')->then('c');
556  
557 $match->none(sub{"($_) not found"});
558  
559 my $result = $match->result;
560  
561 # "(z) not found"
562  
563 =back
564  
565 =cut
566  
567 =head2 only
568  
569 only(CodeRef $code) (Match)
570  
571 The only method registers a special condition that only allows matching on the
572 match value only if the code provided returns truthy.
573  
574 I<Since C<0.03>>
575  
576 =over 4
577  
578 =item only example 1
579  
580 package main;
581  
582 use Venus::Match;
583  
584 my $match = Venus::Match->new(5);
585  
586 $match->only(sub{$_ != 5});
587  
588 $match->just(5)->then(5);
589 $match->just(6)->then(6);
590  
591 my $result = $match->result;
592  
593 # undef
594  
595 =back
596  
597 =over 4
598  
599 =item only example 2
600  
601 package main;
602  
603 use Venus::Match;
604  
605 my $match = Venus::Match->new(6);
606  
607 $match->only(sub{$_ != 5});
608  
609 $match->just(5)->then(5);
610 $match->just(6)->then(6);
611  
612 my $result = $match->result;
613  
614 # 6
615  
616 =back
617  
618 =cut
619  
620 =head2 result
621  
622 result() (Any)
623  
624 The result method evaluates the registered conditions and returns the result of
625 the action (i.e. the L</then> code) or the special L</none> condition if there
626 were no matches. In list context, this method returns both the result and
627 whether or not a condition matched.
628  
629 I<Since C<0.03>>
630  
631 =over 4
632  
633 =item result example 1
634  
635 package main;
636  
637 use Venus::Match;
638  
639 my $match = Venus::Match->new('a');
640  
641 $match->just('a')->then('a');
642 $match->just('b')->then('b');
643 $match->just('c')->then('c');
644  
645 my $result = $match->result;
646  
647 # "a"
648  
649 =back
650  
651 =over 4
652  
653 =item result example 2
654  
655 package main;
656  
657 use Venus::Match;
658  
659 my $match = Venus::Match->new('a');
660  
661 $match->just('a')->then('a');
662 $match->just('b')->then('b');
663 $match->just('c')->then('c');
664  
665 my ($result, $matched) = $match->result;
666  
667 # ("a", 1)
668  
669 =back
670  
671 =over 4
672  
673 =item result example 3
674  
675 package main;
676  
677 use Venus::Match;
678  
679 sub fibonacci {
680 my ($n) = @_;
681 my $match = Venus::Match->new($n)
682 ->just(1)->then(1)
683 ->just(2)->then(1)
684 ->none(sub{fibonacci($n - 1) + fibonacci($n - 2)})
685 ->result
686 }
687  
688 my $result = [fibonacci(4), fibonacci(6), fibonacci(12)]
689  
690 # [3, 8, 144]
691  
692 =back
693  
694 =cut
695  
696 =head2 then
697  
698 then(CodeRef $code) (Match)
699  
700 The then method registers an action to be executed if the corresponding match
701 condition returns truthy.
702  
703 I<Since C<0.03>>
704  
705 =over 4
706  
707 =item then example 1
708  
709 package main;
710  
711 use Venus::Match;
712  
713 my $match = Venus::Match->new('b');
714  
715 $match->just('a');
716 $match->then('a');
717  
718 $match->just('b');
719 $match->then('b');
720  
721 my $result = $match->result;
722  
723 # "b"
724  
725 =back
726  
727 =over 4
728  
729 =item then example 2
730  
731 package main;
732  
733 use Venus::Match;
734  
735 my $match = Venus::Match->new('b');
736  
737 $match->just('a');
738 $match->then('a');
739  
740 $match->just('b');
741 $match->then('b');
742 $match->then('x');
743  
744 my $result = $match->result;
745  
746 # "x"
747  
748 =back
749  
750 =cut
751  
752 =head2 when
753  
754 when(Str | CodeRef $code, Any @args) (Match)
755  
756 The when method registers a match condition that will be passed the match value
757 during evaluation. If the match condition returns truthy the corresponding
758 action will be used to return a result. If the match value is an object, this
759 method can take a method name and arguments which will be used as a match
760 condition.
761  
762 I<Since C<0.03>>
763  
764 =over 4
765  
766 =item when example 1
767  
768 package main;
769  
770 use Venus::Match;
771  
772 my $match = Venus::Match->new('a');
773  
774 $match->when(sub{$_ eq 'a'});
775 $match->then('a');
776  
777 $match->when(sub{$_ eq 'b'});
778 $match->then('b');
779  
780 $match->when(sub{$_ eq 'c'});
781 $match->then('c');
782  
783 my $result = $match->result;
784  
785 # "a"
786  
787 =back
788  
789 =over 4
790  
791 =item when example 2
792  
793 package main;
794  
795 use Venus::Match;
796 use Venus::Type;
797  
798 my $match = Venus::Match->new(Venus::Type->new(1)->deduce);
799  
800 $match->when('isa', 'Venus::Number');
801 $match->then('Venus::Number');
802  
803 $match->when('isa', 'Venus::String');
804 $match->then('Venus::String');
805  
806 my $result = $match->result;
807  
808 # "Venus::Number"
809  
810 =back
811  
812 =over 4
813  
814 =item when example 3
815  
816 package main;
817  
818 use Venus::Match;
819 use Venus::Type;
820  
821 my $match = Venus::Match->new(Venus::Type->new('1')->deduce);
822  
823 $match->when('isa', 'Venus::Number');
824 $match->then('Venus::Number');
825  
826 $match->when('isa', 'Venus::String');
827 $match->then('Venus::String');
828  
829 my $result = $match->result;
830  
831 # "Venus::String"
832  
833 =back
834  
835 =cut
« Venus/Space.pm ↑ Summary Venus/Template.pm »