blib/lib/App/RecordStream/Operation/xform.pm

filter: Statementstmt 91.8% 136 / 148 Branchbran 72.7% 32 / 44 Conditioncond 70.0% 7 / 10 MC/DCmcdc 66.6% 4 / 6 Subroutinesub 94.7% 18 / 19 Podpod 0.0% 0 / 12 totaltotal 82.4% 197 / 239 CC 28 SCAR 16.5
CC
28
Cov
87%
accept_record
10.9
handle_spools
6.2
has_spooled_data
6.0
CRAP 5.2
« blib/lib/App/RecordStream/Operation/grep.pm ↑ Summary blib/lib/App/RecordStream/DBHandle.pm »
1 package App::RecordStream::Operation::xform;
2  
3 10 our $VERSION = "4.0.23";
4  
5 10 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 10 ▶use base qw(App::RecordStream::Operation);
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 10 ▶use App::RecordStream::Executor::Getopt;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 10 ▶use App::RecordStream::Executor;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 sub init {
13 30 ▶ my $this = shift;
Subroutine init: called CC=1 SCAR=0.0
Pod undocumented
14 30 my $args = shift;
15  
16 30 my $executor_options = App::RecordStream::Executor::Getopt->new();
17 30 my $before = 0;
18 30 my $after = 0;
19 30 my $post_snippet;
20 my $pre_snippet;
21  
22 my $spec = {
23 'B|before=n' => \$before,
24 'A|after=n' => \$after,
25 1 ▶ 'C|context=n' => sub { $before = $_[1]; $after = $_[1]; },
Subroutine __ANON__: called CC=1 SCAR=0.0
26 30 'post-snippet=s' => \$post_snippet,
27 'pre-snippet=s' => \$pre_snippet,
28 $executor_options->arguments(),
29 };
30  
31 30 $this->parse_options($args, $spec, ['bundling']);
32  
33 29 my $expression = $executor_options->get_string($args);
34 29 my $executor = $this->create_executor($expression, $post_snippet, $pre_snippet);
35  
36 29 $this->{'BEFORE'} = $before;
37 29 $this->{'AFTER'} = $after;
38 29 $this->{'EXECUTOR'} = $executor;
39  
40 29 $this->{'XFORM_REF'} = $executor->get_code_ref('xform');
41  
42 29 $this->{'BEFORE_ARRAY'} = [];
43 29 $this->{'AFTER_ARRAY'} = [];
44 29 $this->{'SPOOLED_INPUT'} = [];
45 29 $this->{'SPOOLED_OUTPUT'} = [];
46  
47 29 $executor->execute_method('pre_xform');
48 29 $this->handle_spools();
49 }
50  
51 sub create_executor {
52 29 ▶ my $this = shift;
Subroutine create_executor: called CC=4 SCAR=13.9
Pod undocumented
53 29 my $snippet = shift;
54 29 ▶ my $post_snippet = shift || '';
Condition100%2/2
shift() || ''
l!l
326
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
55 29 ▶ my $pre_snippet = shift || '';
Condition100%2/2
shift() || ''
l!l
326
MC/DC100%1/1
shift() || ''
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
56  
57 29 my $args = {
58 xform => {
59 code => <<" CODE",
60 \$line++;
61 $snippet
62 ; # Safe from a trailing comment in \$snippet
63 \$r
64 CODE
65 arg_names => [qw(r filename B A)],
66 },
67 post_xform => {
68 code => $post_snippet,
69 },
70 pre_xform => {
71 code => $pre_snippet,
72 },
73 };
74  
75 29 my $executor;
76 29 eval {
77 29 $executor = App::RecordStream::Executor->new($args);
78 };
79  
80 29 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 29 29
81 0 die "FATAL: Problem compiling a snippet: $@";
82 }
83  
84 # Initialize the annonymous sub refs to contain $this
85 $executor->set_executor_method('push_input', sub {
86 3 ▶ $this->push_input(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
87 29 });
88  
89 $executor->set_executor_method('push_output', sub {
90 3 ▶ $this->push_output(@_);
Subroutine __ANON__: called CC=1 SCAR=0.0
91 29 });
92  
93 29 return $executor;
94 }
95  
96 sub push_input {
97 3 ▶ my $this = shift;
Subroutine push_input: called CC=1 SCAR=0.0
Pod undocumented
98 3 push @{$this->{'SPOOLED_INPUT'}}, @_;
99 }
100  
101 sub push_output {
102 3 ▶ my $this = shift;
Subroutine push_output: called CC=1 SCAR=0.0
Pod undocumented
103 3 $this->{'SUPPRESS_R'} = 1;
104 3 push @{$this->{'SPOOLED_OUTPUT'}}, @_;
105 }
106  
107 sub accept_record {
108 89 ▶ my $this = shift;
Subroutine accept_record: called CC=10 SCAR=23.8
Pod undocumented
109 89 my $record = shift;
110  
111 89 my $before = $this->{'BEFORE'};
112 89 my $after = $this->{'AFTER'};
113  
114 89 ▶ if ( $before == 0 && $after == 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before == 0 and $after == 0) 80 9 89
Condition100%3/3
$before == 0 and $after == 0
!ll&&!rl&&r
7280
MC/DC100%2/2
$before == 0 and $after == 0
$before == 0 $after == 0
Truth table100%3/3
A and B
A= $before == 0B= $after == 0
ABresult
0X0
100
111
115 80 return $this->run_record_with_context($record);
116 }
117  
118 9 my $before_array = $this->{'BEFORE_ARRAY'};
119 9 my $after_array = $this->{'AFTER_ARRAY'};
120 9 my $current_record = $this->{'CURRENT_RECORD'};
121  
122 9 push @$after_array, $record;
123  
124 9 ▶ if ( scalar @$after_array > $after ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$after_array > $after) 7 2 9
125 7 my $new_record = shift @$after_array;
126  
127 7 ▶ unshift @$before_array, $current_record if ( $current_record );
Branch: 1 branch
BranchTrueFalseTotal
if $current_record 4 3 7
128 7 $current_record = $new_record;
129 }
130  
131 9 ▶ if ( scalar @$after_array > $after ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$after_array > $after) 0 9 9
132 0 my $new_record = shift @$after_array;
133  
134 0 ▶ pop @$before_array if ( scalar @$before_array > $before );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$before_array > $before 0 0 0
135 0 ▶ unshift @$before_array, $current_record if ( $current_record );
Branch: 1 branch
BranchTrueFalseTotal
if $current_record 0 0 0
136 0 $current_record = $new_record;
137 }
138  
139 9 $this->{'CURRENT_RECORD'} = $current_record;
140 9 ▶ pop @$before_array if ( scalar @$before_array > $before );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$before_array > $before 2 7 9
141  
142 9 ▶ if ( !$current_record ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($current_record) 2 7 9
143 2 return 1;
144 }
145 7 $this->{'CURRENT_RECORD'} = $current_record;
146  
147 7 return $this->run_record_with_context($current_record, $before_array, $after_array);
148 }
149  
150 sub stream_done {
151 27 ▶ my $this = shift;
Subroutine stream_done: called CC=5 SCAR=16.1
Pod undocumented
152  
153 27 my $after_array = $this->{'AFTER_ARRAY'};
154  
155 27 ▶ if ( scalar @$after_array > 0 ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$after_array > 0) 2 25 27
156 2 my $before = $this->{'BEFORE'};
157 2 my $before_array = $this->{'BEFORE_ARRAY'};
158 2 my $current_record = $this->{'CURRENT_RECORD'};
159  
160 2 ▶ while ( scalar @$after_array ) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @$after_array) 2 2 4
161 2 my $new_record = shift @$after_array;
162 2 ▶ unshift @$before_array, $current_record if ( $current_record );
Branch: 1 branch
BranchTrueFalseTotal
if $current_record 2 0 2
163 2 $current_record = $new_record;
164  
165 2 ▶ pop @$before_array if ( scalar @$before_array > $before );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @$before_array > $before 2 0 2
166  
167 2 $this->run_record_with_context($current_record, $before_array, $after_array);
168 }
169 }
170  
171 27 $this->{'EXECUTOR'}->execute_method('post_xform');
172 27 $this->handle_spools();
173 }
174  
175 sub run_record_with_context {
176 89 ▶ my $this = shift;
Subroutine run_record_with_context: called CC=5 SCAR=16.2
Pod undocumented
177 89 my $record = shift;
178 89 my $before = shift;
179 89 my $after = shift;
180  
181 89 my $value = $this->run_xform_with_record($record, $before, $after);
182  
183 89 ▶ if ( ! $this->{'SUPPRESS_R'} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->{"SUPPRESS_R"}) 88 1 89
184 88 ▶ if ( ref($value) eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 4 84 88
185 4 foreach my $new_record (@$value) {
186 7 ▶ if ( ref($new_record) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_record eq 'HASH') { } 7 0 7
187 7 $this->push_record(App::RecordStream::Record->new($new_record));
188 }
189 else {
190 0 $this->push_record($new_record);
191 }
192 }
193 }
194 else {
195 84 $this->push_record($value);
196 }
197 }
198  
199 89 return $this->handle_spools();
200 }
201  
202 sub has_spooled_data {
203 0 ▶ my $this = shift;
Subroutine has_spooled_data: not called CC=2 SCAR=17.9
Pod undocumented
204 0 ▶ return (scalar @{$this->{'SPOOLED_INPUT'}} > 0) || (scalar @{$this->{'SPOOLED_OUTPUT'}} > 0);
Condition0%0/3
scalar @{$this->{'SPOOLED_INPUT'};} > 0 || scalar @{$this->{'SPOOLED_OUTPUT'};} > 0
l!l&&r!l&&!r
000
MC/DC0%0/2
scalar @{$this->{'SPOOLED_INPUT'};} > 0 || scalar @{$this->{'SPOOLED_OUTPUT'};} > 0
scalar @{$this->{'SPOOLED_INPUT'};} > 0 scalar @{$this->{'SPOOLED_OUTPUT'};} > 0
Truth table0%0/3
A || B
A= scalar @{$this->{'SPOOLED_INPUT'};} > 0B= scalar @{$this->{'SPOOLED_OUTPUT'};} > 0
ABresult
1X1
011
000
205 }
206  
207 sub handle_spools {
208 145 ▶ my $this = shift;
Subroutine handle_spools: called CC=6 SCAR=18.2
Pod undocumented
209  
210 145 $this->{'SUPPRESS_R'} = 0;
211  
212 145 ▶ while ( @{$this->{'SPOOLED_OUTPUT'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->{"SPOOLED_OUTPUT"};}) 2 145 147
213 2 my $new_record = shift @{$this->{'SPOOLED_OUTPUT'}};
214 2 ▶ if ( ref($new_record) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_record eq "HASH") 2 0 2
215 2 $new_record = App::RecordStream::Record->new($new_record);
216 }
217  
218 2 $this->push_record($new_record);
219 }
220  
221 145 ▶ while ( @{$this->{'SPOOLED_INPUT'}} ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$this->{"SPOOLED_INPUT"};}) 3 145 148
222 3 my $new_record = shift @{$this->{'SPOOLED_INPUT'}};
223 3 ▶ if ( ref($new_record) eq 'HASH' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $new_record eq "HASH") 3 0 3
224 3 $new_record = App::RecordStream::Record->new($new_record);
225 }
226  
227 3 ▶ if (! $this->accept_record($new_record) ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($this->accept_record($new_record)) 0 3 3
228 #we've requested a stop, clear the input and return 0
229 0 $this->{'SPOOLED_INPUT'} = [];
230 0 return 0;
231 }
232 }
233  
234 145 return 1;
235 }
236  
237 sub run_xform_with_record {
238 89 ▶ my $this = shift;
Subroutine run_xform_with_record: called CC=2 SCAR=6.9
Pod undocumented
239 89 my $record = shift;
240 89 my $before = shift;
241 89 my $after = shift;
242  
243 89 ▶ if ( $before ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($before) 9 80 89
244 9 $before = [@$before];
245 9 $after = [@$after];
246 }
247  
248 # Must copy before and after due to autovivification in the case of:
249 # {{after}} = $A->[0]->{'foo'}
250 # (which is unintintional vivification into the array in stream_done)
251 89 return $this->{'XFORM_REF'}->(
252 $record,
253 App::RecordStream::Operation::get_current_filename(),
254 $before,
255 $after,
256 );
257 }
258  
259 sub add_help_types {
260 30 ▶ my $this = shift;
Subroutine add_help_types: called CC=1 SCAR=0.0
Pod undocumented
261 30 $this->use_help_type('snippet');
262 30 $this->use_help_type('keyspecs');
263 }
264  
265 sub usage {
266 1 ▶ my $this = shift;
Subroutine usage: called CC=1 SCAR=0.0
Pod undocumented
267  
268 1 my $options = [
269 App::RecordStream::Executor::options_help(),
270 ['A NUM', 'Make NUM records after this one available in $A (closest record to current in first position)'],
271 ['B NUM', 'Make NUM records before this one available in $B (closest record to current in first position)'],
272 ['C NUM', 'Make NUM records after this one available in $A and $B, as per -A NUM and -B NUM'],
273 ['post-snippet SNIP', 'A snippet to run once the stream has completed'],
274 ['pre-snippet SNIP', 'A snippet to run before the stream starts'],
275 ];
276  
277 1 my $args_string = $this->options_string($options);
278  
279 1 return <<USAGE;
280 Usage: recs-xform <args> <expr> [<files>]
281 __FORMAT_TEXT__
282 <expr> is evaluated as perl on each record of input (or records from
283 <files>) with \$r set to a App::RecordStream::Record object and \$line set to the current
284 line number (starting at 1). All records are printed back out (changed as
285 they may be).
286  
287 If \$r is set to an ARRAY ref in the expr, then the values of the array will
288 be treated as records and outputed one to a line. The values of the array
289 may either be a hash ref or a App::RecordStream::Record object. The
290 original record will not be outputted in this case.
291  
292 There are two helper methods: push_input and push_output. Invoking
293 push_input on a Record object or hash will cause the next record to be
294 processed to be the indicated record. You may pass multiple records with
295 one call. Similarly push_output causes the next record to be output to be
296 the passed record(s). If push_output is called, the original record will
297 not be output in this case. (call push_output(\$r) if you want that record
298 also outputted). You may call these methods from a --pre-snippet or a
299 --post-snippet. You may also call push_output() without any argument to
300 suppress the outputting of the current record
301 __FORMAT_TEXT__
302  
303 $args_string
304  
305 Examples:
306 Add line number to records
307 recs-xform '\$r->{line} = \$line'
308 Rename field old to new, remove field a
309 recs-xform '\$r->rename("old", "new"); \$r->remove("a");'
310 Remove fields which are not "a", "b", or "c"
311 recs-xform '\$r->prune_to("a", "b", "c")'
312 Double records
313 recs-xform '\$r = [{\%\$r}, {\%\$r}]'
314 Double records with function interface
315 recs-xform 'push_output(\$r, \$r);'
316 Move a value from the previous record to the next record
317 recs-xform -B 1 '{{before_val}} = \$B->[0]'
318 USAGE
319 }
320  
321 10 1;
« blib/lib/App/RecordStream/Operation/grep.pm ↑ Summary blib/lib/App/RecordStream/DBHandle.pm »