Replace/Safe.pm

filter: Statementstmt 89.5% 77 / 86 Branchbran 64.7% 22 / 34 Conditioncond 51.5% 17 / 33 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 17 / 17 Podpod 0.0% 0 / 4 totaltotal 71.3% 137 / 192 CC 32 SCAR 24.4
CC
32
Cov
76%
__prepare_unreplace
24.6
__prepare_replace
16.4
__replace_method
5.0
CRAP 11.5
↑ Summary Replace.pm »
1 package String::Replace::Safe;
2 1 our $VERSION = '0.02';
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Scalar::Util 'reftype', 'blessed';
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use List::MoreUtils 'natatime';
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our @EXPORT_OK = ('replace', 'unreplace');
11 1 our %EXPORT_TAGS = ('all' => [ @EXPORT_OK ] );
12  
13 # There is a lot of code duplication between String::Replace and
14 # String::Replace::Safe, but I don't see a simple way to reduce it without
15 # adding a additionnal indirection level.
16  
17 13 ▶sub __prepare_replace {
Subroutine __prepare_replace: called CC=10 SCAR=28.0
18 13 my %param;
19 13 ▶ if (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'HASH') { } 3 10 13
elsif (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'ARRAY') { } 0 10 10
Condition66%4/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
1003
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1003
MC/DC33%1/3
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
@_ == 1 ref $_[0] builtin::reftype($_[0]) eq "HASH"
Truth table50%2/4
A and B and C
A= @_ == 1B= ref $_[0]C= builtin::reftype($_[0]) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
20 3 %param = %{$_[0]};
21 ▶ } elsif (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'ARRAY') {
Condition33%2/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "ARRAY"
!ll&&!rl&&r
1000
@_ == 1 and ref $_[0]
!ll&&!rl&&r
1000
MC/DC0%0/3
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "ARRAY"
@_ == 1 ref $_[0] builtin::reftype($_[0]) eq "ARRAY"
Truth table25%1/4
A and B and C
A= @_ == 1B= ref $_[0]C= builtin::reftype($_[0]) eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
22 0 ▶ croak 'The replace list must have an even number of element' if @{$_[0]} & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0];} & 1 0 0 0
23 0 %param = @{$_[0]};
24 } else {
25 10 ▶ croak 'The replace list must have an even number of element' if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 10 10
26 10 %param = @_;
27 }
28
29 13 my @repl;
30 13 for my $k (keys %param) {
31 26 push @repl, "\Q$k\E";
32 }
33 13 my $regexp = '('.(join '|', @repl).')';
34  
35 13 return { regexp => qr/$regexp/, replace => \%param };
36 }
37  
38  
39 4 ▶sub __prepare_unreplace {
Subroutine __prepare_unreplace: called CC=13 SCAR=32.0
40 4 my %param;
41 4 ▶ if (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'HASH') { } 2 2 4
elsif (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'ARRAY') { } 0 2 2
Condition66%4/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
202
@_ == 1 and ref $_[0]
!ll&&!rl&&r
202
MC/DC33%1/3
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
@_ == 1 ref $_[0] builtin::reftype($_[0]) eq "HASH"
Truth table50%2/4
A and B and C
A= @_ == 1B= ref $_[0]C= builtin::reftype($_[0]) eq "HASH"
ABCresult
0XX0
10X0
1100
1111
42 2 %param = %{$_[0]};
43 ▶ } elsif (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'ARRAY') {
Condition33%2/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "ARRAY"
!ll&&!rl&&r
200
@_ == 1 and ref $_[0]
!ll&&!rl&&r
200
MC/DC0%0/3
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "ARRAY"
@_ == 1 ref $_[0] builtin::reftype($_[0]) eq "ARRAY"
Truth table25%1/4
A and B and C
A= @_ == 1B= ref $_[0]C= builtin::reftype($_[0]) eq "ARRAY"
ABCresult
0XX0
10X0
1100
1111
44 0 ▶ croak 'The replace list must have an even number of element' if @{$_[0]} & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0];} & 1 0 0 0
45 0 %param = @{$_[0]};
46 } else {
47 2 ▶ croak 'The replace list must have an even number of element' if @_ & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @_ & 1 0 2 2
48 2 %param = @_;
49 }
50
51 4 my %rparam;
52 4 ▶ while (my ($k, $val) = each %param) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %param) 8 4 12
53 0 ▶ my @lv = (ref $val && reftype $val eq 'ARRAY') ? @{$val} : $val;
Branch: 1 branch
BranchTrueFalseTotal
ref $val && builtin::reftype($val) eq 'ARRAY' ? : 0 8 8
Condition33%1/3
ref $val and builtin::reftype($val) eq "ARRAY"
!ll&&!rl&&r
800
MC/DC0%0/2
ref $val and builtin::reftype($val) eq "ARRAY"
ref $val builtin::reftype($val) eq "ARRAY"
Truth table33%1/3
A and B
A= ref $valB= builtin::reftype($val) eq "ARRAY"
ABresult
0X0
100
111
54 8 for my $v (@lv) {
55 8 $rparam{$v} = $k;
56 }
57 }
58  
59 4 return __prepare_replace(%rparam);
60 }
61  
62 # This function is the same for replace and unreplace.
63 23 ▶sub __execute_replace {
Subroutine __execute_replace: called CC=1 SCAR=0.0
64 23 my ($str, $repl) = @_;
65
66 22 $str =~ s/$repl->{regexp}/$repl->{replace}{$1}/ge;
67 #return $str =~ s/$repl->{regexp}/$repl->{replace}{$1}/gre; require v5.14
68  
69 22 return $str;
70 }
71  
72 2 ▶sub __execute_replace_in {
Subroutine __execute_replace_in: called CC=1 SCAR=0.0
73 2 my (undef, $repl) = @_;
74  
75 2 $_[0] =~ s/$repl->{regexp}/$repl->{replace}{$1}/ge;
76  
77 2 return;
78 }
79  
80 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
81 2 my ($class, @param) = @_;
82
83 2 my $self = __prepare_replace(@param);
84  
85 2 return bless $self, $class;
86 }
87  
88 2 ▶sub new_unreplace {
Subroutine new_unreplace: called CC=1 SCAR=0.0
Pod undocumented
89 2 my ($class, @param) = @_;
90
91 2 my $self = __prepare_unreplace(@param);
92  
93 2 return bless $self, $class;
94 }
95  
96  
97 12 ▶sub __replace_method {
Subroutine __replace_method: called CC=5 SCAR=16.1
98 12 my $repl = shift;
99
100 12 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 5 7 12
elsif (defined wantarray) { } 6 1 7
101 5 return map { __execute_replace($_, $repl) } @_;
102 } elsif (defined wantarray) {
103 6 ▶ return @_ ? __execute_replace($_[0], $repl) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
104 } else {
105 1 __execute_replace_in($_, $repl) for @_;
106 1 return;
107 }
108 }
109  
110 7 ▶sub __replace_fun {
Subroutine __replace_fun: called CC=1 SCAR=0.0
111 7 my ($str, @list) = @_;
112  
113 7 return __execute_replace($str, __prepare_replace(@list))
114 }
115  
116 2 ▶sub __unreplace_fun {
Subroutine __unreplace_fun: called CC=1 SCAR=0.0
117 2 my ($str, @list) = @_;
118  
119 2 return __execute_replace($str, __prepare_unreplace(@list))
120 }
121  
122 17 ▶sub replace {
Subroutine replace: called CC=4 SCAR=14.1
Pod undocumented
123 17 ▶ croak 'Missing argument to '.__PACKAGE__.'::replace' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 17 17
124  
125 17 ▶ if (blessed($_[0]) && $_[0]->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)) { } 10 7 17
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
7010
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
126 10 return &__replace_method;
127 } else {
128 7 return &__replace_fun;
129 }
130 }
131  
132 4 ▶sub unreplace {
Subroutine unreplace: called CC=4 SCAR=14.1
Pod undocumented
133 4 ▶ croak 'Missing argument to '.__PACKAGE__.'::unreplace' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
134  
135 4 ▶ if (blessed($_[0]) && $_[0]->isa(__PACKAGE__)) {
Branch: 1 branch
BranchTrueFalseTotal
if (builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)) { } 2 2 4
Condition66%2/3
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
!ll&&!rl&&r
202
MC/DC50%1/2
builtin::blessed($_[0]) and $_[0]->isa(__PACKAGE__)
builtin::blessed($_[0]) $_[0]->isa(__PACKAGE__)
Truth table66%2/3
A and B
A= builtin::blessed($_[0])B= $_[0]->isa(__PACKAGE__)
ABresult
0X0
100
111
136 2 return &__replace_method;
137 } else {
138 2 return &__unreplace_fun;
139 }
140 }
141  
142 =cut
143  
144 1;
145  
146  
147 =encoding utf-8
148  
149 =head1 NAME
150  
151 String::Replace::Safe - Performs arbitrary replacement in strings, safely
152  
153 =head1 SYNOPSIS
154  
155 use String::Replace::Safe ':all';
156
157 print replace('hello name', 'name' => 'world');
158 print unreplace('hello world', {'name' => 'world'});
159
160 my $r = String::Replace::Safe->new('name' => 'world');
161 print $r->replace('hello world');
162  
163 =head1 DESCRIPTION
164  
165 C<String::Replace::Safe> is a safe version of the C<L<String::Replace>> library.
166 That is that this version does not depend on the order of evaluation of the
167 argument to its function. This version is also consistently slower than the I<unsafe>
168 version (by a factor of approximately 50%).
169  
170 Apart from that, the interface of the safe version is exactly the same (both
171 functionnal and object oriented) as the interface of the C<L<String::Replace>>
172 library. Hence the absence of documentation here.
173  
174 =head1 BUGS
175  
176 Please report any bugs or feature requests to C<bug-string-replace@rt.cpan.org>, or
177 through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Replace>.
178  
179 =head1 AUTHOR
180  
181 Mathias Kende (mathias@cpan.org)
182  
183 =head1 VERSION
184  
185 Version 0.02 (January 2013)
186  
187 =head1 COPYRIGHT & LICENSE
188  
189 Copyright 2013 © Mathias Kende. All rights reserved.
190  
191 This program is free software; you can redistribute it and/or
192 modify it under the same terms as Perl itself.
193  
194 =cut
195  
196  
197  
198  
↑ Summary Replace.pm »