Common.pm

filter: Statementstmt 88.8% 151 / 170 Branchbran 66.6% 44 / 66 Conditioncond 58.8% 10 / 17 MC/DCmcdc 40.0% 4 / 10 Subroutinesub 83.3% 25 / 30 Podpod 37.5% 3 / 8 totaltotal 78.7% 237 / 301 CC 47 SCAR 27.3
CC
47
Cov
81%
import
38.0
pattern
12.6
_decache
6.0
CRAP 15.3
↑ Summary Common/number.pm »
1 package Regexp::Common;
2  
3 72 ▶use 5.10.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 72 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 72 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 72 ▶no warnings 'syntax';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 72 our $VERSION = '2024080801';
10 72 our %RE;
11 72 our %sub_interface;
12 72 our $AUTOLOAD;
13  
14  
15 0 ▶sub _croak {
Subroutine _croak: not called CC=1 SCAR=6.9
16 0 require Carp;
17 0 goto &Carp::croak;
18 }
19  
20 0 ▶sub _carp {
Subroutine _carp: not called CC=1 SCAR=6.9
21 0 require Carp;
22 0 goto &Carp::carp;
23 }
24  
25 16563 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
26 16563 my ($class, @data) = @_;
27 16563 my %self;
28 16563 tie %self, $class, @data;
29 16563 return \%self;
30 }
31  
32 18726 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=1 SCAR=0.0
33 18726 my ($class, @data) = @_;
34 18726 bless \@data, $class;
35 }
36  
37 16563 ▶sub FETCH {
Subroutine FETCH: called CC=1 SCAR=0.0
38 16563 my ($self, $extra) = @_;
39 16563 return bless ref($self)->new(@$self, $extra), ref($self);
40 }
41  
42 72 my %imports = map {$_ => "Regexp::Common::$_"}
43 qw /balanced CC comment delimited lingua list
44 net number profanity SEN URI whitespace
45 zip/;
46  
47 2163 ▶sub import {
Subroutine import: called CC=24 SCAR=36.4
48 2163 shift; # Shift off the class.
49 2163 tie %RE, __PACKAGE__;
50 {
51 72 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
52 2163 *{caller() . "::RE"} = \%RE;
53 }
54  
55 2163 my $saw_import;
56 my $no_defaults;
57 2163 my %exclude;
58 2163 foreach my $entry (grep {!/^RE_/} @_) {
59 6179 ▶ if ($entry eq 'pattern') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry eq "pattern") 2060 4119 6179
60 72 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
61 2060 *{caller() . "::pattern"} = \&pattern;
62 2060 next;
63 }
64 # This used to prevent $; from being set. We still recognize it,
65 # but we won't do anything.
66 4119 ▶ if ($entry eq 'clean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry eq "clean") 2059 2060 4119
67 2059 next;
68 }
69 2060 ▶ if ($entry eq 'no_defaults') {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry eq "no_defaults") 2060 0 2060
70 2060 $no_defaults ++;
71 2060 next;
72 }
73 0 ▶ if (my $module = $imports {$entry}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $module = $imports{$entry}) 0 0 0
74 0 $saw_import ++;
75 0 eval "require $module;";
76 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
77 0 next;
78 }
79 0 ▶ if ($entry =~ /^!(.*)/ && $imports {$1}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /^!(.*)/ and $imports{$1}) 0 0 0
Condition0%0/3
$entry =~ /^!(.*)/ and $imports{$1}
!ll&&!rl&&r
000
MC/DC0%0/2
$entry =~ /^!(.*)/ and $imports{$1}
$entry =~ /^!(.*)/ $imports{$1}
Truth table0%0/3
A and B
A= $entry =~ /^!(.*)/B= $imports{$1}
ABresult
0X0
100
111
80 0 $exclude {$1} ++;
81 0 next;
82 }
83 # As a last resort, try to load the argument.
84 0 ▶ my $module = $entry =~ /^Regexp::Common/
Branch: 1 branch
BranchTrueFalseTotal
$entry =~ /^Regexp::Common/ ? : 0 0 0
85 ? $entry
86 : "Regexp::Common::" . $entry;
87 0 eval "require $module;";
88 0 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 0 0
89 }
90  
91 2163 ▶ unless ($saw_import || $no_defaults) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($saw_import or $no_defaults) 103 2060 2163
Condition66%2/3
$saw_import or $no_defaults
l!l&&r!l&&!r
02060103
MC/DC50%1/2
$saw_import or $no_defaults
$saw_import $no_defaults
Truth table66%2/3
A or B
A= $saw_importB= $no_defaults
ABresult
1X1
011
000
92 103 foreach my $module (values %imports) {
93 1339 ▶ next if $exclude {$module};
Branch: 1 branch
BranchTrueFalseTotal
if $exclude{$module} 0 1339 1339
94 1339 eval "require $module;";
95 1339 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 1339 1339
96 }
97 }
98  
99 2163 my %exported;
100 2163 foreach my $entry (grep {/^RE_/} @_) {
101 12 ▶ if ($entry =~ /^RE_(\w+_)?ALL$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry =~ /^RE_(\w+_)?ALL$/) { } 6 6 12
102 6 ▶ my $m = defined $1 ? $1 : "";
Branch: 1 branch
BranchTrueFalseTotal
defined $1 ? : 4 2 6
103 6 my $re = qr /^RE_${m}.*$/;
104 6 ▶ while (my ($sub, $interface) = each %sub_interface) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %Regexp::Common::sub_interface) 1038 6 1044
105 1038 ▶ next if $exported {$sub};
Branch: 1 branch
BranchTrueFalseTotal
if $exported{$sub} 0 1038 1038
106 1038 ▶ next unless $sub =~ /$re/;
Branch: 1 branch
BranchTrueFalseTotal
unless $sub =~ /$re/ 308 730 1038
107 {
108 72 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
109 730 *{caller() . "::$sub"} = $interface;
110 }
111 730 $exported {$sub} ++;
112 }
113 }
114 else {
115 6 ▶ next if $exported {$entry};
Branch: 1 branch
BranchTrueFalseTotal
if $exported{$entry} 0 6 6
116 _croak "Can't export unknown subroutine &$entry"
117 6 ▶ unless $sub_interface {$entry};
Branch: 1 branch
BranchTrueFalseTotal
unless $Regexp::Common::sub_interface{$entry} 0 6 6
118 {
119 72 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
120 6 *{caller() . "::$entry"} = $sub_interface {$entry};
121 }
122 6 $exported {$entry} ++;
123 }
124 }
125 }
126  
127 0 ▶sub AUTOLOAD { _croak "Can't $AUTOLOAD" }
Subroutine AUTOLOAD: not called CC=1 SCAR=6.9
128  
129 0 ▶sub DESTROY {}
Subroutine DESTROY: not called CC=1 SCAR=0.0
130  
131 72 my %cache;
132  
133 72 my $fpat = qr/^(-\w+)/;
134  
135 1364140 ▶sub _decache {
Subroutine _decache: called CC=6 SCAR=18.0
136 1364140 my @args = @{tied %{$_[0]}};
137 1364140 my @nonflags = grep {!/$fpat/} @args;
138 1364140 my $cache = get_cache(@nonflags);
139 _croak "Can't create unknown regex: \$RE{"
140 . join("}{",@args) . "}"
141 1364140 ▶ unless exists $cache->{__VAL__};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $cache->{"__VAL__"} 0 1364140 1364140
142 _croak "Perl $] does not support the pattern "
143 . "\$RE{" . join("}{",@args)
144 . "}.\nYou need Perl $cache->{__VAL__}{version} or later"
145 1364140 ▶ unless ($cache->{__VAL__}{version}||0) <= $];
Branch: 1 branch
BranchTrueFalseTotal
unless ($cache->{"__VAL__"}{"version"} || 0) <= $] 0 1364140 1364140
Condition100%2/2
$cache->{'__VAL__'}{'version'} || 0
l!l
28531361287
MC/DC100%1/1
$cache->{'__VAL__'}{'version'} || 0
$cache->{'__VAL__'}{'version'}
Truth table100%2/2
A
A= $cache->{'__VAL__'}{'version'}
Aresult
11
00
146 1364140 my %flags = ( %{$cache->{__VAL__}{default}},
147 1364140 ▶ map { /$fpat\Q$;\E(.*)/ ? ($1 => $2)
Branch: 2 branches
BranchTrueFalseTotal
/$fpat\Q$;\E(.*)/ ? : 54239 3410106 3464345
/$fpat/ ? : 675739 2734367 3410106
148 : /$fpat/ ? ($1 => undef)
149 : ()
150 } @args);
151 1364140 $cache->{__VAL__}->_clone_with(\@args, \%flags);
152 }
153  
154 72 ▶use overload q{""} => \&_decache;
Subroutine BEGIN: called CC=1 SCAR=0.0
155  
156  
157 1376496 ▶sub get_cache {
Subroutine get_cache: called CC=3 SCAR=11.0
Pod undocumented
158 1376496 my $cache = \%cache;
159 1376496 foreach (@_) {
160 $cache = $cache->{$_}
161 2760073 ▶ || ($cache->{$_} = {});
Condition100%2/2
$cache->{$_} || ($cache->{$_} = {})
l!l
274700713066
MC/DC100%1/1
$cache->{$_} || ($cache->{$_} = {})
$cache->{$_}
Truth table100%2/2
A
A= $cache->{$_}
Aresult
11
00
162 }
163 1376496 return $cache;
164 }
165  
166 0 ▶sub croak_version {
Subroutine croak_version: not called CC=1 SCAR=6.9
Pod undocumented
167 0 my ($entry, @args) = @_;
168 }
169  
170 12356 ▶sub pattern {
Subroutine pattern: called CC=12 SCAR=25.4
Pod documented
171 12356 my %spec = @_;
172 _croak 'pattern() requires argument: name => [ @list ]'
173 12356 ▶ unless $spec{name} && ref $spec{name} eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
unless $spec{"name"} and ref $spec{"name"} eq "ARRAY" 0 12356 12356
Condition33%1/3
$spec{"name"} and ref $spec{"name"} eq "ARRAY"
!ll&&!rl&&r
0012356
MC/DC0%0/2
$spec{"name"} and ref $spec{"name"} eq "ARRAY"
$spec{"name"} ref $spec{"name"} eq "ARRAY"
Truth table33%1/3
A and B
A= $spec{"name"}B= ref $spec{"name"} eq "ARRAY"
ABresult
0X0
100
111
174 _croak 'pattern() requires argument: create => $sub_ref_or_string'
175 12356 ▶ unless $spec{create};
Branch: 1 branch
BranchTrueFalseTotal
unless $spec{"create"} 0 12356 12356
176  
177 12356 ▶ if (ref $spec{create} ne "CODE") {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $spec{"create"} ne "CODE") 7597 4759 12356
178 7597 my $fixed_str = "$spec{create}";
179 109369 ▶ $spec{create} = sub { $fixed_str }
Subroutine __ANON__: called CC=1 SCAR=0.0
180 7597 }
181  
182 12356 my @nonflags;
183 my %default;
184 12356 foreach ( @{$spec{name}} ) {
185 34226 ▶ if (/$fpat=(.*)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/$fpat=(.*)/) { } 8449 25777 34226
elsif (/$fpat\s*$/) { } 71 25706 25777
186 8449 $default{$1} = $2;
187 }
188 elsif (/$fpat\s*$/) {
189 71 $default{$1} = undef;
190 }
191 else {
192 25706 push @nonflags, $_;
193 }
194 }
195  
196 12356 my $entry = get_cache(@nonflags);
197  
198 12356 ▶ if ($entry->{__VAL__}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($entry->{"__VAL__"}) 0 12356 12356
199 0 _carp "Overriding \$RE{"
200 . join("}{",@nonflags)
201 . "}";
202 }
203  
204 $entry->{__VAL__} = bless {
205 create => $spec{create},
206 match => $spec{match} || \&generic_match,
207 subs => $spec{subs} || \&generic_subs,
208 version => $spec{version},
209 12356 ▶ default => \%default,
Condition75%3/4
$spec{'match'} || \&Regexp::Common::generic_match
l!l
012356
$spec{'subs'} || \&Regexp::Common::generic_subs
l!l
49711859
MC/DC0%0/1
$spec{'match'} || \&Regexp::Common::generic_match
$spec{'match'}
MC/DC100%1/1
$spec{'subs'} || \&Regexp::Common::generic_subs
$spec{'subs'}
Truth table50%1/2
A
A= $spec{'match'}
Aresult
11
00
Truth table100%2/2
A
A= $spec{'subs'}
Aresult
11
00
210 }, 'Regexp::Common::Entry';
211  
212 12356 foreach (@nonflags) {s/\W/X/g}
213 12356 my $subname = "RE_" . join ("_", @nonflags);
214 35555 ▶ $sub_interface{$subname} = sub {
Subroutine __ANON__: called CC=4 SCAR=14.0
215 35555 ▶ push @_ => undef if @_ % 2;
Branch: 1 branch
BranchTrueFalseTotal
if @_ % 2 1 35554 35555
216 35555 my %flags = @_;
217 my $pat = $spec{create}->($entry->{__VAL__},
218 35555 {%default, %flags}, \@nonflags);
219 0 ▶ if (exists $flags{-keep}) { $pat =~ s/\Q(?k:/(/g; }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $flags{'-keep'}) { } 0 35555 35555
220 35555 else { $pat =~ s/\Q(?k:/(?:/g; }
221 35555 ▶ return exists $flags {-i} ? qr /(?i:$pat)/ : qr/$pat/;
Branch: 1 branch
BranchTrueFalseTotal
exists $flags{'-i'} ? : 32 35523 35555
222 12356 };
223  
224 12356 return 1;
225 }
226  
227 3320 ▶sub generic_match {$_ [1] =~ /$_[0]/}
Subroutine generic_match: called CC=1 SCAR=0.0
Pod undocumented
228 3316 ▶sub generic_subs {$_ [1] =~ s/$_[0]/$_[2]/}
Subroutine generic_subs: called CC=1 SCAR=0.0
Pod undocumented
229  
230 3320 ▶sub matches {
Subroutine matches: called CC=1 SCAR=0.0
Pod undocumented
231 3320 my ($self, $str) = @_;
232 3320 my $entry = $self -> _decache;
233 3320 $entry -> {match} -> ($entry, $str);
234 }
235  
236 3328 ▶sub subs {
Subroutine subs: called CC=1 SCAR=0.0
Pod documented
237 3328 my ($self, $str, $newstr) = @_;
238 3328 my $entry = $self -> _decache;
239 3328 $entry -> {subs} -> ($entry, $str, $newstr);
240 3328 return $str;
241 }
242  
243  
244 package Regexp::Common::Entry;
245 # use Carp;
246  
247 use overload
248 72 ▶ q{""} => sub {
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine __ANON__: called CC=3 SCAR=11.0
249 1364129 my ($self) = @_;
250 1364129 my $pat = $self->{create}->($self, $self->{flags}, $self->{args});
251 1364125 ▶ if (exists $self->{flags}{-keep}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{'flags'}{'-keep'}) { } 642250 721875 1364125
252 642250 $pat =~ s/\Q(?k:/(/g;
253 }
254 else {
255 721875 $pat =~ s/\Q(?k:/(?:/g;
256 }
257 1364125 ▶ if (exists $self->{flags}{-i}) { $pat = "(?i)$pat" }
Branch: 1 branch
BranchTrueFalseTotal
if (exists $self->{"flags"}{"-i"}) 32 1364093 1364125
258 1364125 return $pat;
259 72 };
260  
261 1364140 ▶sub _clone_with {
Subroutine _clone_with: called CC=1 SCAR=0.0
262 1364140 my ($self, $args, $flags) = @_;
263 1364140 bless { %$self, args=>$args, flags=>$flags }, ref $self;
264 }
265  
266 72 1;
267  
268 __END__
↑ Summary Common/number.pm »