Venus/Search.pm

filter: Statementstmt 100% 103 / 103 Branchbran 66.6% 8 / 12 Conditioncond 47.9% 23 / 48 MC/DCmcdc 0.0% 0 / 29 Subroutinesub 100% 23 / 23 Podpod 92.8% 13 / 14 totaltotal 74.2% 170 / 229 CC 26 SCAR 11.5
CC
26
Cov
82%
captures
6.4
build_self
4.3
matched
4.3
CRAP 3.2
« Venus/Role/Proxyable.pm ↑ Summary Venus/Role/Testable.pm »
1 package Venus::Search;
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::Explainable';
13 3 with 'Venus::Role::Stashable';
14  
15 use overload (
16 '""' => 'explain',
17 1 ▶ '.' => sub{"$_[0]" . "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
18 1 ▶ 'eq' => sub{"$_[0]" eq "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
19 1 ▶ 'ne' => sub{"$_[0]" ne "$_[1]"},
Subroutine __ANON__: called CC=1 SCAR=0.0
20 1 ▶ 'qr' => sub{qr{@{[quotemeta("$_[0]")]}}},
Subroutine __ANON__: called CC=1 SCAR=0.0
21 3 '~~' => 'explain',
22 fallback => 1,
23 3 ▶);
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 # ATTRIBUTES
26  
27 3 attr 'flags';
28 3 attr 'regexp';
29 3 attr 'string';
30  
31 # BUILDERS
32  
33 sub build_self {
34 23 ▶ my ($self, $data) = @_;
Subroutine build_self: called CC=4 SCAR=14.6
Pod undocumented
35  
36 23 ▶ $self->flags('') if !$self->flags;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->flags 23 0 23
37 23 ▶ $self->regexp(qr//) if !$self->regexp;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->regexp 0 23 23
38 23 ▶ $self->string('') if !$self->string;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->string 0 23 23
39  
40 23 return $self;
41 }
42  
43 # METHODS
44  
45 sub captures {
46 1 ▶ my ($self) = @_;
Subroutine captures: called CC=6 SCAR=18.6
Pod documented
47  
48 1 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
49  
50 1 my $string = $self->initial;
51 1 my $last_match_start = $self->last_match_start;
52 1 my $last_match_end = $self->last_match_end;
53  
54 1 my $captures = [];
55  
56 1 ▶ for (my $i = 1; $i < @$last_match_end; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$last_match_end) 1 1 2
57 1 ▶ my $start = $last_match_start->[$i] || 0;
Condition50%1/2
$last_match_start->[$i] || 0
l!l
01
MC/DC0%0/1
$last_match_start->[$i] || 0
$last_match_start->[$i]
Truth table50%1/2
A
A= $last_match_start->[$i]
Aresult
11
00
58 1 ▶ my $end = $last_match_end->[$i] || 0;
Condition50%1/2
$last_match_end->[$i] || 0
l!l
10
MC/DC0%0/1
$last_match_end->[$i] || 0
$last_match_end->[$i]
Truth table50%1/2
A
A= $last_match_end->[$i]
Aresult
11
00
59  
60 1 push @$captures, substr $string, $start, $end - $start;
61 }
62  
63 1 ▶ return wantarray ? (@$captures) : $captures;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 0 1 1
64 }
65  
66 sub evaluate {
67 23 ▶ my ($self) = @_;
Subroutine evaluate: called CC=3 SCAR=11.0
Pod documented
68  
69 23 my $captures = 0;
70 23 my $flags = $self->flags;
71 23 my @matches = ();
72 23 my $regexp = $self->regexp;
73 23 my $string = $self->string;
74 23 my $initial = "$string";
75  
76 23 local $@;
77 23 ▶ eval join ';', (
Condition50%1/2
$flags // ""
l!l
230
MC/DC0%0/1
$flags // ""
$flags
Truth table50%1/2
A
A= $flags
Aresult
11
00
78 '$captures = (' . '$string =~ m/$regexp/' . ($flags // '') . ')',
79 '@matches = ([@-], [@+], {%-})',
80 );
81  
82 23 my $error = $@;
83  
84 23 ▶ if ($error) {
Branch: 1 branch
BranchTrueFalseTotal
if ($error) 1 22 23
85 1 $self->throw->error({message => $error});
86 }
87  
88 22 return $self->stash(evaluation => [
89 $regexp,
90 $string,
91 $captures,
92 @matches,
93 $initial,
94 ]);
95 }
96  
97 sub explain {
98 12 ▶ my ($self) = @_;
Subroutine explain: called CC=1 SCAR=0.0
Pod documented
99  
100 12 return $self->get;
101 }
102  
103 sub get {
104 13 ▶ my ($self) = @_;
Subroutine get: called CC=2 SCAR=7.0
Pod documented
105  
106 13 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition66%2/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
580
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table66%2/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
107  
108 13 return $evaluation->[1];
109 }
110  
111 sub count {
112 1 ▶ my ($self) = @_;
Subroutine count: called CC=2 SCAR=7.6
Pod documented
113  
114 1 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
115  
116 1 return $evaluation->[2];
117 }
118  
119 sub initial {
120 5 ▶ my ($self) = @_;
Subroutine initial: called CC=2 SCAR=7.0
Pod documented
121  
122 5 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition66%2/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
410
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table66%2/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
123  
124 5 return $evaluation->[6];
125 }
126  
127 sub last_match_end {
128 5 ▶ my ($self) = @_;
Subroutine last_match_end: called CC=2 SCAR=7.0
Pod documented
129  
130 5 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition66%2/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
410
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table66%2/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
131  
132 5 return $evaluation->[4];
133 }
134  
135 sub last_match_start {
136 5 ▶ my ($self) = @_;
Subroutine last_match_start: called CC=2 SCAR=7.0
Pod documented
137  
138 5 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition66%2/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
410
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table66%2/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
139  
140 5 return $evaluation->[3];
141 }
142  
143 sub matched {
144 1 ▶ my ($self) = @_;
Subroutine matched: called CC=4 SCAR=14.6
Pod documented
145  
146 1 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
147  
148 1 my $string = $self->initial;
149 1 my $last_match_start = $self->last_match_start;
150 1 my $last_match_end = $self->last_match_end;
151  
152 1 ▶ my $start = $last_match_start->[0] || 0;
Condition50%1/2
$last_match_start->[0] || 0
l!l
01
MC/DC0%0/1
$last_match_start->[0] || 0
$last_match_start->[0]
Truth table50%1/2
A
A= $last_match_start->[0]
Aresult
11
00
153 1 ▶ my $end = $last_match_end->[0] || 0;
Condition50%1/2
$last_match_end->[0] || 0
l!l
10
MC/DC0%0/1
$last_match_end->[0] || 0
$last_match_end->[0]
Truth table50%1/2
A
A= $last_match_end->[0]
Aresult
11
00
154  
155 1 return substr $string, $start, $end - $start;
156 }
157  
158 sub named_captures {
159 2 ▶ my ($self) = @_;
Subroutine named_captures: called CC=2 SCAR=7.6
Pod documented
160  
161 2 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
020
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
162  
163 2 return $evaluation->[5];
164 }
165  
166 sub prematched {
167 1 ▶ my ($self) = @_;
Subroutine prematched: called CC=4 SCAR=14.6
Pod documented
168  
169 1 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
170  
171 1 my $string = $self->initial;
172 1 my $last_match_start = $self->last_match_start;
173 1 my $last_match_end = $self->last_match_end;
174  
175 1 ▶ my $start = $last_match_start->[0] || 0;
Condition50%1/2
$last_match_start->[0] || 0
l!l
01
MC/DC0%0/1
$last_match_start->[0] || 0
$last_match_start->[0]
Truth table50%1/2
A
A= $last_match_start->[0]
Aresult
11
00
176 1 ▶ my $end = $last_match_end->[0] || 0;
Condition50%1/2
$last_match_end->[0] || 0
l!l
10
MC/DC0%0/1
$last_match_end->[0] || 0
$last_match_end->[0]
Truth table50%1/2
A
A= $last_match_end->[0]
Aresult
11
00
177  
178 1 return substr $string, 0, $start;
179 }
180  
181 sub postmatched {
182 1 ▶ my ($self) = @_;
Subroutine postmatched: called CC=4 SCAR=14.6
Pod documented
183  
184 1 ▶ my $evaluation = $self->stash('evaluation') || $self->evaluate;
Condition33%1/3
$self->stash('evaluation') || $self->evaluate
l!l&&r!l&&!r
010
MC/DC0%0/2
$self->stash('evaluation') || $self->evaluate
$self->stash('evaluation') $self->evaluate
Truth table33%1/3
A || B
A= $self->stash('evaluation')B= $self->evaluate
ABresult
1X1
011
000
185  
186 1 my $string = $self->initial;
187 1 my $last_match_start = $self->last_match_start;
188 1 my $last_match_end = $self->last_match_end;
189  
190 1 ▶ my $start = $last_match_start->[0] || 0;
Condition50%1/2
$last_match_start->[0] || 0
l!l
01
MC/DC0%0/1
$last_match_start->[0] || 0
$last_match_start->[0]
Truth table50%1/2
A
A= $last_match_start->[0]
Aresult
11
00
191 1 ▶ my $end = $last_match_end->[0] || 0;
Condition50%1/2
$last_match_end->[0] || 0
l!l
10
MC/DC0%0/1
$last_match_end->[0] || 0
$last_match_end->[0]
Truth table50%1/2
A
A= $last_match_end->[0]
Aresult
11
00
192  
193 1 return substr $string, $end;
194 }
195  
196 sub set {
197 1 ▶ my ($self, $string) = @_;
Subroutine set: called CC=1 SCAR=0.0
Pod documented
198  
199 1 $self->string($string);
200  
201 1 my $evaluation = $self->evaluate;
202  
203 1 return $evaluation->[1];
204 }
205  
206 3 1;
207  
208  
209  
210 =head1 NAME
211  
212 Venus::Search - Search Class
213  
214 =cut
215  
216 =head1 ABSTRACT
217  
218 Search Class for Perl 5
219  
220 =cut
221  
222 =head1 SYNOPSIS
223  
224 package main;
225  
226 use Venus::Search;
227  
228 my $search = Venus::Search->new(
229 string => 'hello world',
230 regexp => '(hello)',
231 );
232  
233 # $search->captures;
234  
235 =cut
236  
237 =head1 DESCRIPTION
238  
239 This package provides methods for manipulating regexp search data.
240  
241 =cut
242  
243 =head1 ATTRIBUTES
244  
245 This package has the following attributes:
246  
247 =cut
248  
249 =head2 flags
250  
251 flags(Str)
252  
253 This attribute is read-write, accepts C<(Str)> values, is optional, and defaults to C<''>.
254  
255 =cut
256  
257 =head2 regexp
258  
259 regexp(Regexp)
260  
261 This attribute is read-write, accepts C<(Regexp)> values, is optional, and defaults to C<qr//>.
262  
263 =cut
264  
265 =head2 string
266  
267 string(Str)
268  
269 This attribute is read-write, accepts C<(Str)> values, is optional, and defaults to C<''>.
270  
271 =cut
272  
273 =head1 INHERITS
274  
275 This package inherits behaviors from:
276  
277 L<Venus::Kind::Utility>
278  
279 =cut
280  
281 =head1 INTEGRATES
282  
283 This package integrates behaviors from:
284  
285 L<Venus::Role::Explainable>
286  
287 L<Venus::Role::Stashable>
288  
289 =cut
290  
291 =head1 METHODS
292  
293 This package provides the following methods:
294  
295 =cut
296  
297 =head2 captures
298  
299 captures() (ArrayRef)
300  
301 The captures method returns the capture groups from the result object which
302 contains information about the results of the regular expression operation.
303 This method can return a list of values in list-context.
304  
305 I<Since C<0.01>>
306  
307 =over 4
308  
309 =item captures example 1
310  
311 # given: synopsis;
312  
313 my $captures = $search->captures;
314  
315 # ["hello"]
316  
317 =back
318  
319 =cut
320  
321 =head2 count
322  
323 count() (Num)
324  
325 The count method returns the number of matches found in the result object which
326 contains information about the results of the regular expression operation.
327  
328 I<Since C<0.01>>
329  
330 =over 4
331  
332 =item count example 1
333  
334 # given: synopsis;
335  
336 my $count = $search->count;
337  
338 # 1
339  
340 =back
341  
342 =cut
343  
344 =head2 evaluate
345  
346 evaluate() (ArrayRef)
347  
348 The evaluate method performs the regular expression operation and returns an
349 arrayref representation of the results.
350  
351 I<Since C<0.01>>
352  
353 =over 4
354  
355 =item evaluate example 1
356  
357 # given: synopsis;
358  
359 my $evaluate = $search->evaluate;
360  
361 # ["(hello)", "hello world", 1, [0, 0], [5, 5], {}, "hello world"]
362  
363 =back
364  
365 =over 4
366  
367 =item evaluate example 2
368  
369 package main;
370  
371 use Venus::Search;
372  
373 my $search = Venus::Search->new(
374 string => 'hello world',
375 regexp => 'hello:)',
376 );
377  
378 my $evaluate = $search->evaluate;
379  
380 # Exception! Venus::Search::Error (isa Venus::Error)
381  
382 =back
383  
384 =cut
385  
386 =head2 explain
387  
388 explain() (Str)
389  
390 The explain method returns the subject of the regular expression operation and
391 is used in stringification operations.
392  
393 I<Since C<0.01>>
394  
395 =over 4
396  
397 =item explain example 1
398  
399 # given: synopsis;
400  
401 my $explain = $search->explain;
402  
403 # "hello world"
404  
405 =back
406  
407 =cut
408  
409 =head2 get
410  
411 get() (Str)
412  
413 The get method returns the subject of the regular expression operation.
414  
415 I<Since C<0.01>>
416  
417 =over 4
418  
419 =item get example 1
420  
421 # given: synopsis;
422  
423 my $get = $search->get;
424  
425 # "hello world"
426  
427 =back
428  
429 =cut
430  
431 =head2 initial
432  
433 initial() (Str)
434  
435 The initial method returns the unaltered string from the result object which
436 contains information about the results of the regular expression operation.
437  
438 I<Since C<0.01>>
439  
440 =over 4
441  
442 =item initial example 1
443  
444 # given: synopsis;
445  
446 my $initial = $search->initial;
447  
448 # "hello world"
449  
450 =back
451  
452 =cut
453  
454 =head2 last_match_end
455  
456 last_match_end() (Maybe[ArrayRef[Int]])
457  
458 The last_match_end method returns an array of offset positions into the string
459 where the capture(s) stopped matching from the result object which contains
460 information about the results of the regular expression operation.
461  
462 I<Since C<0.01>>
463  
464 =over 4
465  
466 =item last_match_end example 1
467  
468 # given: synopsis;
469  
470 my $last_match_end = $search->last_match_end;
471  
472 # [5, 5]
473  
474 =back
475  
476 =cut
477  
478 =head2 last_match_start
479  
480 last_match_start() (Maybe[ArrayRef[Int]])
481  
482 The last_match_start method returns an array of offset positions into the
483 string where the capture(s) matched from the result object which contains
484 information about the results of the regular expression operation.
485  
486 I<Since C<0.01>>
487  
488 =over 4
489  
490 =item last_match_start example 1
491  
492 # given: synopsis;
493  
494 my $last_match_start = $search->last_match_start;
495  
496 # [0, 0]
497  
498 =back
499  
500 =cut
501  
502 =head2 matched
503  
504 matched() (Maybe[Str])
505  
506 The matched method returns the portion of the string that matched from the
507 result object which contains information about the results of the regular
508 expression operation.
509  
510 I<Since C<0.01>>
511  
512 =over 4
513  
514 =item matched example 1
515  
516 # given: synopsis;
517  
518 my $matched = $search->matched;
519  
520 # "hello"
521  
522 =back
523  
524 =cut
525  
526 =head2 named_captures
527  
528 named_captures() (HashRef)
529  
530 The named_captures method returns a hash containing the requested named regular
531 expressions and captured string pairs from the result object which contains
532 information about the results of the regular expression operation.
533  
534 I<Since C<0.01>>
535  
536 =over 4
537  
538 =item named_captures example 1
539  
540 # given: synopsis;
541  
542 my $named_captures = $search->named_captures;
543  
544 # {}
545  
546 =back
547  
548 =over 4
549  
550 =item named_captures example 2
551  
552 package main;
553  
554 use Venus::Search;
555  
556 my $search = Venus::Search->new(
557 string => 'hello world',
558 regexp => '(?<locale>world)',
559 );
560  
561 my $named_captures = $search->named_captures;
562  
563 # { locale => ["world"] }
564  
565 =back
566  
567 =cut
568  
569 =head2 postmatched
570  
571 postmatched() (Maybe[Str])
572  
573 The postmatched method returns the portion of the string after the regular
574 expression matched from the result object which contains information about the
575 results of the regular expression operation.
576  
577 I<Since C<0.01>>
578  
579 =over 4
580  
581 =item postmatched example 1
582  
583 # given: synopsis;
584  
585 my $postmatched = $search->postmatched;
586  
587 # " world"
588  
589 =back
590  
591 =cut
592  
593 =head2 prematched
594  
595 prematched() (Maybe[Str])
596  
597 The prematched method returns the portion of the string before the regular
598 expression matched from the result object which contains information about the
599 results of the regular expression operation.
600  
601 I<Since C<0.01>>
602  
603 =over 4
604  
605 =item prematched example 1
606  
607 # given: synopsis;
608  
609 my $prematched = $search->prematched;
610  
611 # ""
612  
613 =back
614  
615 =cut
616  
617 =head2 set
618  
619 set(Str $string) (Str)
620  
621 The set method sets the subject of the regular expression operation.
622  
623 I<Since C<0.01>>
624  
625 =over 4
626  
627 =item set example 1
628  
629 # given: synopsis;
630  
631 my $set = $search->set('hello universe');
632  
633 # "hello universe"
634  
635 =back
636  
637 =cut
638  
639 =head1 OPERATORS
640  
641 This package overloads the following operators:
642  
643 =cut
644  
645 =over 4
646  
647 =item operation: C<(.)>
648  
649 This package overloads the C<.> operator.
650  
651 B<example 1>
652  
653 # given: synopsis;
654  
655 my $result = $search . ', welcome';
656  
657 # "hello world, welcome"
658  
659 =back
660  
661 =over 4
662  
663 =item operation: C<(eq)>
664  
665 This package overloads the C<eq> operator.
666  
667 B<example 1>
668  
669 # given: synopsis;
670  
671 my $result = $search eq 'hello world';
672  
673 # 1
674  
675 =back
676  
677 =over 4
678  
679 =item operation: C<(ne)>
680  
681 This package overloads the C<ne> operator.
682  
683 B<example 1>
684  
685 # given: synopsis;
686  
687 my $result = $search ne 'Hello world';
688  
689 # 1
690  
691 =back
692  
693 =over 4
694  
695 =item operation: C<(qr)>
696  
697 This package overloads the C<qr> operator.
698  
699 B<example 1>
700  
701 # given: synopsis;
702  
703 my $result = 'hello world, welcome' =~ qr/$search/;
704  
705 # 1
706  
707 =back
708  
709 =over 4
710  
711 =item operation: C<("")>
712  
713 This package overloads the C<""> operator.
714  
715 B<example 1>
716  
717 # given: synopsis;
718  
719 my $result = "$search";
720  
721 # "hello world"
722  
723 =back
724  
725 =over 4
726  
727 =item operation: C<(~~)>
728  
729 This package overloads the C<~~> operator.
730  
731 B<example 1>
732  
733 # given: synopsis;
734  
735 my $result = $search ~~ 'hello world';
736  
737 # 1
738  
739 =back
« Venus/Role/Proxyable.pm ↑ Summary Venus/Role/Testable.pm »