blib/lib/Regexp/VerbalExpressions.pm

filter: Statementstmt 96.8% 92 / 95 Branchbran 81.2% 13 / 16 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 96.6% 29 / 30 Podpod 75.0% 18 / 24 totaltotal 92.1% 152 / 165 CC 10 SCAR 5.1
CC
10
Cov
95%
stop_at_first
3.3
_build_modifier
3.0
regex
3.0
CRAP 1.7
↑ Summary
1 package Regexp::VerbalExpressions;
2 4 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use parent qw/Exporter/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use overload fallback => 1, q{""} => \&source, 'qr' => \&regex;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 4 our $VERSION = "0.02";
9  
10 4 our @EXPORT = qw/verex/;
11  
12 38 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
13 38 my $class = shift;
14 38 return bless {
15 s => '',
16 modifiers => {
17 i => 0,
18 m => 0,
19 s => 0,
20 x => 0,
21 },
22 global_matching => 0,
23 }, $class;
24 }
25  
26 38 ▶sub verex { __PACKAGE__->new() }
Subroutine verex: called CC=1 SCAR=0.0
Pod undocumented
27  
28 118 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
29 118 my ($self, $value) = @_;
30 118 $self->{s} .= $value;
31 118 return $self;
32 }
33  
34 36 ▶sub regex {
Subroutine regex: called CC=3 SCAR=11.0
Pod undocumented
35 36 my $self = shift;
36 36 my $enabled = my $disabled = '';
37 36 for my $modifier (sort keys %{$self->{modifiers}}) {
38 144 ▶ if ($self->{modifiers}{$modifier}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'modifiers'}{$modifier}) { } 5 139 144
39 5 $enabled .= $modifier;
40 } else {
41 139 $disabled .= $modifier;
42 }
43 }
44 36 return qr/(?$enabled-$disabled:$self->{s})/;
45 }
46 4 *compile = \&regex;
47  
48 1 ▶sub source {
Subroutine source: called CC=1 SCAR=0.0
Pod undocumented
49 1 my $self = shift;
50 1 return $self->{s};
51 }
52 4 *raw = *value = \&source;
53  
54 29 ▶sub find {
Subroutine find: called CC=1 SCAR=0.0
Pod documented
55 29 my ($self, $value) = @_;
56 29 return $self->add('(?:' . quotemeta($value) . ')');
57 }
58 4 *then = \&find;
59  
60 31 ▶sub start_of_line {
Subroutine start_of_line: called CC=1 SCAR=0.0
Pod documented
61 31 my $self = shift;
62 31 return $self->add('^');
63 }
64  
65 17 ▶sub end_of_line {
Subroutine end_of_line: called CC=1 SCAR=0.0
Pod documented
66 17 my $self = shift;
67 17 return $self->add('$');
68 }
69  
70 5 ▶sub maybe {
Subroutine maybe: called CC=1 SCAR=0.0
Pod documented
71 5 my ($self, $value) = @_;
72 5 return $self->add('(?:' . quotemeta($value) . ')?');
73 }
74  
75 10 ▶sub anything {
Subroutine anything: called CC=1 SCAR=0.0
Pod documented
76 10 my $self = shift;
77 10 return $self->add('(?:.*)');
78 }
79  
80 2 ▶sub anything_but {
Subroutine anything_but: called CC=1 SCAR=0.0
Pod documented
81 2 my ($self, $value) = @_;
82 2 return $self->add('(?:[^' . quotemeta($value) . ']*)');
83 }
84  
85 1 ▶sub something {
Subroutine something: called CC=1 SCAR=0.0
Pod undocumented
86 1 my $self = shift;
87 1 return $self->add('(?:.+)');
88 }
89  
90 1 ▶sub something_but {
Subroutine something_but: called CC=1 SCAR=0.0
Pod undocumented
91 1 my ($self, $value) = @_;
92 1 return $self->add('(?:[^' . quotemeta($value) . ']+)');
93 }
94  
95 5 ▶sub line_break {
Subroutine line_break: called CC=1 SCAR=0.0
Pod documented
96 5 my $self = shift;
97 5 return $self->add('(?:\n|(?:\r\n))');
98 }
99 4 *br = \&line_break;
100  
101 4 ▶sub tab {
Subroutine tab: called CC=1 SCAR=0.0
Pod documented
102 4 my $self = shift;
103 4 return $self->add('\t');
104 }
105  
106 6 ▶sub word {
Subroutine word: called CC=1 SCAR=0.0
Pod documented
107 6 my $self = shift;
108 6 return $self->add('\w+');
109 }
110  
111 2 ▶sub any {
Subroutine any: called CC=1 SCAR=0.0
Pod documented
112 2 my ($self, $value) = @_;
113 2 return $self->add('[' . quotemeta($value) . ']');
114 }
115 4 *any_of = \&any;
116  
117 2 ▶sub range {
Subroutine range: called CC=2 SCAR=6.9
Pod documented
118 2 my ($self, @args) = @_;
119 2 my $value = '[';
120 2 ▶ for (my $i = 0; $i < @args; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @args) 3 2 5
121 3 $value .= quotemeta($args[$i]) . '-' . quotemeta($args[$i + 1]);
122 }
123 2 $value .= ']';
124 2 return $self->add($value);
125 }
126  
127 0 ▶sub multiple {
Subroutine multiple: not called CC=1 SCAR=6.9
Pod documented
128 0 my ($self, $value) = @_;
129 0 $self->add(quotemeta($value) . '+');
130 }
131  
132 2 ▶sub or {
Subroutine or: called CC=2 SCAR=6.9
Pod documented
133 2 my ($self, $value) = @_;
134 2 $self->add('|');
135 2 ▶ return defined $value ? $self->find($value) : $self;
Branch: 1 branch
BranchTrueFalseTotal
defined $value ? : 1 1 2
136 }
137 4 *alternatively = \&or;
138  
139 3 ▶sub replace {
Subroutine replace: called CC=2 SCAR=6.9
Pod documented
140 3 my ($self, $source, $value) = @_;
141 3 my $re = $self->regex;
142 3 ▶ if ($self->{global_matching}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'global_matching'}) { } 2 1 3
143 2 $source =~ s/$re/$value/g;
144 } else {
145 1 $source =~ s/$re/$value/;
146 }
147 3 return $source;
148 }
149  
150 5 ▶sub _build_modifier {
Subroutine _build_modifier: called CC=3 SCAR=11.0
151 5 my ($self, $modifier, $value) = @_;
152 5 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 2 3 5
153 # XXX verex->with_any_case;
154 2 $self->{modifiers}{$modifier} = 1;
155 } else {
156 3 ▶ $self->{modifiers}{$modifier} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 3 0 3
157 }
158 }
159  
160 3 ▶sub with_any_case {
Subroutine with_any_case: called CC=1 SCAR=0.0
Pod documented
161 3 my ($self, $value) = @_;
162 3 $self->_build_modifier(i => $value);
163 3 return $self;
164 }
165  
166 2 ▶sub stop_at_first {
Subroutine stop_at_first: called CC=3 SCAR=12.0
Pod documented
167 2 my ($self, $value) = @_;
168 2 ▶ if (!defined $value) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $value) { } 0 2 2
169 0 $self->{global_matching} = 1;
170 } else {
171 2 ▶ $self->{global_matching} = $value ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$value ? : 2 0 2
172 }
173 2 return $self;
174 }
175  
176 2 ▶sub search_one_line {
Subroutine search_one_line: called CC=1 SCAR=0.0
Pod documented
177 2 my ($self, $value) = @_;
178 2 $self->_build_modifier(m => $value);
179 2 return $self;
180 }
181  
182 4 1;
183 __END__
↑ Summary