Replace.pm

filter: Statementstmt 94.4% 85 / 90 Branchbran 75.0% 24 / 32 Conditioncond 51.5% 17 / 33 MC/DCmcdc 22.2% 4 / 18 Subroutinesub 100% 17 / 17 Podpod 100% 4 / 4 totaltotal 77.8% 151 / 194 CC 32 SCAR 20.9
CC
32
Cov
81%
__prepare_unreplace
17.7
__prepare_replace
11.3
__replace_method
5.0
CRAP 8.1
« Replace/Safe.pm ↑ Summary
1 package String::Replace;
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 9 ▶sub __prepare_replace {
Subroutine __prepare_replace: called CC=9 SCAR=24.2
14 9 my @l;
15 9 ▶ if (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'HASH') {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'HASH') { } 3 6 9
elsif (@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq 'ARRAY') { } 0 6 6
Condition66%4/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "HASH"
!ll&&!rl&&r
603
@_ == 1 and ref $_[0]
!ll&&!rl&&r
603
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
16 3 @l = ( %{$_[0]} );
17 ▶ } elsif (@_ == 1 && ref($_[0]) && reftype($_[0]) eq 'ARRAY') {
Condition33%2/6
@_ == 1 and ref $_[0] and builtin::reftype($_[0]) eq "ARRAY"
!ll&&!rl&&r
600
@_ == 1 and ref $_[0]
!ll&&!rl&&r
600
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
18 0 @l = @{$_[0]};
19 } else {
20 6 @l = @_;
21 }
22  
23 9 ▶ croak 'The replace list must have an even number of element' if @l & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @l & 1 0 9 9
24  
25 9 my $it = natatime 2, @l;
26  
27 9 my @repl;
28 9 ▶ while (my ($k, $v) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 18 9 27
29 18 push @repl, [qr{\Q$k\E}, $v];
30 }
31  
32 9 return \@repl;
33 }
34  
35 4 ▶sub __prepare_unreplace {
Subroutine __prepare_unreplace: called CC=12 SCAR=28.8
36 4 my @l;
37 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
38 2 @l = ( %{$_[0]} );
39 ▶ } 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
40 0 @l = @{$_[0]};
41 } else {
42 2 @l = @_;
43 }
44  
45 4 ▶ croak 'The replace list must have an even number of element' if @l & 1;
Branch: 1 branch
BranchTrueFalseTotal
if @l & 1 0 4 4
46  
47 4 my $it = natatime 2, @l;
48  
49 4 my @repl;
50 4 ▶ while (my ($k, $val) = $it->()) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = &$it()) 8 4 12
51 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
52 8 for my $v (@lv) {
53 8 push @repl, [qr{\Q$v\E}, $k];
54 }
55 }
56  
57 4 return \@repl;
58 }
59  
60 # This function is the same for replace and unreplace.
61 23 ▶sub __execute_replace {
Subroutine __execute_replace: called CC=2 SCAR=6.9
62 23 my ($str, $repl) = @_;
63  
64 22 for my $e (@{$repl}) {
65 44 my ($re, $v) = @{$e};
66 44 $str =~ s/$re/$v/g;
67 }
68  
69 22 return $str;
70 }
71  
72 2 ▶sub __execute_replace_in {
Subroutine __execute_replace_in: called CC=2 SCAR=6.9
73 2 my (undef, $repl) = @_;
74  
75 2 for my $e (@{$repl}) {
76 4 my ($re, $v) = @{$e};
77 4 $_[0] =~ s/$re/$v/g;
78 }
79  
80 2 return;
81 }
82  
83  
84 2 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
85 2 my ($class, @param) = @_;
86
87 2 my $self = __prepare_replace(@param);
88  
89 2 return bless $self, $class;
90 }
91  
92 2 ▶sub new_unreplace {
Subroutine new_unreplace: called CC=1 SCAR=0.0
Pod documented
93 2 my ($class, @param) = @_;
94
95 2 my $self = __prepare_unreplace(@param);
96  
97 2 return bless $self, $class;
98 }
99  
100  
101 12 ▶sub __replace_method {
Subroutine __replace_method: called CC=5 SCAR=16.1
102 12 my $repl = shift;
103
104 12 ▶ if (wantarray) {
Branch: 2 branches
BranchTrueFalseTotal
if (wantarray) { } 5 7 12
elsif (defined wantarray) { } 6 1 7
105 5 return map { __execute_replace($_, $repl) } @_;
106 } elsif (defined wantarray) {
107 6 ▶ return @_ ? __execute_replace($_[0], $repl) : undef;
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 6 0 6
108 } else {
109 1 __execute_replace_in($_, $repl) for @_;
110 1 return;
111 }
112 }
113  
114 7 ▶sub __replace_fun {
Subroutine __replace_fun: called CC=1 SCAR=0.0
115 7 my ($str, @list) = @_;
116  
117 7 return __execute_replace($str, __prepare_replace(@list))
118 }
119  
120 2 ▶sub __unreplace_fun {
Subroutine __unreplace_fun: called CC=1 SCAR=0.0
121 2 my ($str, @list) = @_;
122  
123 2 return __execute_replace($str, __prepare_unreplace(@list))
124 }
125  
126 17 ▶sub replace {
Subroutine replace: called CC=4 SCAR=14.1
Pod documented
127 17 ▶ croak 'Missing argument to '.__PACKAGE__.'::replace' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 17 17
128  
129 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
130 10 return &__replace_method;
131 } else {
132 7 return &__replace_fun;
133 }
134 }
135  
136 4 ▶sub unreplace {
Subroutine unreplace: called CC=4 SCAR=14.1
Pod documented
137 4 ▶ croak 'Missing argument to '.__PACKAGE__.'::unreplace' unless @_;
Branch: 1 branch
BranchTrueFalseTotal
unless @_ 0 4 4
138  
139 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
140 2 return &__replace_method;
141 } else {
142 2 return &__unreplace_fun;
143 }
144 }
145  
146 1 1;
147  
148 =encoding utf-8
149  
150 =head1 NAME
151  
152 String::Replace - Performs arbitrary replacement in strings
153  
154 =head1 SYNOPSIS
155  
156 use String::Replace ':all';
157
158 print replace('hello name', 'name' => 'world');
159 print unreplace('hello world', {'name' => 'world'});
160
161 my $r = String::Replace->new('name' => 'world');
162 print $r->replace('hello world');
163  
164 =head1 DESCRIPTION
165  
166 C<String::Replace> is a small module allowing to performs arbitrary replacement
167 in strings. Arbitrary means that there is no specific syntax to do so, you can
168 just replace any arbitrary substring.
169  
170 The real functionnality of C<String::Replace> is its OO interface which allows
171 you to prepare and encapsulate replacement to be performed in string. While other
172 templating systems (all of them ?) allow you to load a template and then to
173 perform successive series of replacement in it, C<String::Replace> allows you to
174 load a serie of replacement and then apply them successively to many template.
175 If this is what you need to do, your code will be simpler to read with C<String::Replace>
176 and maybe slighly faster due to the preprocessing which can be done.
177  
178 Standard templating systems are typically used to generate the same web page many
179 times for different users. C<String::Replace> is rather used to generate a lot
180 of different content for a single user, or to provide a simple parametrisation
181 system for code (as is done with SQL in my C<SQL::Exec> module).
182  
183 =head1 FUNCTIONS
184  
185 This is a list of the public function of this library. Functions not listed here
186 are for internal use only by this module and should not be used in any external
187 code.
188  
189 Each function of this library (that is C<replace> and C<unreplace>) may be
190 exported on request. There is also a C<':all'> tag to get everything at once.
191 Just do :
192  
193 use String::Replace ':all';
194  
195 to have all the functions of the library imported into your current package.
196  
197 =head2 replace
198  
199 my $s = replace(EXPR, LIST);
200 my $s = replace(EXPR, HASH);
201  
202 The C<replace> function take a string and a list of replacement to perform in the
203 string and return a string where all replacement have been done. the replacement
204 can be given either as list or as a hash reference.
205  
206 replace('this is a string', 'this' => 'that', 'string' => 'chair');
207 replace('this is a string', { 'this' => 'that', 'string' => 'chair' });
208  
209 will both return the string C<'that is a chair'>.
210  
211 You should not that the replacement will be executed in the order in which they
212 appear if you give a list but in no particular order if you give a hash reference.
213 So if a replacement creates a substring that may be replaced by an other replacement
214 then you should use a list of replacement to be sure of what will be happening.
215  
216 =head2 unreplace
217  
218 my $s = replace(I<EXPR>, I<LIST>);
219 my $s = replace(I<EXPR>, I<HASH>);
220  
221 Performs the opposite of the C<replace> function.
222  
223 replace('that is a chair', 'this' => 'that', 'string' => 'chair');
224 replace('that is a chair', { 'this' => 'that', 'string' => 'chair' });
225  
226 will both return the string C<'this is a string'>. The same caveat than for the
227 C<replace> function will apply.
228  
229 =head1 Object-Oriented interface
230  
231 If you wish so, you may also use an object oriented interface to C<String::Replace>.
232 The object oriented interface will be (slightly) faster than the functionnal one
233 if you have many strings on which you will perform the same replacement (as some
234 regexp can be pre-compiled).
235  
236 =head2 new
237  
238 my $r = String::Replace->new(I<LIST>);
239 my $r = String::Replace->new(I<HASH>);
240  
241 This constructor may be called with either a list of replacement to performs or
242 a reference to a hash describing these replacements. The argument is treated in
243 the same way as the second argument to the C<replace> function. When created,
244 the C<replace> method may then be called on the object.
245  
246 The code:
247  
248 my $r = String::Replace->new('this' => 'that', 'string' => 'chair');
249 $r->replace('this is a string');
250  
251 will return the same thing than the example above but the C<$r> object might be
252 reused.
253  
254 The same caveat as for the order of the argument to the C<replace> function apply
255 for this constructor.
256  
257 =head2 new_unreplace
258  
259 my $u = String::Replace->new_unreplace(I<LIST>);
260 my $u = String::Replace->new_unreplace(I<HASH>);
261  
262 This constructor may be called with either a list of replacement a reference to
263 a hash describing replacements. The argument is treated in the same way as the
264 second argument to the C<unreplace> function. When created, the C<replace> method
265 may then be called on the object the execute this I<un-replacement>.
266  
267 The code:
268  
269 my $u = String::Replace->new_unreplace('this' => 'that', 'string' => 'chair');
270 $u->replace('that is a chair');
271  
272 will return the same thing than the example above but the C<$u> object might be
273 reused.
274  
275 The same caveat as for the order of the argument to the C<replace> function apply
276 for this constructor.
277  
278 =head2 replace
279  
280 my $s = $r->replace(I<LIST>);
281 my @l = $r->replace(I<LIST>);
282 $r->replace(I<LIST>);
283  
284 This function performs a prepared replacement or I<un-replacement> as described
285 in the documentation of the C<new> and C<new_unreplace> constructors.
286  
287 This function is context sensitive: if it is called in list context, it will
288 apply its replacement in turn to each of its argument and returns a list with
289 each string where the replacement has been done. If it is called in sink (void)
290 context, then the replacement are executed in place. If called in scalar context
291 only the first argument of the C<replace> function is taken and replaced and the
292 result of this replacement is returned.
293  
294 The same apply if the object was prepared with C<new_unreplace> instead of C<new>.
295  
296 =head2 unreplace
297  
298 $r->unreplace(LIST);
299  
300 This method is exactly the same as the C<replace> one and will not distinguish
301 between object created with the C<new> or the C<new_unreplace> functions. It is
302 provided only for convenience.
303  
304 =head1 CAVEATS
305  
306 As stated above, the order in which the arguments are provided to the functions
307 of this library may matter. To avoid problem, you should use a non-ambiguous
308 parametrisation scheme (like prefixing all your variable to be replaced with a
309 given character).
310  
311 If this a problem for you, there is a safe version of this library: C<L<String::Replace::Safe>>.
312 This version will performs all its replacement atomically so the order of the
313 argument does not matter. However the speed of this version will be approximately
314 half that of the C<String::Replace> version (according to my test, this does not
315 depend much on the size of the string, the number of replacement that you want
316 to perform or the number of replacement actually performed).
317  
318 In an unambiguous case, the two version of this library should give back exactly
319 the same results.
320  
321 =head1 BUGS
322  
323 Please report any bugs or feature requests to C<bug-string-replace@rt.cpan.org>, or
324 through the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Replace>.
325  
326 =head1 SEE ALSO
327  
328 There is a safer (and slower) version of this library: C<L<String::Replace::Safe>>.
329  
330 There is also a lot of templating system on CPAN and a lot of them could let you
331 achieve the same thing than C<String::Replace> (with the caveat that they are
332 all centered around the template and not around the replace operation). Some
333 simple and efficient modules are the followings: C<L<String::Interpolate::Shell>>
334 and C<L<String::Interpolate::RE>>.
335  
336 =head1 AUTHOR
337  
338 Mathias Kende (mathias@cpan.org)
339  
340 =head1 VERSION
341  
342 Version 0.02 (January 2013)
343  
344 =head1 COPYRIGHT & LICENSE
345  
346 Copyright 2013 © Mathias Kende. All rights reserved.
347  
348 This program is free software; you can redistribute it and/or
349 modify it under the same terms as Perl itself.
350  
351 =cut
352  
353  
354  
355  
« Replace/Safe.pm ↑ Summary