Filter/Transform/Internal.pm

filter: Statementstmt 86.5% 71 / 82 Branchbran 57.1% 8 / 14 Conditioncond 33.3% 2 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 91.6% 11 / 12 Podpod 100% 5 / 5 totaltotal 78.8% 97 / 123 CC 11 SCAR 19.3
CC
11
Cov
79%
substitute
13.7
mod_regexp
2.0
set_override_modifiers
2.0
CRAP 6.9
« Filter/Filters/Standard.pm ↑ Summary Filter/Transform.pm »
1 package List::Filter::Transform::Internal;
2 3 ▶use base qw( Class::Base );
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 =head1 NAME
5  
6 List::Filter::Transform::Internal - common operations used by transform methods
7  
8 =head1 SYNOPSIS
9  
10 # using as a utility object
11 use List::Filter::Transform::Internal;
12 my $lftu = List::Filter::Transform::Internal->new();
13 $term = [ qr{ slimey \s+ boss }x, 'g', 'professional management' ];
14 $changed_item = $lftu->substitute( $item, $term );
15  
16  
17 # one way of setting override_modifiers attribute (case insensitve, "i")
18 my $lftu = List::Filter::Transform::Internal->new( {override_modifiers => 'i' } );
19  
20 # another way of setting override_modifiers (extended regexps, "x"):
21 $lftu->set_override_modifiers( 'x' );
22  
23  
24 # this can be inherited from (deprecated):
25 use base ("List::Filter::Transform::Internal");
26 my $changed = $self->substitute( $item, $term );
27  
28  
29 =head1 DESCRIPTION
30  
31 List::Filter::Transform::Internal is at present a class of utility
32 object, used by the List::Filter::Transforms::* modules,
33 which contain "methods" that are exported to the Dispatcher
34 namespace ultimately.
35  
36 At present, there is only one object attribute that's important:
37 "override_modifiers"
38 which is used by the "substitute" method.
39  
40 =head2 METHODS
41  
42 =over
43  
44 =cut
45  
46 3 ▶use 5.8.0;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
48 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
49 3 my $DEBUG = 0;
50 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
51 3 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
52 3 ▶use Hash::Util qw(lock_keys unlock_keys);
Subroutine BEGIN: called CC=1 SCAR=0.0
53  
54 3 our $VERSION = '0.01';
55  
56 =item new
57  
58 Instantiates a new List::Filter::Transforms::* object.
59  
60 Takes an optional hashref as an argument, with named fields
61 identical to the names of the object attributes.
62  
63 With no arguments, the newly created filter will be empty.
64  
65 =cut
66  
67 # Note:
68 # "new" is inherited from Class::Base.
69 # It calls the following "init" routine automatically.
70  
71 =item init
72  
73 Initialize object attributes and then lock them down to prevent
74 accidental creation of new ones.
75  
76 Note: there is no leading underscore on name "init", though it's
77 arguably an "internal" routine (i.e. not likely to be of use to
78 client code).
79  
80 =cut
81  
82 2 ▶sub init {
Subroutine init: called CC=1 SCAR=0.0
Pod documented
83 2 my $self = shift;
84 2 my $args = shift;
85 2 unlock_keys( %{ $self } );
86  
87 # define new attributes
88 my $attributes = {
89 override_modifiers => $args->{ override_modifiers },
90 2 };
91  
92 # add attributes to object
93 2 my @fields = (keys %{ $attributes });
94 2 @{ $self }{ @fields } = @{ $attributes }{ @fields }; # hash slice
95  
96 2 lock_keys( %{ $self } );
97 2 return $self;
98 }
99  
100  
101 =back
102  
103 =head2 methods internally used by transforms
104  
105 =over
106  
107 =item substitute
108  
109 Performs a perl s/// on the given string, building up the the
110 substitution from three parts supplied in a an array ref:
111  
112 (1) a perl regular expression, qr{}; (2) any external match
113 modifiers (e.g. "g", "e"); (3) the replacement string (or
114 expression, if the 'e' modifier is in use).
115  
116 Some attempt has been made to avoid re-compiling the regular
117 expressions if there's no need to. To take advantage of this, if
118 at all possible, the "ismx" modifiers should be applied when
119 creating the regexp. The "g" and "e" modififiers are the only ones
120 that need to be applied externally. The "o" modifier is silently ignored.
121  
122 Inputs:
123 (1) the string to be modified
124 (2) a "transform term" (aref, three parts),
125  
126 Returns: the modified string
127  
128 Example:
129 $term = [ qr{ slimey \s+ boss }x, 'g', 'professional management' ];
130 $self->set_override_modifiers( 'x' );
131 $fixed_item = $self->substitute( $item, $term );
132  
133 =cut
134  
135 2 ▶sub substitute {
Subroutine substitute: called CC=10 SCAR=26.2
Pod documented
136 2 my $self = shift;
137 2 my $item = shift;
138 2 my $term = shift;
139  
140 2 my $override_modifiers = $self->override_modifiers;
141  
142 2 my $regexp = $term->[0];
143 2 my $replace = $term->[2];
144 2 ▶ my $mods = $term->[1] || $override_modifiers;
Condition33%1/3
$term->[1] || $override_modifiers
l!l&&r!l&&!r
200
MC/DC0%0/2
$term->[1] || $override_modifiers
$term->[1] $override_modifiers
Truth table33%1/3
A || B
A= $term->[1]B= $override_modifiers
ABresult
1X1
011
000
145  
146 2 ▶ if ($mods) { # just skip this stuff if there aren't any
Branch: 1 branch
BranchTrueFalseTotal
if ($mods) { } 2 0 2
147  
148 # Note: perl s/// mods are "egimosx", but only "imsx" are regexp attributes
149  
150 # extract modifiers that can be applied internally to the regexp
151 2 my @internal = qw( i m s x );
152 2 my $re_mods = '';
153 2 foreach my $c (@internal) {
154 8 ▶ if ( $mods =~ m/$c/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($mods =~ /$c/) 4 4 8
155 4 $re_mods .= $c;
156 }
157 }
158 # prepend internal mods to regexp in (?imsx) form
159 2 ▶ if ($re_mods) { # don't mess with precompiled regexp if we don't need to
Branch: 1 branch
BranchTrueFalseTotal
if ($re_mods) 2 0 2
160 2 $regexp = $self->mod_regexp( $regexp, $re_mods );
161 }
162  
163 # silently ignore 'o' if present
164 2 $mods =~ s/o//;
165  
166 # two valid posibilites remain: g and e, so we cover all 4 permutations:
167 2 my $g_flag = ($mods =~ m/g/);
168 2 my $e_flag = ($mods =~ m/e/);
169  
170 2 ▶ if ($g_flag && $e_flag) {
Branch: 3 branches
BranchTrueFalseTotal
if ($g_flag and $e_flag) { } 0 2 2
elsif ($g_flag) { } 0 2 2
elsif ($e_flag) { } 0 2 2
Condition33%1/3
$g_flag and $e_flag
!ll&&!rl&&r
200
MC/DC0%0/2
$g_flag and $e_flag
$g_flag $e_flag
Truth table33%1/3
A and B
A= $g_flagB= $e_flag
ABresult
0X0
100
111
171 0 $item =~ s{$regexp}{$replace}ge;
172 } elsif ($g_flag) {
173 0 $item =~ s{$regexp}{$replace}g;
174 } elsif ($e_flag) {
175 0 $item =~ s{$regexp}{$replace}e;
176 } else {
177 2 $item =~ s{$regexp}{$replace};
178 }
179 } else { # no late modifiers, so do the simplest (fastest) thing
180 0 $item =~ s{$regexp}{$replace};
181 }
182 2 return $item;
183 }
184  
185 =item mod_regexp
186  
187 Given a qr{} value and a set of modifiers (any of xism),
188 returns a qr{} value with those modifiers applied.
189  
190 Inputs:
191 (1) qr{}
192 (2) string: some sub-set of "xism"
193  
194 Return: qr{}
195  
196 (This is an inheritable "method", though it makes no use of
197 object or class data.)
198  
199 =cut
200  
201 2 ▶sub mod_regexp {
Subroutine mod_regexp: called CC=2 SCAR=7.0
Pod documented
202 2 my $self = shift;
203 2 my $regexp = shift;
204 2 my $mods = shift;
205  
206 2 $self->debug("mod_regexp in Transforms.pm:\n");
207  
208 # Strip the "(?-xism:" and ")" from a qr{} value. They look like:
209 # qr/(?-xism:bush-league of whirled crime)/;
210  
211 2 $self->debug("regexp:>>>", $regexp, "<<<\n");
212  
213 2 my $intermed;
214 2 ▶ if (
Branch: 1 branch
BranchTrueFalseTotal
if (($intermed = $regexp) =~ s/ ^ \( \? [-xism]*? : //x) 0 2 2
215 ($intermed = $regexp) =~ s{ ^ \( \? [-xism]*? : }{}x
216 ) {
217 0 $intermed =~ s/\)$//; # do the closing paren only if the leading one was stripped
218 }
219  
220 2 $self->debug("mods:>>>", $mods, "<<<\n");
221 2 $self->debug("intermed:>>>", $intermed, "<<<\n");
222  
223 # generate new regexp with modifiers applied internally
224 2 my $new_regexp = qr{(?$mods:$intermed)};
225 2 return $new_regexp;
226 }
227  
228 =back
229  
230 =head2 basic setters and getters
231  
232 =over
233  
234 =item override_modifiers
235  
236 Getter for object attribute override_modifiers
237  
238 =cut
239  
240 2 ▶sub override_modifiers {
Subroutine override_modifiers: called CC=1 SCAR=0.0
Pod documented
241 2 my $self = shift;
242 2 my $override_modifiers = $self->{ override_modifiers };
243 2 return $override_modifiers;
244 }
245  
246 =item set_override_modifiers
247  
248 Setter for object attribute set_override_modifiers
249  
250 =cut
251  
252 0 ▶sub set_override_modifiers {
Subroutine set_override_modifiers: not called CC=1 SCAR=6.9
Pod documented
253 0 my $self = shift;
254 0 my $override_modifiers = shift;
255 0 $self->{ override_modifiers } = $override_modifiers;
256 0 return $override_modifiers;
257 }
258  
259  
260  
261  
262 3 1;
263  
264 =back
265  
266 =head1 SEE ALSO
267  
268 L<List::Filter>
269  
270 =head1 AUTHOR
271  
272 Joseph Brenner, E<lt>doom@kzsu.stanford.eduE<gt>
273  
274 =head1 COPYRIGHT AND LICENSE
275  
276 Copyright (C) 2007 by Joseph Brenner
277  
278 This library is free software; you can redistribute it and/or modify
279 it under the same terms as Perl itself, either Perl version 5.8.2 or,
280 at your option, any later version of Perl 5 you may have available.
281  
282 =head1 BUGS
283  
284 None reported... yet.
285  
286 =cut
« Filter/Filters/Standard.pm ↑ Summary Filter/Transform.pm »