blib/lib/Venus/Match.pm

filter: Statementstmt 95.0% 116 / 122 Branchbran 75.9% 41 / 54 Conditioncond 60.0% 9 / 15 MC/DCmcdc 20.0% 2 / 10 Subroutinesub 100% 27 / 27 Podpod 91.6% 11 / 12 totaltotal 85.8% 206 / 240 SCAR 35.7
CC
33
Cov
87%
__ANON__
12.0
__ANON__
8.8
result
8.0
CRAP 35.4
« blib/lib/Venus/Template.pm ↑ Summary blib/lib/Venus/Unpack.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 'attr', 'base', 'with';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 base 'Venus::Kind::Utility';
11  
12 3 with 'Venus::Role::Valuable';
13 3 with 'Venus::Role::Buildable';
14 3 with 'Venus::Role::Accessible';
15  
16 3 ▶use Scalar::Util ();
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # ATTRIBUTES
19  
20 3 attr 'on_none';
21 3 attr 'on_only';
22 3 attr 'on_then';
23 3 attr 'on_when';
24  
25 # BUILDERS
26  
27 sub build_self {
28 350 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=5 SCAR=17.0
Pod undocumented
29  
30 350 ▶ $self->on_none(sub{}) if !$self->on_none;
Subroutine __ANON__: called CC=1 SCAR=0.4
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_none 350 0 350
31 350 ▶ $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 350 0 350
32 350 ▶ $self->on_then([]) if !$self->on_then;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_then 350 0 350
33 350 ▶ $self->on_when([]) if !$self->on_when;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_when 350 0 350
34  
35 350 return $self;
36 }
37  
38 # METHODS
39  
40 sub clear {
41 1 ▶ my ($self) = @_;
Subroutine clear: called CC=1 SCAR=0.0
Pod documented
42  
43 1 ▶ $self->on_none(sub{});
Subroutine __ANON__: called CC=1 SCAR=0.0
44 1 ▶ $self->on_only(sub{1});
Subroutine __ANON__: called CC=1 SCAR=0.0
45 1 $self->on_then([]);
46 1 $self->on_when([]);
47  
48 1 return $self;
49 }
50  
51 sub data {
52 4 ▶ my ($self, $data) = @_;
Subroutine data: called CC=2 SCAR=6.9
Pod documented
53  
54 4 ▶ while(my($key, $value) = each(%$data)) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %$data) 16 4 20
55 16 $self->just($key)->then($value);
56 }
57  
58 4 return $self;
59 }
60  
61 sub expr {
62 8 ▶ my ($self, $topic) = @_;
Subroutine expr: called CC=1 SCAR=0.3
Pod documented
63  
64 $self->when(sub{
65 6 ▶ my $value = $self->value;
Subroutine __ANON__: called CC=9 SCAR=24.8
66  
67 6 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 6 6
68 0 return false;
69 }
70 6 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 6 6
Condition33%1/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
600
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
71 0 return false;
72 }
73 6 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 6 6
Condition33%1/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
060
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
74 0 return false;
75 }
76 6 ▶ if (ref($topic) eq 'Regexp' && "$value" =~ qr/$topic/) {
Branch: 2 branches
BranchTrueFalseTotal
if (ref $topic eq 'Regexp' and "$value" =~ qr/$topic/u) { } 4 2 6
elsif ("$value" eq "$topic") { } 1 1 2
Condition100%3/3
ref $topic eq "Regexp" and "$value" =~ qr/$topic/u
!ll&&!rl&&r
114
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
77 4 return true;
78 }
79 elsif ("$value" eq "$topic") {
80 1 return true;
81 }
82 else {
83 1 return false;
84 }
85 8 });
86  
87 8 return $self;
88 }
89  
90 sub just {
91 701 ▶ my ($self, $topic) = @_;
Subroutine just: called CC=1 SCAR=0.3
Pod documented
92  
93 $self->when(sub{
94 617 ▶ my $value = $self->value;
Subroutine __ANON__: called CC=7 SCAR=21.8
95  
96 617 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $value) 0 617 617
97 0 return false;
98 }
99 617 ▶ if (Scalar::Util::blessed($value) && !overload::Overloaded($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($value) and not overload::Overloaded($value)) 0 617 617
Condition66%2/3
builtin::blessed($value) and not overload::Overloaded($value)
!ll&&!rl&&r
61070
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
100 0 return false;
101 }
102 617 ▶ if (!Scalar::Util::blessed($value) && ref($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not builtin::blessed($value) and ref $value) 0 617 617
Condition66%2/3
not builtin::blessed($value) and ref $value
!ll&&!rl&&r
76100
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
103 0 return false;
104 }
105 617 ▶ if ("$value" eq "$topic") {
Branch: 1 branch
BranchTrueFalseTotal
if ("$value" eq "$topic") { } 176 441 617
106 176 return true;
107 }
108 else {
109 441 return false;
110 }
111 701 });
112  
113 701 return $self;
114 }
115  
116 sub none {
117 314 ▶ my ($self, $code) = @_;
Subroutine none: called CC=2 SCAR=6.9
Pod documented
118  
119 314 ▶ $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') ? : 310 4 314
120  
121 314 return $self;
122 }
123  
124 sub only {
125 2 ▶ my ($self, $code) = @_;
Subroutine only: called CC=1 SCAR=0.0
Pod documented
126  
127 2 $self->on_only($code);
128  
129 2 return $self;
130 }
131  
132 sub result {
133 343 ▶ my ($self, @args) = @_;
Subroutine result: called CC=8 SCAR=20.8
Pod documented
134  
135 343 ▶ $self->value($args[0]) if @args;
Branch: 1 branch
BranchTrueFalseTotal
if @args 12 331 343
136  
137 343 my $result;
138 343 my $matched = 0;
139 343 my $value = $self->value;
140  
141 343 local $_ = $value;
142 343 ▶ return wantarray ? ($result, $matched) : $result if !$self->on_only->($value);
Branch: 2 branches
BranchTrueFalseTotal
unless $self->on_only->($value) 1 342 343
wantarray ? : 0 1 1
143  
144 342 ▶ for (my $i = 0; $i < @{$self->on_when}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->on_when;}) 626 160 786
145 626 ▶ if ($self->on_when->[$i]->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_when->[$i]->($value)) 182 444 626
146 182 $result = $self->on_then->[$i]->($value);
147 182 $matched++;
148 182 last;
149 }
150 }
151  
152 342 ▶ if (!$matched) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($matched) 160 182 342
153 160 local $_ = $value;
154 160 $result = $self->on_none->($value);
155 }
156  
157 342 ▶ return wantarray ? ($result, $matched) : $result;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 1 341 342
158 }
159  
160 sub test {
161 3 ▶ my ($self) = @_;
Subroutine test: called CC=4 SCAR=13.9
Pod documented
162  
163 3 my $matched = 0;
164  
165 3 my $value = $self->value;
166  
167 3 local $_ = $value;
168  
169 3 ▶ return $matched if !$self->on_only->($value);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->on_only->($value) 0 3 3
170  
171 3 ▶ for (my $i = 0; $i < @{$self->on_when}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->on_when;}) 6 1 7
172 6 ▶ if ($self->on_when->[$i]->($value)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->on_when->[$i]->($value)) 2 4 6
173 2 $matched++;
174 2 last;
175 }
176 }
177  
178 3 return $matched;
179 }
180  
181 sub then {
182 722 ▶ my ($self, $code) = @_;
Subroutine then: called CC=2 SCAR=6.9
Pod documented
183  
184 722 my $next = $#{$self->on_when};
185  
186 722 ▶ $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') ? : 21 701 722
187  
188 722 return $self;
189 }
190  
191 sub when {
192 721 ▶ my ($self, $code, @args) = @_;
Subroutine when: called CC=2 SCAR=7.0
Pod documented
193  
194 721 ▶ 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 721 721
195  
196 $self->on_when->[$next] = sub {
197 632 ▶ (local $_ = $_[0])->$code(@args);
Subroutine __ANON__: called CC=1 SCAR=0.0
198 721 };
199  
200 721 return $self;
201 }
202  
203 sub where {
204 6 ▶ my ($self) = @_;
Subroutine where: called CC=1 SCAR=0.0
Pod documented
205  
206 6 my $where = $self->new;
207  
208 6 ▶ $self->then(sub{$where->result(@_)});
Subroutine __ANON__: called CC=1 SCAR=0.0
209  
210 6 return $where;
211 }
212  
213 3 1;
214  
215  
216  
217 =head1 NAME
218  
219 Venus::Match - Match Class
220  
221 =cut
222  
223 =head1 ABSTRACT
224  
225 Match Class for Perl 5
226  
227 =cut
228  
229 =head1 SYNOPSIS
230  
231 package main;
232  
233 use Venus::Match;
234  
235 my $match = Venus::Match->new(5);
236  
237 $match->when(sub{$_ < 5})->then(sub{"< 5"});
238 $match->when(sub{$_ > 5})->then(sub{"> 5"});
239  
240 $match->none(sub{"?"});
241  
242 my $result = $match->result;
243  
244 # "?"
245  
246 =cut
247  
248 =head1 DESCRIPTION
249  
250 This package provides an object-oriented interface for complex pattern matching
251 operations on scalar values. See L<Venus::Gather> for operating on collections
252 of data, e.g. array references.
253  
254 =cut
255  
256 =head1 ATTRIBUTES
257  
258 This package has the following attributes:
259  
260 =cut
261  
262 =head2 on_none
263  
264 on_none(CodeRef)
265  
266 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{}>.
267  
268 =cut
269  
270 =head2 on_only
271  
272 on_only(CodeRef)
273  
274 This attribute is read-write, accepts C<(CodeRef)> values, is optional, and defaults to C<sub{1}>.
275  
276 =cut
277  
278 =head2 on_then
279  
280 on_then(ArrayRef[CodeRef])
281  
282 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
283  
284 =cut
285  
286 =head2 on_when
287  
288 on_when(ArrayRef[CodeRef])
289  
290 This attribute is read-write, accepts C<(ArrayRef[CodeRef])> values, is optional, and defaults to C<[]>.
291  
292 =cut
293  
294 =head1 INHERITS
295  
296 This package inherits behaviors from:
297  
298 L<Venus::Kind::Utility>
299  
300 =cut
301  
302 =head1 INTEGRATES
303  
304 This package integrates behaviors from:
305  
306 L<Venus::Role::Accessible>
307  
308 L<Venus::Role::Buildable>
309  
310 L<Venus::Role::Valuable>
311  
312 =cut
313  
314 =head1 METHODS
315  
316 This package provides the following methods:
317  
318 =cut
319  
320 =head2 clear
321  
322 clear() (Venus::Match)
323  
324 The clear method resets all match conditions and returns the invocant.
325  
326 I<Since C<1.23>>
327  
328 =over 4
329  
330 =item clear example 1
331  
332 # given: synopsis
333  
334 package main;
335  
336 my $clear = $match->clear;
337  
338 # bless(..., "Venus::Match")
339  
340 =back
341  
342 =cut
343  
344 =head2 data
345  
346 data(hashref $data) (Venus::Match)
347  
348 The data method takes a hashref (i.e. lookup table) and creates match
349 conditions and actions based on the keys and values found.
350  
351 I<Since C<0.07>>
352  
353 =over 4
354  
355 =item data example 1
356  
357 package main;
358  
359 use Venus::Match;
360  
361 my $match = Venus::Match->new('a');
362  
363 $match->data({
364 'a' => 'b',
365 'c' => 'd',
366 'e' => 'f',
367 'g' => 'h',
368 });
369  
370 my $result = $match->none('z')->result;
371  
372 # "b"
373  
374 =back
375  
376 =over 4
377  
378 =item data example 2
379  
380 package main;
381  
382 use Venus::Match;
383  
384 my $match = Venus::Match->new('x');
385  
386 $match->data({
387 'a' => 'b',
388 'c' => 'd',
389 'e' => 'f',
390 'g' => 'h',
391 });
392  
393 my $result = $match->none('z')->result;
394  
395 # "z"
396  
397 =back
398  
399 =cut
400  
401 =head2 expr
402  
403 expr(string | regexp $expr) (Venus::Match)
404  
405 The expr method registers a L</when> condition that check if the match value is
406 an exact string match of the C<$topic> if the topic is a string, or that it
407 matches against the topic if the topic is a regular expression.
408  
409 I<Since C<0.07>>
410  
411 =over 4
412  
413 =item expr example 1
414  
415 package main;
416  
417 use Venus::Match;
418  
419 my $match = Venus::Match->new('1901-01-01');
420  
421 $match->expr('1901-01-01')->then(sub{[split /-/]});
422  
423 my $result = $match->result;
424  
425 # ["1901", "01", "01"]
426  
427 =back
428  
429 =over 4
430  
431 =item expr example 2
432  
433 package main;
434  
435 use Venus::Match;
436  
437 my $match = Venus::Match->new('1901-01-01');
438  
439 $match->expr(qr/^1901-/)->then(sub{[split /-/]});
440  
441 my $result = $match->result;
442  
443 # ["1901", "01", "01"]
444  
445 =back
446  
447 =cut
448  
449 =head2 just
450  
451 just(string $topic) (Venus::Match)
452  
453 The just method registers a L</when> condition that check if the match value is
454 an exact string match of the C<$topic> provided.
455  
456 I<Since C<0.03>>
457  
458 =over 4
459  
460 =item just example 1
461  
462 package main;
463  
464 use Venus::Match;
465  
466 my $match = Venus::Match->new('a');
467  
468 $match->just('a')->then('a');
469 $match->just('b')->then('b');
470 $match->just('c')->then('c');
471  
472 my $result = $match->result;
473  
474 # "a"
475  
476 =back
477  
478 =over 4
479  
480 =item just example 2
481  
482 package main;
483  
484 use Venus::Match;
485 use Venus::String;
486  
487 my $match = Venus::Match->new(Venus::String->new('a'));
488  
489 $match->just('a')->then('a');
490 $match->just('b')->then('b');
491 $match->just('c')->then('c');
492  
493 my $result = $match->result;
494  
495 # "a"
496  
497 =back
498  
499 =over 4
500  
501 =item just example 3
502  
503 package main;
504  
505 use Venus::Match;
506 use Venus::String;
507  
508 my $match = Venus::Match->new(Venus::String->new('c'));
509  
510 $match->just('a')->then('a');
511 $match->just('b')->then('b');
512 $match->just('c')->then('c');
513  
514 my $result = $match->result;
515  
516 # "c"
517  
518 =back
519  
520 =over 4
521  
522 =item just example 4
523  
524 package main;
525  
526 use Venus::Match;
527  
528 my $match = Venus::Match->new(1.23);
529  
530 $match->just('1.230')->then('1.230');
531 $match->just(01.23)->then('123');
532 $match->just(1.230)->then(1.23);
533  
534 my $result = $match->result;
535  
536 # "1.23"
537  
538 =back
539  
540 =over 4
541  
542 =item just example 5
543  
544 package main;
545  
546 use Venus::Match;
547 use Venus::Number;
548  
549 my $match = Venus::Match->new(Venus::Number->new(1.23));
550  
551 $match->just('1.230')->then('1.230');
552 $match->just(01.23)->then('123');
553 $match->just(1.230)->then(1.23);
554  
555 my $result = $match->result;
556  
557 # "1.23"
558  
559 =back
560  
561 =over 4
562  
563 =item just example 6
564  
565 package main;
566  
567 use Venus::Match;
568 use Venus::Number;
569  
570 my $match = Venus::Match->new(1.23);
571  
572 $match->just(Venus::Number->new('1.230'))->then('1.230');
573 $match->just(Venus::Number->new(01.23))->then('123');
574 $match->just(Venus::Number->new(1.230))->then(1.23);
575  
576 my $result = $match->result;
577  
578 # "1.23"
579  
580 =back
581  
582 =cut
583  
584 =head2 none
585  
586 none(any | coderef $code) (Venus::Match)
587  
588 The none method registers a special condition that returns a result only when
589 no other conditions have been matched.
590  
591 I<Since C<0.03>>
592  
593 =over 4
594  
595 =item none example 1
596  
597 package main;
598  
599 use Venus::Match;
600  
601 my $match = Venus::Match->new('z');
602  
603 $match->just('a')->then('a');
604 $match->just('b')->then('b');
605 $match->just('c')->then('c');
606  
607 $match->none('z');
608  
609 my $result = $match->result;
610  
611 # "z"
612  
613 =back
614  
615 =over 4
616  
617 =item none example 2
618  
619 package main;
620  
621 use Venus::Match;
622  
623 my $match = Venus::Match->new('z');
624  
625 $match->just('a')->then('a');
626 $match->just('b')->then('b');
627 $match->just('c')->then('c');
628  
629 $match->none(sub{"($_) not found"});
630  
631 my $result = $match->result;
632  
633 # "(z) not found"
634  
635 =back
636  
637 =cut
638  
639 =head2 only
640  
641 only(coderef $code) (Venus::Match)
642  
643 The only method registers a special condition that only allows matching on the
644 match value only if the code provided returns truthy.
645  
646 I<Since C<0.03>>
647  
648 =over 4
649  
650 =item only example 1
651  
652 package main;
653  
654 use Venus::Match;
655  
656 my $match = Venus::Match->new(5);
657  
658 $match->only(sub{$_ != 5});
659  
660 $match->just(5)->then(5);
661 $match->just(6)->then(6);
662  
663 my $result = $match->result;
664  
665 # undef
666  
667 =back
668  
669 =over 4
670  
671 =item only example 2
672  
673 package main;
674  
675 use Venus::Match;
676  
677 my $match = Venus::Match->new(6);
678  
679 $match->only(sub{$_ != 5});
680  
681 $match->just(5)->then(5);
682 $match->just(6)->then(6);
683  
684 my $result = $match->result;
685  
686 # 6
687  
688 =back
689  
690 =cut
691  
692 =head2 result
693  
694 result(any $data) (any)
695  
696 The result method evaluates the registered conditions and returns the result of
697 the action (i.e. the L</then> code) or the special L</none> condition if there
698 were no matches. In list context, this method returns both the result and
699 whether or not a condition matched. Optionally, when passed an argument this
700 method assign the argument as the value/topic and then perform the operation.
701  
702 I<Since C<0.03>>
703  
704 =over 4
705  
706 =item result example 1
707  
708 package main;
709  
710 use Venus::Match;
711  
712 my $match = Venus::Match->new('a');
713  
714 $match->just('a')->then('a');
715 $match->just('b')->then('b');
716 $match->just('c')->then('c');
717  
718 my $result = $match->result;
719  
720 # "a"
721  
722 =back
723  
724 =over 4
725  
726 =item result example 2
727  
728 package main;
729  
730 use Venus::Match;
731  
732 my $match = Venus::Match->new('a');
733  
734 $match->just('a')->then('a');
735 $match->just('b')->then('b');
736 $match->just('c')->then('c');
737  
738 my ($result, $matched) = $match->result;
739  
740 # ("a", 1)
741  
742 =back
743  
744 =over 4
745  
746 =item result example 3
747  
748 package main;
749  
750 use Venus::Match;
751  
752 sub fibonacci {
753 my ($n) = @_;
754 my $match = Venus::Match->new($n)
755 ->just(1)->then(1)
756 ->just(2)->then(1)
757 ->none(sub{fibonacci($n - 1) + fibonacci($n - 2)})
758 ->result
759 }
760  
761 my $result = [fibonacci(4), fibonacci(6), fibonacci(12)]
762  
763 # [3, 8, 144]
764  
765 =back
766  
767 =over 4
768  
769 =item result example 4
770  
771 package main;
772  
773 use Venus::Match;
774  
775 my $match = Venus::Match->new('a');
776  
777 $match->just('a')->then('a');
778 $match->just('b')->then('b');
779 $match->just('c')->then('c');
780  
781 my $result = $match->result('b');
782  
783 # "b"
784  
785 =back
786  
787 =over 4
788  
789 =item result example 5
790  
791 package main;
792  
793 use Venus::Match;
794  
795 my $match = Venus::Match->new('a');
796  
797 $match->just('a')->then('a');
798 $match->just('b')->then('b');
799 $match->just('c')->then('c');
800  
801 my $result = $match->result('z');
802  
803 # undef
804  
805 =back
806  
807 =cut
808  
809 =head2 then
810  
811 then(any | coderef $code) (Venus::Match)
812  
813 The then method registers an action to be executed if the corresponding match
814 condition returns truthy.
815  
816 I<Since C<0.03>>
817  
818 =over 4
819  
820 =item then example 1
821  
822 package main;
823  
824 use Venus::Match;
825  
826 my $match = Venus::Match->new('b');
827  
828 $match->just('a');
829 $match->then('a');
830  
831 $match->just('b');
832 $match->then('b');
833  
834 my $result = $match->result;
835  
836 # "b"
837  
838 =back
839  
840 =over 4
841  
842 =item then example 2
843  
844 package main;
845  
846 use Venus::Match;
847  
848 my $match = Venus::Match->new('b');
849  
850 $match->just('a');
851 $match->then('a');
852  
853 $match->just('b');
854 $match->then('b');
855 $match->then('x');
856  
857 my $result = $match->result;
858  
859 # "x"
860  
861 =back
862  
863 =cut
864  
865 =head2 when
866  
867 when(string | coderef $code, any @args) (Venus::Match)
868  
869 The when method registers a match condition that will be passed the match value
870 during evaluation. If the match condition returns truthy the corresponding
871 action will be used to return a result. If the match value is an object, this
872 method can take a method name and arguments which will be used as a match
873 condition.
874  
875 I<Since C<0.03>>
876  
877 =over 4
878  
879 =item when example 1
880  
881 package main;
882  
883 use Venus::Match;
884  
885 my $match = Venus::Match->new('a');
886  
887 $match->when(sub{$_ eq 'a'});
888 $match->then('a');
889  
890 $match->when(sub{$_ eq 'b'});
891 $match->then('b');
892  
893 $match->when(sub{$_ eq 'c'});
894 $match->then('c');
895  
896 my $result = $match->result;
897  
898 # "a"
899  
900 =back
901  
902 =over 4
903  
904 =item when example 2
905  
906 package main;
907  
908 use Venus::Match;
909 use Venus::Type;
910  
911 my $match = Venus::Match->new(Venus::Type->new(1)->deduce);
912  
913 $match->when('isa', 'Venus::Number');
914 $match->then('Venus::Number');
915  
916 $match->when('isa', 'Venus::String');
917 $match->then('Venus::String');
918  
919 my $result = $match->result;
920  
921 # "Venus::Number"
922  
923 =back
924  
925 =over 4
926  
927 =item when example 3
928  
929 package main;
930  
931 use Venus::Match;
932 use Venus::Type;
933  
934 my $match = Venus::Match->new(Venus::Type->new('1')->deduce);
935  
936 $match->when('isa', 'Venus::Number');
937 $match->then('Venus::Number');
938  
939 $match->when('isa', 'Venus::String');
940 $match->then('Venus::String');
941  
942 my $result = $match->result;
943  
944 # "Venus::String"
945  
946 =back
947  
948 =cut
949  
950 =head2 where
951  
952 where() (Venus::Match)
953  
954 The where method registers an action as a sub-match operation, to be executed
955 if the corresponding match condition returns truthy. This method returns the
956 sub-match object.
957  
958 I<Since C<1.40>>
959  
960 =over 4
961  
962 =item where example 1
963  
964 package main;
965  
966 use Venus::Match;
967  
968 my $match = Venus::Match->new;
969  
970 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
971  
972 $submatch1->just('peach')->then('peach-123');
973 $submatch1->just('patch')->then('patch-456');
974 $submatch1->just('punch')->then('punch-789');
975  
976 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
977  
978 $submatch2->just('merch')->then('merch-123');
979 $submatch2->just('march')->then('march-456');
980 $submatch2->just('mouch')->then('mouch-789');
981  
982 my $result = $match->result('peach');
983  
984 # "peach-123"
985  
986 =back
987  
988 =over 4
989  
990 =item where example 2
991  
992 package main;
993  
994 use Venus::Match;
995  
996 my $match = Venus::Match->new;
997  
998 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
999  
1000 $submatch1->just('peach')->then('peach-123');
1001 $submatch1->just('patch')->then('patch-456');
1002 $submatch1->just('punch')->then('punch-789');
1003  
1004 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
1005  
1006 $submatch2->just('merch')->then('merch-123');
1007 $submatch2->just('march')->then('march-456');
1008 $submatch2->just('mouch')->then('mouch-789');
1009  
1010 my $result = $match->result('march');
1011  
1012 # "march-456"
1013  
1014 =back
1015  
1016 =over 4
1017  
1018 =item where example 3
1019  
1020 package main;
1021  
1022 use Venus::Match;
1023  
1024 my $match = Venus::Match->new;
1025  
1026 my $submatch1 = $match->expr(qr/^p([a-z]+)ch/)->where;
1027  
1028 $submatch1->just('peach')->then('peach-123');
1029 $submatch1->just('patch')->then('patch-456');
1030 $submatch1->just('punch')->then('punch-789');
1031  
1032 my $submatch2 = $match->expr(qr/^m([a-z]+)ch/)->where;
1033  
1034 $submatch2->just('merch')->then('merch-123');
1035 $submatch2->just('march')->then('march-456');
1036 $submatch2->just('mouch')->then('mouch-789');
1037  
1038 my $result = $match->result('pirch');
1039  
1040 # undef
1041  
1042 =back
1043  
1044 =cut
1045  
1046 =head1 AUTHORS
1047  
1048 Awncorp, C<awncorp@cpan.org>
1049  
1050 =cut
1051  
1052 =head1 LICENSE
1053  
1054 Copyright (C) 2000, Awncorp, C<awncorp@cpan.org>.
1055  
1056 This program is free software, you can redistribute it and/or modify it under
1057 the terms of the Apache license version 2.0.
1058  
1059 =cut
« blib/lib/Venus/Template.pm ↑ Summary blib/lib/Venus/Unpack.pm »