blib/lib/List/RewriteElements.pm

filter: Statementstmt 100% 127 / 127 Branchbran 94.2% 49 / 52 Conditioncond 100% 36 / 36 MC/DCmcdc 100% 20 / 20 Subroutinesub 100% 21 / 21 Podpod 100% 10 / 10 totaltotal 98.8% 263 / 266 CC 40 SCAR 24.7
CC
40
Cov
99%
new
24.0
generate_output
8.0
_body_rule_handler
5.0
CRAP 11.8
↑ Summary
1 package List::RewriteElements;
2 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 7 our $VERSION = '0.11';
5 7 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use Cwd;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use File::Copy;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use File::Spec;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 7 ▶use Tie::File;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 32 ▶sub new {
Subroutine new: called CC=24 SCAR=31.8
Pod documented
13 32 my ($class, $argsref) = @_;
14 croak "Hash ref passed to constructor must contain 'body_rule' element"
15 32 ▶ unless defined $argsref->{body_rule};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argsref->{"body_rule"} 1 31 32
16 croak "'body_rule' element value must be a code ref"
17 31 ▶ unless ref($argsref->{body_rule}) eq 'CODE';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $argsref->{"body_rule"} eq "CODE" 1 30 31
18 croak "Hash ref passed to constructor must have either a 'file' element or a 'list' element"
19 30 ▶ unless (defined $argsref->{file} or defined $argsref->{list});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $argsref->{"file"} or defined $argsref->{"list"} 1 29 30
Condition100%3/3
defined $argsref->{"file"} or defined $argsref->{"list"}
l!l&&r!l&&!r
6231
MC/DC100%2/2
defined $argsref->{"file"} or defined $argsref->{"list"}
defined $argsref->{"file"} defined $argsref->{"list"}
Truth table100%3/3
A or B
A= defined $argsref->{"file"}B= defined $argsref->{"list"}
ABresult
1X1
011
000
20 croak "'file' element passed to constructor not located"
21 29 ▶ if (defined $argsref->{file} and not -f $argsref->{file});
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"file"} and not -f $argsref->{"file"} 1 28 29
Condition100%3/3
defined $argsref->{"file"} and not -f $argsref->{"file"}
!ll&&!rl&&r
2351
MC/DC100%2/2
defined $argsref->{"file"} and not -f $argsref->{"file"}
defined $argsref->{"file"} not -f $argsref->{"file"}
Truth table100%3/3
A and not(B)
A= defined $argsref->{"file"}B= not -f $argsref->{"file"}
ABresult
0X0
100
111
22 croak "'list' element passed to constructor must be array ref"
23 if ( defined $argsref->{list} and
24 (
25 (not ref($argsref->{list})) or
26 28 ▶ (ref($argsref->{list}) ne 'ARRAY')
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"list"} and !ref($argsref->{"list"}) || ref $argsref->{"list"} ne "ARRAY" 2 26 28
Condition100%6/6
defined $argsref->{"list"} and !ref($argsref->{"list"}) || ref $argsref->{"list"} ne "ARRAY"
!ll&&!rl&&r
5212
not ref $argsref->{"list"} or ref $argsref->{"list"} ne "ARRAY"
l!l&&r!l&&!r
1121
MC/DC100%3/3
defined $argsref->{"list"} and !ref($argsref->{"list"}) || ref $argsref->{"list"} ne "ARRAY"
defined $argsref->{"list"} not ref $argsref->{"list"} ref $argsref->{"list"} ne "ARRAY"
Truth table100%4/4
A and not(B) or C
A= defined $argsref->{"list"}B= not ref $argsref->{"list"}C= ref $argsref->{"list"} ne "ARRAY"
ABCresult
0XX0
1000
11X1
1011
27 )
28 );
29 croak "'body_suppress' element passed to constructor must be code ref"
30 if ( defined $argsref->{body_suppress} and
31 (
32 (not ref($argsref->{body_suppress})) or
33 26 ▶ (ref($argsref->{body_suppress}) ne 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"body_suppress"} and !ref($argsref->{"body_suppress"}) || ref $argsref->{"body_suppress"} ne "CODE" 2 24 26
Condition100%6/6
defined $argsref->{"body_suppress"} and !ref($argsref->{"body_suppress"}) || ref $argsref->{"body_suppress"} ne "CODE"
!ll&&!rl&&r
2042
not ref $argsref->{"body_suppress"} or ref $argsref->{"body_suppress"} ne "CODE"
l!l&&r!l&&!r
114
MC/DC100%3/3
defined $argsref->{"body_suppress"} and !ref($argsref->{"body_suppress"}) || ref $argsref->{"body_suppress"} ne "CODE"
defined $argsref->{"body_suppress"} not ref $argsref->{"body_suppress"} ref $argsref->{"body_suppress"} ne "CODE"
Truth table100%4/4
A and not(B) or C
A= defined $argsref->{"body_suppress"}B= not ref $argsref->{"body_suppress"}C= ref $argsref->{"body_suppress"} ne "CODE"
ABCresult
0XX0
1000
11X1
1011
34 )
35 );
36 croak "'header_rule' element passed to constructor must be code ref"
37 if ( defined $argsref->{header_rule} and
38 (
39 (not ref($argsref->{header_rule})) or
40 24 ▶ (ref($argsref->{header_rule}) ne 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"header_rule"} and !ref($argsref->{"header_rule"}) || ref $argsref->{"header_rule"} ne "CODE" 2 22 24
Condition100%6/6
defined $argsref->{"header_rule"} and !ref($argsref->{"header_rule"}) || ref $argsref->{"header_rule"} ne "CODE"
!ll&&!rl&&r
12102
not ref $argsref->{"header_rule"} or ref $argsref->{"header_rule"} ne "CODE"
l!l&&r!l&&!r
1110
MC/DC100%3/3
defined $argsref->{"header_rule"} and !ref($argsref->{"header_rule"}) || ref $argsref->{"header_rule"} ne "CODE"
defined $argsref->{"header_rule"} not ref $argsref->{"header_rule"} ref $argsref->{"header_rule"} ne "CODE"
Truth table100%4/4
A and not(B) or C
A= defined $argsref->{"header_rule"}B= not ref $argsref->{"header_rule"}C= ref $argsref->{"header_rule"} ne "CODE"
ABCresult
0XX0
1000
11X1
1011
41 )
42 );
43 croak "If 'header_suppress' criterion is supplied, a 'header_rule' element must be supplied as well"
44 if ( defined $argsref->{header_suppress} and
45 ! defined $argsref->{header_rule}
46 22 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"header_suppress"} and not defined $argsref->{"header_rule"} 1 21 22
Condition100%3/3
defined $argsref->{"header_suppress"} and not defined $argsref->{"header_rule"}
!ll&&!rl&&r
1471
MC/DC100%2/2
defined $argsref->{"header_suppress"} and not defined $argsref->{"header_rule"}
defined $argsref->{"header_suppress"} not defined $argsref->{"header_rule"}
Truth table100%3/3
A and not(B)
A= defined $argsref->{"header_suppress"}B= not defined $argsref->{"header_rule"}
ABresult
0X0
100
111
47 croak "'header_suppress' element passed to constructor must be code ref"
48 if ( defined $argsref->{header_suppress} and
49 (
50 (not ref($argsref->{header_suppress})) or
51 21 ▶ (ref($argsref->{header_suppress}) ne 'CODE')
Branch: 1 branch
BranchTrueFalseTotal
if defined $argsref->{"header_suppress"} and !ref($argsref->{"header_suppress"}) || ref $argsref->{"header_suppress"} ne "CODE" 2 19 21
Condition100%6/6
defined $argsref->{"header_suppress"} and !ref($argsref->{"header_suppress"}) || ref $argsref->{"header_suppress"} ne "CODE"
!ll&&!rl&&r
1452
not ref $argsref->{"header_suppress"} or ref $argsref->{"header_suppress"} ne "CODE"
l!l&&r!l&&!r
115
MC/DC100%3/3
defined $argsref->{"header_suppress"} and !ref($argsref->{"header_suppress"}) || ref $argsref->{"header_suppress"} ne "CODE"
defined $argsref->{"header_suppress"} not ref $argsref->{"header_suppress"} ref $argsref->{"header_suppress"} ne "CODE"
Truth table100%4/4
A and not(B) or C
A= defined $argsref->{"header_suppress"}B= not ref $argsref->{"header_suppress"}C= ref $argsref->{"header_suppress"} ne "CODE"
ABCresult
0XX0
1000
11X1
1011
52 )
53 );
54  
55 19 ▶ if ($argsref->{file}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($argsref->{'file'}) { } 5 14 19
56 5 my @elements;
57 5 ▶ tie @elements, 'Tie::File', $argsref->{file}, recsep => $/
Branch: 1 branch
BranchTrueFalseTotal
tie @elements, "Tie::File", $argsref->{"file"}, "recsep", $/ or croak(qq[Unable to tie to $argsref->{"file"}]) 5 0 5
58 or croak "Unable to tie to $argsref->{file}";
59 5 $argsref->{working} = \@elements;
60 } else {
61 14 $argsref->{working} = $argsref->{list};
62 }
63  
64 19 my $self = bless ($argsref, $class);
65  
66 19 $self->{rows_in} = scalar(@{$self->{working}});
67 19 ▶ if (defined $self->{header_rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'header_rule'}) { } 8 11 19
68 8 $self->{records_in} = $self->{rows_in} - 1;
69 } else {
70 11 $self->{records_in} = $self->{rows_in};
71 }
72 # Next attributes are initialized to empty strings because their value
73 # is not fixed until after generate_output() has been called.
74 19 $self->{output_path} = q{};
75 19 $self->{output_basename} = q{};
76 # Next attributes are initialized to zero because their value
77 # is not fixed until after generate_output() has been called.
78 19 $self->{rows_out} = 0;
79 19 $self->{records_out} = 0;
80 19 $self->{records_changed} = 0;
81 19 $self->{records_unchanged} = 0;
82 19 $self->{records_deleted} = 0;
83 19 return $self;
84 }
85  
86 19 ▶sub generate_output {
Subroutine generate_output: called CC=8 SCAR=20.8
Pod documented
87 19 my $self = shift;
88 19 ▶ if ( ! # print to STDOUT
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'output_file'} || defined $self->{'output_suffix'}) { } 11 8 19
Condition100%3/3
defined $self->{'output_file'} || defined $self->{'output_suffix'}
l!l&&r!l&&!r
7111
MC/DC100%2/2
defined $self->{'output_file'} || defined $self->{'output_suffix'}
defined $self->{'output_file'} defined $self->{'output_suffix'}
Truth table100%3/3
A || B
A= defined $self->{'output_file'}B= defined $self->{'output_suffix'}
ABresult
1X1
011
000
89 (
90 defined $self->{output_file} or
91 defined $self->{output_suffix}
92 )
93 ) {
94 11 $self->_handler_control();
95 } else { # print to file
96 8 my $outfile;
97 8 ▶ if (defined $self->{output_file}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'output_file'}) { } 7 1 8
98 7 $outfile = $self->{output_file};
99 } else {
100 $outfile = File::Spec->catfile( ( cwd() ),
101 1 basename($self->{file}) . $self->{output_suffix} );
102 }
103 8 ▶ open my $OUT, '>', $outfile
Branch: 1 branch
BranchTrueFalseTotal
open my $OUT, ">", $outfile or croak("Unable to open $outfile for writing") 8 0 8
104 or croak "Unable to open $outfile for writing";
105 8 my $oldfh = select($OUT);
106 8 $self->_handler_control();
107 8 ▶ close $OUT
Branch: 1 branch
BranchTrueFalseTotal
close $OUT or croak("Unable to close $outfile after writing") 8 0 8
108 or croak "Unable to close $outfile after writing";
109 8 select $oldfh;
110 8 $self->{output_path} = $outfile;
111 8 $self->{output_basename} = basename($self->{output_path});
112 }
113 19 $self->{records_out} = $self->{records_in} - $self->{records_deleted};
114 $self->{records_unchanged} =
115 19 $self->{records_out} - $self->{records_changed};
116 19 ▶ if (! defined $self->{header_rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'header_rule'}) { } 11 8 19
117 11 $self->{rows_out} = $self->{records_out};
118 } else {
119 8 ▶ if ($self->{header_status} != -1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'header_status'} != -1) { } 6 2 8
120 6 $self->{rows_out} = $self->{records_out} + 1;
121 } else {
122 2 $self->{rows_out} = $self->{records_out};
123 }
124 }
125 }
126  
127 19 ▶sub _handler_control {
Subroutine _handler_control: called CC=2 SCAR=6.9
128 19 my $self = shift;
129 19 ▶ if (! defined $self->{header_rule}) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->{'header_rule'}) { } 11 8 19
130 11 $self->_body_rule_handler();
131 } else {
132 8 $self->_header_body_rule_handler();
133 }
134 }
135  
136 19 ▶sub _body_rule_handler {
Subroutine _body_rule_handler: called CC=5 SCAR=16.1
137 19 my $self = shift;
138 19 RECORD: foreach my $el (@{$self->{working}}) {
139 178 chomp $el;
140 178 ▶ if (defined $self->{body_suppress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{"body_suppress"}) 40 138 178
141 40 ▶ unless (defined (&{$self->{body_suppress}}($el))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$self->{"body_suppress"};}($el)) 6 34 40
142 6 $self->{records_deleted}++;
143 6 next RECORD;
144 }
145 }
146 172 my $newel = &{$self->{body_rule}}($el);
147 172 print "$newel\n";
148 172 ▶ $self->{records_changed}++ if $el ne $newel;
Branch: 1 branch
BranchTrueFalseTotal
if $el ne $newel 154 18 172
149 }
150 }
151  
152 8 ▶sub _header_body_rule_handler {
Subroutine _header_body_rule_handler: called CC=5 SCAR=16.1
153 8 my $self = shift;
154 8 $self->{header_status} = 0; # header present, as yet unchanged
155 8 my $header = shift(@{$self->{working}});
156 8 chomp $header;
157 8 ▶ if (defined $self->{header_suppress}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $self->{'header_suppress'}) { } 5 3 8
158 5 ▶ if (defined (&{$self->{header_suppress}}($header))) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined &{$self->{'header_suppress'};}($header)) { } 3 2 5
159 3 my $newheader = &{$self->{header_rule}}($header);
160 3 print "$newheader\n";
161 3 ▶ $self->{header_status} = 1 if $header ne $newheader;
Branch: 1 branch
BranchTrueFalseTotal
if $header ne $newheader 2 1 3
162 # header changed
163 } else {
164 2 $self->{header_status} = -1; # header suppressed
165 }
166 } else {
167 3 my $newheader = &{$self->{header_rule}}($header);
168 3 print "$newheader\n";
169 3 ▶ $self->{header_status} = 1 if $header ne $newheader;
Branch: 1 branch
BranchTrueFalseTotal
if $header ne $newheader 2 1 3
170 # header changed
171 }
172 8 $self->_body_rule_handler();
173 }
174  
175 2 ▶sub get_output_path {
Subroutine get_output_path: called CC=1 SCAR=0.0
Pod documented
176 2 my $self = shift;
177 2 return $self->{output_path};
178 }
179  
180 3 ▶sub get_output_basename {
Subroutine get_output_basename: called CC=1 SCAR=0.0
Pod documented
181 3 my $self = shift;
182 3 return $self->{output_basename};
183 }
184  
185 13 ▶sub get_total_rows {
Subroutine get_total_rows: called CC=1 SCAR=0.0
Pod documented
186 13 my $self = shift;
187 13 return $self->{rows_out};
188 }
189  
190 13 ▶sub get_total_records {
Subroutine get_total_records: called CC=1 SCAR=0.0
Pod documented
191 13 my $self = shift;
192 13 return $self->{records_out};
193 }
194  
195 13 ▶sub get_records_changed {
Subroutine get_records_changed: called CC=1 SCAR=0.0
Pod documented
196 13 my $self = shift;
197 13 return $self->{records_changed};
198 }
199  
200 13 ▶sub get_records_unchanged {
Subroutine get_records_unchanged: called CC=1 SCAR=0.0
Pod documented
201 13 my $self = shift;
202 13 return $self->{records_unchanged};
203 }
204  
205 13 ▶sub get_records_deleted {
Subroutine get_records_deleted: called CC=1 SCAR=0.0
Pod documented
206 13 my $self = shift;
207 13 return $self->{records_deleted};
208 }
209  
210 10 ▶sub get_header_status {
Subroutine get_header_status: called CC=1 SCAR=0.0
Pod documented
211 10 my $self = shift;
212 10 return $self->{header_status};
213 }
214  
215 7 1;
216  
217  
218 #################### DOCUMENTATION ###################
219  
220 =head1 NAME
221  
222 List::RewriteElements - Create a new list by rewriting elements of a first list
223  
224 =head1 SYNOPSIS
225  
226 use List::RewriteElements;
227  
228 =head2 Constructor
229  
230 Simplest case: Input from array, output to STDOUT.
231  
232 $lre = List::RewriteElements->new( {
233 list => \@source,
234 body_rule => sub {
235 my $record = shift;
236 $record .= q{additional field};
237 },
238 } );
239  
240 Input from file, output to STDOUT:
241  
242 $lre = List::RewriteElements->new( {
243 file => "/path/to/source/file",
244 body_rule => sub {
245 my $record = shift;
246 $record .= q{,additional field};
247 },
248 } );
249  
250 Provide a different rule for the first element in the list:
251  
252 $lre = List::RewriteElements->new( {
253 file => "/path/to/source/file",
254 header_rule => sub {
255 my $record = shift;
256 $record .= q{,ADDITIONAL HEADER};
257 },
258 body_rule => sub {
259 my $record = shift;
260 $record .= q{,additional field};
261 },
262 } );
263  
264 Input from file, output to file:
265  
266 $lre = List::RewriteElements->new( {
267 file => "/path/to/source/file",
268 body_rule => sub {
269 my $record = shift;
270 $record .= q{additional field};
271 },
272 output_file => "/path/to/output/file",
273 } );
274  
275 To name output file, just provide a suffix to filename:
276  
277 $lre = List::RewriteElements->new( {
278 file => "/path/to/source/file",
279 body_rule => sub {
280 my $record = shift;
281 $record .= q{additional field};
282 },
283 output_suffix => '.out',
284 } );
285  
286 Provide criteria to suppress output of header or individual record.
287  
288 $lre = List::RewriteElements->new( {
289 file => "/path/to/source/file",
290 header_suppress => sub {
291 my $record = shift;
292 return if $record =~ /$somepattern/;
293 },
294 body_suppress => sub {
295 my $record = shift;
296 return if $record ne 'somestring';
297 },
298 body_rule => sub {
299 my $record = shift;
300 $record .= q{additional field};
301 },
302 } );
303  
304 =head2 Generate Output
305  
306 $lre->generate_output();
307  
308 =head2 Report Output Information
309  
310 $path_to_output_file = $lre->get_output_path();
311  
312 $output_file_basename = $lre->get_output_basename();
313  
314 $output_row_count = $lre->get_total_rows();
315  
316 $output_record_count = $lre->get_total_records();
317  
318 $records_changed = $lre->get_records_changed();
319  
320 $records_unchanged = $lre->get_records_unchanged();
321  
322 $records_deleted = $lre->get_records_deleted();
323  
324 $header_status = $lre->get_header_status();
325  
326 =head1 DESCRIPTION
327  
328 It is common in many situations for you to receive a flat data file from
329 someone else and have to generate a new file in which each row or record in
330 the incoming file must either (a) be transformed according to some rule before
331 being printing to the new file; or (b) if it meets certain criteria, not
332 output to the new file at all.
333  
334 List::RewriteElements enables you to write such rules and criteria, generate
335 the file of transformed data records, and get back some basic statistics about
336 the transformation.
337  
338 List::RewriteElements is useful when the number of records in the incoming
339 file may be large and you do not want to hold the entire list in memory.
340 Similarly, the newly generated records are not held in memory but are
341 immediately C<print>ed to STDOUT or to file.
342  
343 On the other hand, if for some reason you already have an array of records in
344 memory, you can use List::RewriteElements to apply rules and criteria to each
345 element of the array and then print the transformed records (again, without
346 holding the output in memory).
347  
348 =head1 SUBROUTINES
349  
350 =head2 C<new()>
351  
352 B<Purpose:> List::RewriteElements constructor.
353  
354 B<Arguments:> Reference to a hash holding the following keys:
355  
356 =over 4
357  
358 =item * C<file> or C<list>
359  
360 The hash must hold either a C<file> element or a C<list> element -- but not
361 both! The value for the C<file> key must be an absolute path to an input
362 file. The value for C<list> must be a reference to an array in memory.
363  
364 =item * C<body_rule>
365  
366 The hash must have a C<body_rule> element whose value is a reference to a
367 subroutine providing a formula for the transformation of an individual record
368 in the incoming file to a record in the outgoing file. The first argument
369 passed to this subroutine must be the record from the incoming file. The
370 return value from this subroutine should be a string immediately ready for
371 printing to the output file (though the string should not end in a newline, as
372 printing will be handled by C<generate_output()>).
373  
374 =item * C<body_suppress>
375  
376 Optionally, you may provide a C<body_suppress> element whose value is a
377 reference to a subroutine providing a criterion according to which an
378 individual record in the incoming file should be output to the outgoing file
379 or not output, I<i.e.>, omitted from the output entirely. The first argument
380 to this subroutine should be the record from the incoming file. The
381 subroutine should, at least implicitly, return a true value when the record
382 I<should> be output. The subroutine should simply C<return>, <i.e.>,
383 return an implicit C<undef>, when the record should be omitted from the
384 outgoing file.
385  
386 =item * C<header_rule>
387  
388 Frequently the first row in a flat data file is a header row containing, say,
389 the names of the columns in a data table, joined by a delimiter. Because the
390 header row is different from all subsequent rows, you may optionally provide a
391 C<header_rule> element whose value is a reference to a
392 subroutine providing a formula for the transformation of the header row
393 in the incoming file to the header in the outgoing file. The first argument
394 passed to this subroutine must be the header row from the incoming file. The
395 return value from this subroutine should be a string immediately ready for
396 printing to the output file (though the string should not end in a newline, as
397 printing will be handled by C<generate_output()>).
398  
399 =item * C<header_suppress>
400  
401 Optionally, if you have provided a C<header_rule> element, you may provide
402 a C<header_suppress> element whose value is a
403 reference to a subroutine providing a criterion according to which an
404 the header row from the incoming file should be output to the outgoing file
405 or not output, I<i.e.>, omitted from the output entirely. The first argument
406 to this subroutine should be the header from the incoming file. The
407 subroutine should, at least implicitly, return a true value when the header
408 I<should> be output. The subroutine should simply C<return>, <i.e.>,
409 return an implicit C<undef>, when the header should be omitted from the
410 outgoing file.
411  
412 =item * C<output_file> or C<output_suffix>
413  
414 It is recommended that you supply either an C<output_file> or an
415 C<output_suffix> element to the constructor; otherwise, the new list generated
416 by application of the rules and criteria will simply C<print> to C<STDOUT>.
417 The value of an C<output_file> element should be a full path to the newly
418 created file. If you wish to create a new file name without specifying a full
419 path but simply by tacking on a suffix to the name of the incoming file,
420 provide an C<output_suffix> element and the outgoing file will be created in
421 the directory which is the I<current working directory> as of the point where
422 C<generate_output()> is called. An C<output_suffix> element will
423 be ignored if an C<output_file> element is provided.
424  
425 =item * Note 1
426  
427 If neither a C<header_rule> or C<header_suppress> element is provide to the
428 constructor, List::RewriteElements will treat the first row of the incoming
429 file the same as any other row, C<i.e.>, it will apply the C<body_rule>
430 transformation formula.
431  
432 =item * Note 2
433  
434 A C<body_suppress> or C<header_suppress> criterion, if present, will be
435 logically applied I<before> any C<body_rule> or C<header_rule> formula. We
436 don't apply the formula to transform a record if the record should not be
437 output at all.
438  
439 =item * Note 3
440  
441 =back
442  
443 B<Return Value:> List::RewriteElements object.
444  
445 =head2 C<generate_output()>
446  
447 B<Purpose:> Generates the output specified by arguments to C<new()>,
448 I<i.e.>, creates an output file or C<print>s to C<STDOUT> with records
449 transformed as per those arguments.
450  
451 B<Arguments:> None.
452  
453 B<Return Value:> Returns true value upon success. In case of failure it will
454 C<croak> with some error message.
455  
456 =head2 C<get_output_path()>
457  
458 B<Purpose:> Get the full path to the newly created output file.
459  
460 B<Arguments:> None.
461  
462 B<Return Value:> String holding path to newly created output file.
463  
464 B<Comment:> Since use of the C<output_suffix> attribute means that the full
465 path to the output file will not be known until C<generate_output()> has been
466 called, C<get_output_path()> will only give a meaningful result once
467 C<generate_output()> has been called. Otherwise, it will default to an empty
468 string.
469  
470 =head2 C<get_output_basename()>
471  
472 B<Purpose:> Get only the basename of the newly created output file.
473  
474 B<Arguments:> None.
475  
476 B<Return Value:> String holding basename of newly created output file.
477  
478 B<Comment:> Since use of the C<output_suffix> attribute means that the full
479 path to the output file will not be known until C<generate_output()> has been
480 called, C<get_output_basename()> will only give a meaningful result once
481 C<generate_output()> has been called. Otherwise, it will default to an empty
482 string.
483  
484 =head2 C<get_total_rows()>
485  
486 B<Purpose:> Get the total number of rows in the newly created output file.
487 This will include any header row.
488  
489 B<Arguments:> None.
490  
491 B<Return Value:> Nonnegative integer.
492  
493 =head2 C<get_total_records()>
494  
495 B<Purpose:> Get the total number of data records in the newly created output
496 file. If a header row is present in that file, C<get_total_records()> will
497 return a value C<1> less than that returned by C<get_total_rows()>.
498  
499 B<Arguments:> None.
500  
501 B<Return Value:> Nonnegative integer.
502  
503 =head2 C<get_records_changed()>
504  
505 B<Purpose:> Get the number of data records in the newly created output file
506 that are altered versions of records in the incoming file. This value does
507 not include changes in the header row.
508  
509 B<Arguments:> None.
510  
511 B<Return Value:> Nonnegative integer.
512  
513 =head2 C<get_records_unchanged()>
514  
515 B<Purpose:> Get the number of data records in the newly created output file
516 that are unaltered versions of records in the incoming file. This value does
517 not include changes in the header row.
518  
519 B<Arguments:> None.
520  
521 B<Return Value:> Nonnegative integer.
522  
523 =head2 C<get_records_deleted()>
524  
525 B<Purpose:> Get the number of data records in the original source (file or
526 list) that were omitted from the newly created output file due to application
527 of a C<body_suppress> criterion. This value does not include any suppression
528 of a header row following application of a C<header_suppress> criterion.
529  
530 B<Arguments:> None.
531  
532 B<Return Value:> Nonnegative integer.
533  
534 =head2 C<get_header_status()>
535  
536 B<Purpose:> Indicate whether any header row in the original source (file or
537 list)
538  
539 =over 4
540  
541 =item *
542  
543 was rewritten in the newly created output file: return value C<1>;
544  
545 =item *
546  
547 was transferred to the newly created output file without alteration: return
548 value C<0>;
549  
550 =item *
551  
552 was suppressed from appearing in the output file by application of a
553 C<header_suppress> criterion: return value C<-1>;
554  
555 =item *
556  
557 no header row in the source: return value C<undef>.
558  
559 =back
560  
561 B<Arguments:> None.
562  
563 B<Return Value:> Numerical flag: C<1>, C<0>, C<-1> or C<undef> as described
564 above.
565  
566 =head1 FAQ
567  
568 =head2 Can I simultaneously rewrite records and interact with the external environment?
569  
570 Yes. If a C<header_rule>, C<body_rule>, C<header_suppress> or C<body_suppress>
571 either (a) needs additional information from the external environment above
572 and beyond that contained in the individual data record or (b) needs to cause
573 a change in the external environment, you can write a closure and call that
574 closure insider the rule.
575  
576 Example:
577  
578 my @greeks = qw( alpha beta gamma );
579
580 my $get_a_greek = sub {
581 return (shift @greeks);
582 };
583  
584 my $lre = List::RewriteElements->new ( {
585 list => [ map {"$_\n"} (1..5) ],
586 body_rule => sub {
587 my $record = shift;
588 my $rv;
589 chomp $record;
590 if ($record eq '4') {
591 $rv = &{$get_a_greek};
592 } else {
593 $rv = (10 * $record);
594 }
595 return $rv;
596 },
597 body_suppress => sub {
598 my $record = shift;
599 chomp $record;
600 return if $record eq '5';
601 },
602 } );
603  
604 $lre->generate_output();
605  
606 This will produce:
607  
608 10
609 20
610 30
611 alpha
612  
613 =head2 Can I use List-Rewrite Elements with fixed-width data?
614  
615 Yes. Suppose that you have this fixed-width data (adapted from Dave Cross'
616 I<Data Munging with Perl>):
617  
618 my @dataset = (
619 q{00374Bloggs & Co 19991105100103+00015000},
620 q{00375Smith Brothers 19991106001234-00004999},
621 q{00376Camel Inc 19991107289736+00002999},
622 q{00377Generic Code 19991108056789-00003999},
623 );
624  
625 Suppose further that you need to update certain records and that C<%revisions>
626 holds the data for updating:
627  
628 my %revisions = (
629 376 => [ 'Camel Inc', 20061107, 388293, '+', 4999 ],
630 377 => [ 'Generic Code', 20061108, 99821, '-', 6999 ],
631 );
632  
633 Write a C<body_rule> subroutine which uses C<unpack>, C<pack> and C<sprintf>
634 as needed to update the records.
635  
636 my $lre = List::RewriteElements->new ( {
637 list => \@dataset,
638 body_rule => sub {
639 my $record = shift;
640 my $template = 'A5A18A8A6AA8';
641 my @rec = unpack($template, $record);
642 $rec[0] =~ s/^0+//;
643 my ($acctno, %values, $result);
644 $acctno = $rec[0];
645 $values{$acctno} = [ @rec[1..$#rec] ];
646 if ($revisions{$acctno}) {
647 $values{$acctno} = $revisions{$acctno};
648 }
649 $result = sprintf "%05d%-18s%8d%06d%1s%08d",
650 ($acctno, @{$values{$acctno}});
651 return $result;
652 },
653 } );
654  
655 =head2 How does this differ from Tie::File?
656  
657 Mark Jason Dominus' Tie::File module is one of my Fave 5 CPAN modules. It's
658 excellent for modifying a file in place. But I frequently have to leave the
659 source file unmodified and create a new file, which implies, at the very
660 least, opening, printing to, and closing filehandles in addition to using
661 Tie::File. List::RewriteElements hides all
662 that. It also provides the statistical report methods.
663  
664 =head2 Couldn't I do this with C<map> and C<grep>?
665  
666 Quite possibly. But if your rules and criteria were complicated or long, the
667 content of the C<map> and C<grep> C<{}> blocks would be hard to read. You
668 also wouldn't get the statistical report methods.
669  
670 =head2 How Does It Work?
671  
672 Why do you care? Why do you want to look inside the black box? If you really
673 want to know, read the source!
674  
675 =head1 PREREQUISITES
676  
677 List::RewriteElements relies only on modules distributed with the Perl core as
678 of 5.8.0. IO::Capture::Stdout is required for the test suite, but a copy is
679 included in the distribution under the F<t/> directory.
680  
681 =head1 BUGS
682  
683 File bug reports at L<https://github.com/jkeenan/List-RewriteElements/issues>.
684  
685 One test failure reported during testing on Win32 via GitHub Actions.
686  
687 =head1 ACKNOWLEDGEMENTS
688  
689 Thanks to David Landgren for raising the question of use of
690 List-RewriteElements with fixed-width data.
691  
692 I then adapted an example from Dave Cross' I<Data Munging with Perl>,
693 Chapter 7.1, "Fixed-width Data," to provide a test
694 demonstrating processing of fixed-width data.
695  
696 =head1 AUTHOR
697  
698 James E Keenan. CPAN ID: JKEENAN. jkeenan@pobox.com
699 L<http://thenceforward.net/perl/modules/List-RewriteElements>.
700  
701 =head1 COPYRIGHT
702  
703 Copyright 2006-2026 James E Keenan (USA).
704  
705 This program is free software; you can redistribute
706 it and/or modify it under the same terms as Perl itself.
707  
708 The full text of the license can be found in the
709 LICENSE file included with this module.
710  
711 =head1 SEE ALSO
712  
713 David Cross, I<Data Munging with Perl> (Manning, 2001).
714  
715 =cut
716  
717  
↑ Summary