blib/lib/App/RegexpPatternUtils.pm

filter: Statementstmt 41.5% 22 / 53 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 57.1% 4 / 7 Podpod 100% 3 / 3 totaltotal 30.2% 29 / 96 CC 16 SCAR 41.3
CC
16
Cov
27%
match_with_regexp_pattern
110.0
get_regexp_pattern_pattern
42.0
list_regexp_pattern_modules
6.0
CRAP 62.2
↑ Summary
1 package App::RegexpPatternUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict 'subs', 'vars';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 ▶use Regexp::Pattern;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
10 1 our $DATE = '2022-05-08'; # DATE
11 1 our $DIST = 'App-RegexpPatternUtils'; # DIST
12 1 our $VERSION = '0.008'; # VERSION
13  
14 1 our %SPEC;
15  
16 1 our %args_common_pattern = (
17 pattern => {
18 summary => "Name of pattern, with module prefix but without the 'Regexp::Pattern'",
19 schema => 'regexppattern::name*',
20 req => 1,
21 pos => 0,
22 },
23 gen_args => {
24 'x.name.is_plural' => 1,
25 'x.name.singular' => 'gen_arg',
26 summary => 'Supply generator arguments',
27 description => <<'_',
28  
29 If pattern is a dynamic pattern (generated on-demand) and the generator requires
30 some arguments, you can supply them here.
31  
32 _
33 cmdline_aliases => {A=>{}},
34 schema => ['hash*', of=>'str*'],
35 },
36 );
37  
38 1 our %args_common_get_pattern = (
39 %args_common_pattern,
40 anchor => {
41 summary => 'Generate an anchored version of the pattern',
42 schema => 'bool*',
43 },
44 );
45  
46 $SPEC{get_regexp_pattern_pattern} = {
47 1 v => 1.1,
48 summary => 'Get a Regexp::Pattern::* pattern',
49 args => {
50 %args_common_pattern,
51 },
52 examples => [
53 {
54 args => {pattern=>'YouTube/video_id'},
55 },
56 {
57 summary=>"Generate variant A of Example::re3",
58 argv => ['Example::re3', '--gen-arg', 'variant=A'],
59 },
60 {
61 summary=>"Generate variant B of Example::re3",
62 argv => ['Example::re3', '--gen-arg', 'variant=B'],
63 },
64 ],
65 links => [
66 ],
67 };
68 0 ▶sub get_regexp_pattern_pattern {
Subroutine get_regexp_pattern_pattern: not called CC=6 SCAR=37.4
Pod documented
69 0 my %args = @_;
70  
71 0 my $name = $args{pattern};
72 0 $name =~ s!(/|\.)!::!g;
73  
74 0 ▶ my $re = re($name, $args{gen_args} // {});
Condition0%0/2
$args{"gen_args"} // {}
l!l
00
MC/DC0%0/1
$args{"gen_args"} // {}
$args{"gen_args"}
Truth table0%0/2
A
A= $args{"gen_args"}
Aresult
11
00
75  
76 0 ▶ if (-t STDOUT && $args{-cmdline} &&
Branch: 1 branch
BranchTrueFalseTotal
if (-t STDOUT and $args{'-cmdline'} and ($args{'-cmdline_r'}{'format'} // 'text') =~ /text/) { } 0 0 0
Condition0%0/8
-t STDOUT and $args{"-cmdline"} and ($args{"-cmdline_r"}{"format"} // "text") =~ /text/
!ll&&!rl&&r
000
-t STDOUT and $args{"-cmdline"}
!ll&&!rl&&r
000
$args{"-cmdline_r"}{"format"} // "text"
l!l
00
MC/DC0%0/3
-t STDOUT and $args{"-cmdline"} and ($args{"-cmdline_r"}{"format"} // "text") =~ /text/
-t STDOUT $args{"-cmdline"} $args{"-cmdline_r"}{"format"}
Truth table0%0/4
A and B and C
A= -t STDOUTB= $args{"-cmdline"}C= $args{"-cmdline_r"}{"format"}
ABCresult
0XX0
10X0
1100
1111
77 ($args{-cmdline_r}{format} // 'text') =~ /text/) { ## no critic: InputOutput::ProhibitInteractiveTest
78 0 require Data::Dump::Color;
79 0 return [200, "OK", Data::Dump::Color::dump($re) . "\n",
80 {'cmdline.skip_format'=>1}];
81 } else {
82 0 return [200, "OK", "$re"];
83 }
84 }
85  
86 $SPEC{list_regexp_pattern_modules} = {
87 1 v => 1.1,
88 summary => 'List all installed Regexp::Pattern::* modules',
89 };
90 0 ▶sub list_regexp_pattern_modules {
Subroutine list_regexp_pattern_modules: not called CC=2 SCAR=17.9
Pod documented
91 0 require Module::List::Tiny;
92  
93 0 my $res = Module::List::Tiny::list_modules(
94 'Regexp::Pattern::', {list_modules=>1, recurse=>1});
95 0 my @rows;
96 0 for (sort keys %$res) {
97 0 s/\ARegexp::Pattern:://;
98 0 push @rows, $_;
99 }
100 0 [200, "OK", \@rows];
101 }
102  
103 $SPEC{match_with_regexp_pattern} = {
104 1 v => 1.1,
105 summary => 'Match a string against a Regexp::Pattern pattern',
106 args => {
107 %args_common_get_pattern,
108 string => {
109 schema => 'str*',
110 req => 1,
111 pos => 1,
112 },
113 captures => {
114 summary => 'Return array of captures instead of just a boolean status',
115 schema => 'bool*',
116 },
117 quiet => {
118 schema => 'bool*',
119 cmdline_aliases => {q=>{}},
120 },
121 },
122 examples => [
123 {
124 summary => 'A non-match',
125 args => {pattern=>'YouTube/video_id', string=>'foo'},
126 },
127 {
128 summary => 'A match',
129 args => {pattern=>'YouTube/video_id', string=>'Yb4EGj4_uS0'},
130 },
131 ],
132 links => [
133 {url=>'prog:get-regexp-pattern-pattern'},
134 {url=>'prog:rpgrep'},
135 ],
136 };
137 0 ▶sub match_with_regexp_pattern {
Subroutine match_with_regexp_pattern: not called CC=10 SCAR=47.0
Pod documented
138 0 my %args = @_;
139  
140 0 my $name = $args{pattern};
141 0 $name =~ s!(/|\.)!::!g;
142  
143 0 ▶ my %gen_args = %{ $args{gen_args} // {} };
Condition0%0/2
$args{"gen_args"} // {}
l!l
00
MC/DC0%0/1
$args{"gen_args"} // {}
$args{"gen_args"}
Truth table0%0/2
A
A= $args{"gen_args"}
Aresult
11
00
144 0 ▶ $gen_args{-anchor} = 1 if $args{anchor};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"anchor"} 0 0 0
145  
146 0 my $re = re($name, \%gen_args);
147  
148 0 my $matches;
149 my @captures;
150 0 ▶ if ($args{string} =~ $re) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"string"} =~ /$re/) 0 0 0
151 0 $matches = 1;
152 0 ▶ if ($args{captures}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"captures"}) 0 0 0
153 # for perls that do not have @{^CAPTURE}
154 0 for (1..@- - 1) {
155 0 push @captures, ${$_};
156 }
157 }
158 }
159  
160 0 ▶ my $msg = "String ".($matches ? "matches" : "DOES NOT match")." regexp pattern $name";
Branch: 1 branch
BranchTrueFalseTotal
$matches ? : 0 0 0
161 [
162 200, "OK",
163 0 ▶ $args{captures} ? \@captures : $args{quiet} ? undef : $msg,
Branch: 3 branches
BranchTrueFalseTotal
$args{'captures'} ? : 0 0 0
$args{'quiet'} ? : 0 0 0
$matches ? : 0 0 0
164 {"cmdline.exit_code"=>$matches ? 0:1},
165 ];
166 }
167  
168 1 1;
169 # ABSTRACT: CLI utilities related to Regexp::Pattern
170  
171 __END__
↑ Summary