RegGrp.pm

filter: Statementstmt 88.2% 143 / 162 Branchbran 71.7% 33 / 46 Conditioncond 71.4% 10 / 14 MC/DCmcdc 22.2% 2 / 9 Subroutinesub 100% 20 / 20 Podpod 8.3% 1 / 12 totaltotal 79.4% 209 / 263 CC 29 SCAR 18.8
CC
29
Cov
84%
new
14.7
_process
7.0
exec
6.4
CRAP 6.5
« RegGrp/Data.pm ↑ Summary
1 package Regexp::RegGrp;
2  
3 3 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Regexp::RegGrp::Data;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 BEGIN {
10 3 ▶ if ( $] < 5.010000 ) {
Subroutine BEGIN: called CC=2 SCAR=10.5
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 3 3
11 0 require re;
12 0 re->import( 'eval' );
13 }
14 }
15  
16 use constant {
17 3 ESCAPE_BRACKETS => qr~(?<!\\)\[[^\]]+(?<!\\)\]|\(\?([\^dlupimsx-]+:|[:=!><])~,
18 ESCAPE_CHARS => qr~\\.~,
19 BRACKETS => qr~\(~,
20 BACK_REF => qr~(?:\\g?(\d\d*)|\\g\{(\d+)\})~
21 3 ▶};
Subroutine BEGIN: called CC=1 SCAR=0.0
22  
23 # =========================================================================== #
24  
25 3 our $VERSION = '1.001_002';
26  
27 sub new {
28 14 ▶ my ( $class, $in_ref ) = @_;
Subroutine new: called CC=12 SCAR=26.9
Pod undocumented
29 14 my $self = {};
30  
31 14 bless( $self, $class );
32  
33 14 ▶ if ( ref( $in_ref ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_ref ne "HASH") 1 13 14
34 1 carp( 'First argument must be a hashref!' );
35 1 return;
36 }
37  
38 13 ▶ unless ( exists( $in_ref->{reggrp} ) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $in_ref->{"reggrp"}) 0 13 13
39 0 carp( 'Key "reggrp" does not exist in input hashref!' );
40 0 return;
41 }
42  
43 13 ▶ if ( ref( $in_ref->{reggrp} ) ne 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_ref->{"reggrp"} ne "ARRAY") 0 13 13
44 0 carp( 'Value for key "reggrp" must be an arrayref!' );
45 0 return;
46 }
47  
48 13 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (ref $in_ref->{"restore_pattern"} and ref $in_ref->{"restore_pattern"} ne "Regexp") 0 13 13
Condition66%2/3
ref $in_ref->{"restore_pattern"} and ref $in_ref->{"restore_pattern"} ne "Regexp"
!ll&&!rl&&r
1120
MC/DC0%0/2
ref $in_ref->{"restore_pattern"} and ref $in_ref->{"restore_pattern"} ne "Regexp"
ref $in_ref->{"restore_pattern"} ref $in_ref->{"restore_pattern"} ne "Regexp"
Truth table66%2/3
A and B
A= ref $in_ref->{"restore_pattern"}B= ref $in_ref->{"restore_pattern"} ne "Regexp"
ABresult
0X0
100
111
49 ref( $in_ref->{restore_pattern} ) and
50 ref( $in_ref->{restore_pattern} ) ne 'Regexp'
51 ) {
52 0 carp( 'Value for key "restore_pattern" must be a scalar or regexp!' );
53 0 return;
54 }
55  
56 13 my $no = 0;
57  
58 map {
59 38 $no++;
60  
61 my $reggrp_data = Regexp::RegGrp::Data->new(
62 {
63 regexp => $_->{regexp},
64 replacement => $_->{replacement},
65 store => $_->{store},
66 modifier => $_->{modifier},
67 restore_pattern => $in_ref->{restore_pattern}
68 }
69 38 );
70  
71 38 ▶ unless ( $reggrp_data ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($reggrp_data) 0 38 38
72 0 carp( 'RegGrp No ' . $no . ' in arrayref is malformed!' );
73 0 return;
74 }
75  
76 38 $self->reggrp_add( $reggrp_data );
77 13 } @{$in_ref->{reggrp}};
78  
79 13 ▶ my $restore_pattern = $in_ref->{restore_pattern} || qr~\x01(\d+)\x01~;
Condition66%2/3
$in_ref->{'restore_pattern'} || qr/\x01(\d+)\x01/
l!l&&r!l&&!r
2110
MC/DC0%0/2
$in_ref->{'restore_pattern'} || qr/\x01(\d+)\x01/
$in_ref->{'restore_pattern'} qr/\x01(\d+)\x01/
Truth table66%2/3
A || B
A= $in_ref->{'restore_pattern'}B= qr/\x01(\d+)\x01/
ABresult
1X1
011
000
80 13 $self->{_restore_pattern} = qr/$restore_pattern/;
81  
82 13 my $offset = 1;
83 13 my $midx = 0;
84  
85 # In perl versions < 5.10 hash %+ doesn't exist, so we have to initialize it
86 $self->{_re_str} = ( ( $] < 5.010000 ) ? '(?{ %+ = (); })' : '' ) . join(
87 '|',
88 map {
89 13 ▶ my $re = $_->regexp();
Branch: 1 branch
BranchTrueFalseTotal
$] < 5.01 ? : 0 13 13
90 # Count backref brackets
91 38 $re =~ s/${\(ESCAPE_CHARS)}//g;
92 38 $re =~ s/${\(ESCAPE_BRACKETS)}//g;
93 38 my @nparen = $re =~ /${\(BRACKETS)}/g;
94  
95 38 $re = $_->regexp();
96  
97 38 my $backref_pattern = '\\g{%d}';
98  
99 38 ▶ if ( $] < 5.010000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < "5.01") 0 38 38
100 0 $backref_pattern = '\\%d';
101 }
102  
103 38 ▶ $re =~ s/${\(BACK_REF)}/sprintf( $backref_pattern, $offset + ( $1 || $2 ) )/eg;
Condition66%2/3
$1 || $2
l!l&&r!l&&!r
1110
MC/DC0%0/2
$1 || $2
$1 $2
Truth table66%2/3
A || B
A= $1B= $2
ABresult
1X1
011
000
104  
105 38 my $ret;
106  
107 38 ▶ if ( $] < 5.010000 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($] < 5.01) { } 0 38 38
108 # In perl versions < 5.10 we need to fill %+ hash manually
109 # perl 5.8 doesn't reset the %+ hash correctly if there are zero-length submatches
110 # so this is also done here
111 0 $ret = '(' . $re . ')' . '(?{ %+ = ( \'_' . $midx++ . '\' => $^N ); })';
112 }
113 else {
114 38 $ret = '(?\'_' . $midx++ . '\'' . $re . ')';
115 }
116  
117 38 $offset += scalar( @nparen ) + 1;
118  
119 38 $ret;
120  
121 } $self->reggrp_array()
122 );
123  
124 13 return $self;
125 }
126  
127 # re_str methods
128  
129 sub re_str {
130 26 ▶ my $self = shift;
Subroutine re_str: called CC=1 SCAR=0.0
Pod undocumented
131  
132 26 return $self->{_re_str};
133 }
134  
135 # /re_str methods
136  
137 # restore_pattern methods
138  
139 sub restore_pattern {
140 12 ▶ my $self = shift;
Subroutine restore_pattern: called CC=1 SCAR=0.0
Pod documented
141  
142 12 return $self->{_restore_pattern};
143 }
144  
145 # /restore_pattern methods
146  
147 # store_data methods
148  
149 sub store_data_add {
150 16 ▶ my ( $self, $data ) = @_;
Subroutine store_data_add: called CC=1 SCAR=0.0
Pod undocumented
151  
152 16 push( @{$self->{_store_data}}, $data );
153 }
154  
155 sub store_data_by_idx {
156 8 ▶ my ( $self, $idx ) = @_;
Subroutine store_data_by_idx: called CC=1 SCAR=0.0
Pod undocumented
157  
158 8 return $self->{_store_data}->[$idx];
159 }
160  
161 sub store_data_count {
162 48 ▶ my $self = shift;
Subroutine store_data_count: called CC=2 SCAR=6.9
Pod undocumented
163  
164 48 ▶ return scalar( @{$self->{_store_data} || []} );
Branch: 1 branch
BranchTrueFalseTotal
$self->{"_store_data"} or [] 20 28 48
165 }
166  
167 sub flush_stored {
168 17 ▶ my $self = shift;
Subroutine flush_stored: called CC=1 SCAR=0.0
Pod undocumented
169  
170 17 $self->{_store_data} = [];
171 }
172  
173 # /store_data methods
174  
175 # reggrp methods
176  
177 sub reggrp_add {
178 38 ▶ my ( $self, $reggrp ) = @_;
Subroutine reggrp_add: called CC=1 SCAR=0.0
Pod undocumented
179  
180 38 push( @{$self->{_reggrp}}, $reggrp );
181 }
182  
183 sub reggrp_array {
184 13 ▶ my $self = shift;
Subroutine reggrp_array: called CC=1 SCAR=0.0
Pod undocumented
185  
186 13 return @{$self->{_reggrp}};
187 }
188  
189 sub reggrp_by_idx {
190 106 ▶ my ( $self, $idx ) = @_;
Subroutine reggrp_by_idx: called CC=1 SCAR=0.0
Pod undocumented
191  
192 106 return $self->{_reggrp}->[$idx];
193 }
194  
195 # /reggrp methods
196  
197 sub exec {
198 26 ▶ my ( $self, $input, $opts ) = @_;
Subroutine exec: called CC=6 SCAR=18.5
Pod undocumented
199  
200 26 ▶ if ( ref( $input ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "SCALAR") 0 26 26
201 0 carp( 'First argument in Regexp::RegGrp->exec must be a scalarref!' );
202 0 return undef;
203 }
204  
205 26 ▶ $opts ||= {};
Condition50%1/2
$opts ||= {}
l!l
026
MC/DC0%0/1
$opts ||= {}
$opts
Truth table50%1/2
A
A= $opts
Aresult
11
00
206  
207 26 ▶ if ( ref( $opts ) ne 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $opts ne "HASH") 0 26 26
208 0 carp( 'Second argument in Regexp::RegGrp->exec must be a hashref!' );
209 0 return undef;
210 }
211  
212 26 my $to_process = \'';
213 26 my $cont = 'void';
214  
215 26 ▶ if ( defined( wantarray ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 13 13 26
216 13 my $tmp_input = ${$input};
217  
218 13 $to_process = \$tmp_input;
219 13 $cont = 'scalar';
220 }
221 else {
222 13 $to_process = $input;
223 }
224  
225 26 ${$to_process} =~ s/${\$self->re_str()}/$self->_process( { match_hash => \%+, opts => $opts } )/eg;
226  
227 # Return a scalar if requested by context
228 26 ▶ return ${$to_process} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 13 13 26
229 }
230  
231 sub _process {
232 106 ▶ my ( $self, $in_ref ) = @_;
Subroutine _process: called CC=7 SCAR=19.5
233  
234 106 my %match_hash = %{$in_ref->{match_hash}};
235 106 my $opts = $in_ref->{opts};
236  
237 106 my $match_key = ( keys( %match_hash ) )[0];
238 106 my ( $midx ) = $match_key =~ /^_(\d+)$/;
239 106 my $match = $match_hash{$match_key};
240  
241 106 my $reggrp = $self->reggrp_by_idx( $midx );
242  
243 106 my @submatches = $match =~ $reggrp->regexp();
244 106 map { $_ .= ''; } @submatches;
245  
246 106 my $ret = $match;
247  
248 106 my $replacement = $reggrp->replacement();
249  
250 106 ▶ if (
Branch: 2 branches
BranchTrueFalseTotal
if (defined $replacement and not ref $replacement) { } 50 56 106
elsif (ref $replacement eq 'CODE') { } 48 8 56
Condition100%3/3
defined $replacement and not ref $replacement
!ll&&!rl&&r
84850
MC/DC100%2/2
defined $replacement and not ref $replacement
defined $replacement not ref $replacement
Truth table100%3/3
A and not(B)
A= defined $replacementB= not ref $replacement
ABresult
0X0
100
111
251 defined( $replacement ) and
252 not ref( $replacement )
253 ) {
254 50 $ret = $replacement;
255 }
256 elsif ( ref( $replacement ) eq 'CODE' ) {
257 48 $ret = $replacement->(
258 {
259 match => $match,
260 submatches => \@submatches,
261 opts => $opts,
262 store_index => $self->store_data_count()
263 }
264 );
265 }
266  
267 106 my $store = $reggrp->store();
268  
269 106 ▶ if ( $store ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($store) 16 90 106
270 16 my $tmp_match = $match;
271 16 ▶ if ( not ref( $store ) ) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $store) { } 0 16 16
elsif (ref $store eq 'CODE') { } 16 0 16
272 0 $tmp_match = $store;
273 }
274 elsif ( ref( $store ) eq 'CODE' ) {
275 16 $tmp_match = $store->(
276 {
277 match => $match,
278 submatches => \@submatches,
279 opts => $opts
280 }
281 );
282 }
283  
284 16 $self->store_data_add( $tmp_match );
285 }
286  
287 106 return $ret;
288 };
289  
290 sub restore_stored {
291 4 ▶ my ( $self, $input ) = @_;
Subroutine restore_stored: called CC=5 SCAR=16.1
Pod undocumented
292  
293 4 ▶ if ( ref( $input ) ne 'SCALAR' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $input ne "SCALAR") 0 4 4
294 0 carp( 'First argument in Regexp::RegGrp->restore must be a scalarref!' );
295 0 return undef;
296 }
297  
298 4 my $to_process = \'';
299 4 my $cont = 'void';
300  
301 4 ▶ if ( defined( wantarray ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined wantarray) { } 2 2 4
302 2 my $tmp_input = ${$input};
303  
304 2 $to_process = \$tmp_input;
305 2 $cont = 'scalar';
306 }
307 else {
308 2 $to_process = $input;
309 }
310  
311 # Here is a while loop, because there could be recursive replacements
312 4 ▶ while ( ${$to_process} =~ /${\$self->restore_pattern()}/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (${$to_process;} =~ /${\$self->restore_pattern;}/) 4 4 8
313 4 ${$to_process} =~ s/${\$self->restore_pattern()}/$self->store_data_by_idx( $1 )/egsm;
314 }
315  
316 4 $self->flush_stored();
317  
318 # Return a scalar if requested by context
319 4 ▶ return ${$to_process} if ( $cont eq 'scalar' );
Branch: 1 branch
BranchTrueFalseTotal
if $cont eq "scalar" 2 2 4
320 }
321  
322 3 1;
323  
324 __END__
« RegGrp/Data.pm ↑ Summary