Porf/Table/Simple/CsvReportConfigurator.pm

filter: Statementstmt 91.8% 136 / 148 Branchbran 47.3% 18 / 38 Conditioncond 33.3% 4 / 12 MC/DCmcdc 0.0% 0 / 8 Subroutinesub 90.9% 20 / 22 Podpod 0.0% 0 / 9 totaltotal 75.1% 178 / 237 CC 26 SCAR 25.4
CC
26
Cov
80%
__ANON__
29.6
__ANON__
5.1
__ANON__
3.0
CRAP 12.7
« Porf/Table/Simple/TextReportConfigurator.pm ↑ Summary Porf/examples/example_array.pl »
1 # perl
2 #
3 # Class Report::Porf::Table::Simple::CsvReportConfigurator
4 #
5 # Perl Open Report Framework (Porf)
6 #
7 # Configures a Report::Porf::Table::Simple to write out ASCII- or UTF-Csv tables
8 #
9 # Ralf Peine, Tue May 27 11:29:22 2014
10 #
11 # More documentation at the end of file
12 #------------------------------------------------------------------------------
13  
14 1 $VERSION = "2.001";
15  
16 #------------------------------------------------------------------------------
17 #
18 # Example list with 10 lines (csv)
19 #
20 # # Count,Prename,Surname,Age,TimeStamp
21 # 1,Vorname 1,Name 1,10,0.002329
22 # 2,Vorname 2,Name 2,20,0.003106
23 # 3,Vorname 3,Name 3,30,0.003822
24 # 4,Vorname 4,Name 4,40,0.004533
25 # 5,Vorname 5,Name 5,50,0.005235
26 # 6,Vorname 6,Name 6,60,0.005944
27 # 7,Vorname 7,Name 7,70,0.006656
28 # 8,Vorname 8,Name 8,80,0.007362
29 # 9,Vorname 9,Name 9,90,0.008069
30 # 10,Vorname 1,Name 10,100,0.008779
31 #
32 # # Time needed for export of 10 data lines: 0.001954
33 #
34 #------------------------------------------------------------------------------
35  
36 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38  
39 #--------------------------------------------------------------------------------
40 #
41 # Report::Porf::Table::Simple::CsvReportConfigurator;
42 #
43 #--------------------------------------------------------------------------------
44  
45 package Report::Porf::Table::Simple::CsvReportConfigurator;
46  
47 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 1 ▶use Report::Porf::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 1 ▶use Report::Porf::Table::Simple;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 #--------------------------------------------------------------------------------
53 #
54 # Creation / Filling Of Instances
55 #
56 #--------------------------------------------------------------------------------
57  
58 # --- create Instance -----------------
59 sub new
60 3 ▶{
Subroutine new: called CC=2 SCAR=7.1
Pod undocumented
61 3 my $caller = $_[0];
62 3 ▶ my $class = ref($caller) || $caller;
Condition33%1/3
ref $caller || $caller
l!l&&r!l&&!r
030
MC/DC0%0/2
ref $caller || $caller
ref $caller $caller
Truth table33%1/3
A || B
A= ref $callerB= $caller
ABresult
1X1
011
000
63
64 # let the class go
65 3 my $self = {};
66 3 bless $self, $class;
67  
68 3 $self->_init();
69  
70 3 return $self;
71 }
72  
73 # --- Init ------------------------------------------------------------------
74  
75 3 ▶sub _init {
Subroutine _init: called CC=1 SCAR=0.0
76 3 my ($self # instance_ref
77 ) = @_;
78 3 $self->set_data_separator_char(',');
79 3 $self->set_data_string_char('"');
80 }
81  
82 # --- verbose ---------------------------------------------------------------
83  
84 0 ▶sub set_verbose {
Subroutine set_verbose: not called CC=1 SCAR=6.9
Pod undocumented
85 0 my ($self, # instance_ref
86 $value # value to set
87 ) = @_;
88
89 0 $self->{verbose} = $value;
90 }
91  
92 0 ▶sub get_verbose {
Subroutine get_verbose: not called CC=1 SCAR=6.9
Pod undocumented
93 0 my ($self, # instance_ref
94 ) = @_;
95
96 0 return $self->{verbose};
97 }
98  
99 # --- DataSeparatorChar ---------------------------------------------------------
100  
101 3 ▶sub set_data_separator_char {
Subroutine set_data_separator_char: called CC=1 SCAR=0.0
Pod undocumented
102 3 my ($self, # instance_ref
103 $value # value to set
104 ) = @_;
105
106 3 $self->{DataSeparatorChar} = $value;
107 }
108  
109 20 ▶sub get_data_separator_char {
Subroutine get_data_separator_char: called CC=1 SCAR=0.0
Pod undocumented
110 20 my ($self, # instance_ref
111 ) = @_;
112
113 20 return $self->{DataSeparatorChar};
114 }
115  
116 # --- DataStringChar ---------------------------------------------------------
117  
118 3 ▶sub set_data_string_char {
Subroutine set_data_string_char: called CC=1 SCAR=0.0
Pod undocumented
119 3 my ($self, # instance_ref
120 $value # value to set
121 ) = @_;
122
123 3 $self->{DataStringChar} = $value;
124 }
125  
126 14 ▶sub get_data_string_char {
Subroutine get_data_string_char: called CC=1 SCAR=0.0
Pod undocumented
127 14 my ($self, # instance_ref
128 ) = @_;
129
130 14 return $self->{DataStringChar};
131 }
132  
133 #--------------------------------------------------------------------------------
134 #
135 # Configure format
136 #
137 #--------------------------------------------------------------------------------
138  
139 # --- create new report and configure -------------------------------------------
140 2 ▶sub create_and_configure_report {
Subroutine create_and_configure_report: called CC=1 SCAR=0.0
Pod undocumented
141 2 my ($self # instance_ref
142 ) = @_;
143  
144 2 return $self->configure_report(Report::Porf::Table::Simple->new());
145 }
146  
147 # --- Configure Export For Csv ------------------------------------------------
148 3 ▶sub configure_report {
Subroutine configure_report: called CC=1 SCAR=0.1
Pod undocumented
149 3 my ($self, # instance_ref
150 $report2configure, # report to be configured
151 ) = @_;
152  
153 # $report2configure->set_default_column_width (10);
154 3 $report2configure->set_default_align ('Left');
155  
156 3 $report2configure->set_file_start('');
157 3 $report2configure->set_file_end ('');
158  
159 3 $report2configure->set_table_start("");
160 3 $report2configure->set_table_end ("");
161  
162 3 $report2configure->set_row_start("");
163 3 $report2configure->set_row_end ("\n");
164
165 3 $report2configure->set_header_row_start("# ");
166 3 $report2configure->set_header_row_end ("\n");
167 3 $report2configure->set_header_start("");
168 3 $report2configure->set_header_end ($self->get_data_separator_char());
169
170 3 $report2configure->set_cell_start("");
171 3 $report2configure->set_cell_end ($self->get_data_separator_char());
172  
173 #===================================================================================
174 #
175 # Configure Actions, no need to speed up
176 #
177 #===================================================================================
178  
179 # === Column store Action ============================================================
180 $report2configure->set_configure_column_action(
181 14 ▶ sub {
Subroutine __ANON__: called CC=18 SCAR=33.9
182 14 my $report = shift; # instance_ref
183 14 my %options = @_; # options
184
185 14 ▶ print_hash_ref(\%options) if verbose($report, 2);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 2) 0 14 14
186  
187 14 my $cell_start = $report->get_cell_start();
188 14 my $cell_end = $report->get_cell_end();
189  
190 14 my $left = ""; # left from value
191 14 my $right = ""; # right from value
192  
193 # --- default value ---------------------------------------
194 14 my $default_value = get_option_value(\%options, qw (-default_value -def_val -dv));
195 14 ▶ $default_value = $report->get_default_cell_value() unless defined $default_value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_value 14 0 14
196  
197 # --- value ---------------------------------------
198 14 my $value = interprete_value_options(\%options);
199 14 my $value_ref = ref($value);
200 14 my $value_action;
201  
202 14 ▶ die "value action not defined" unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 0 14 14
203
204 14 ▶ if ($value_ref) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_ref) { } 2 12 14
205 2 ▶ if ($value_ref =~ /^CODE/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value_ref =~ /^CODE/) { } 2 0 2
206 2 $value_action = $value;
207 }
208 else {
209 # or what ??
210 0 die "# ref(value_ref) = $value_ref unknown";
211 # $value_action = eval ("sub { return $$value; };");
212 }
213 }
214 else {
215 12 $value = complete_value_code($value, $default_value);
216 12 $value_action = $report->create_action("$value;");
217 }
218  
219 # --- format value ----------------------------------------
220 14 my $format = get_option_value(\%options, qw (-format -f));
221  
222 14 ▶ if ($format) {
Branch: 1 branch
BranchTrueFalseTotal
if ($format) 0 14 14
223 0 my $format_ref = ref($format);
224  
225 0 ▶ die "You used a '$format_ref' type to set the 'Format', "
Branch: 1 branch
BranchTrueFalseTotal
if $format_ref 0 0 0
226 ."but currently only strings are supported!"
227 if $format_ref;
228
229 0 ▶ print "format $format\n" if verbose($report, 3);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) 0 0 0
230  
231 0 $left = "sprintf(\"$format\", ".$left;
232 0 $right .= ")";
233 }
234  
235 # --- coloring is not supported for csv ------------------
236 14 my $color = get_option_value(\%options, qw (-color -c));
237  
238 14 ▶ print "color cannot be supported for csv (color = $color)\n" if verbose($report, 3) && $color;
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) and $color 0 14 14
Condition33%1/3
verbose($report, 3) and $color
!ll&&!rl&&r
1400
MC/DC0%0/2
verbose($report, 3) and $color
verbose($report, 3) $color
Truth table33%1/3
A and B
A= verbose($report, 3)B= $color
ABresult
0X0
100
111
239  
240 # --- width / align ---------------------------------------
241 14 my $width = get_option_value(\%options, qw (-width -w));
242 14 my $align = get_option_value(\%options, qw (-align -a));
243  
244 14 ▶ print "width cannot be supported for csv (width = $width)\n" if verbose($report, 3) && $width;
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) and $width 0 14 14
Condition33%1/3
verbose($report, 3) and $width
!ll&&!rl&&r
1400
MC/DC0%0/2
verbose($report, 3) and $width
verbose($report, 3) $width
Truth table33%1/3
A and B
A= verbose($report, 3)B= $width
ABresult
0X0
100
111
245 14 ▶ print "align cannot be supported for csv (align = $align)\n" if verbose($report, 3) && $align;
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) and $align 0 14 14
Condition33%1/3
verbose($report, 3) and $align
!ll&&!rl&&r
1400
MC/DC0%0/2
verbose($report, 3) and $align
verbose($report, 3) $align
Truth table33%1/3
A and B
A= verbose($report, 3)B= $align
ABresult
0X0
100
111
246
247 # --- configure header ----------------------------
248  
249 14 my $header_text = get_option_value(\%options, qw (-header -h));
250 14 ▶ $header_text = '' unless defined $header_text;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $header_text 0 14 14
251 14 push (@{$report->get_header_texts_ref()}, $header_text);
252
253 # --- ---------------------------------------------
254 # --- ---------------------------------------------
255 # --- escape separator / string char ----------------------------------------
256  
257 14 my $sep_char = $self->get_data_separator_char();
258 14 my $string_char = $self->get_data_string_char();
259
260 18 ▶ my $escape_sep_char_sub_ref = sub {
Subroutine __ANON__: called CC=2 SCAR=8.4
261 18 my $value = $_[0];
262  
263 18 ▶ if ($value =~ /$sep_char|$string_char/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /$sep_char|$string_char/) 0 18 18
264 0 $value =~ s/$string_char/$string_char$string_char/g;
265 0 $value = "$string_char$value$string_char";
266 }
267  
268 18 return $value;
269 14 };
270
271 14 $left = '$escape_sep_char_sub_ref->('.$left;
272 14 $right .= ")";
273
274 # --- build cell content action ---------------------------------------------
275 14 my $cell_action_str = $left.'$value_action->($_[0])'.$right;
276  
277 14 my $eval_str = 'sub { return $cell_start.'.$cell_action_str.'.$cell_end; }';
278  
279 14 ▶ print "### eval_str = sub { return $cell_start$cell_action_str$cell_end; }\n"
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) 0 14 14
280 if verbose($report, 3);
281
282 14 my $cell_action = eval ($eval_str);
283
284 14 ▶ print "### ref(cell_action) ".ref($cell_action) ."\n" if verbose($report, 3);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 3) 0 14 14
285
286 14 $report->add_cell_output_action($cell_action);
287 3 });
288  
289 # === Configure Complete Action ====================================================
290 $report2configure->set_configure_complete_action(
291 3 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
292 3 my ($report, # instance_ref
293 ) = @_;
294  
295 3 $report->set_header_line ('');
296 3 $report->set_separator_line ('');
297 3 $report->set_bold_header_line('');
298 3 });
299
300 3 my $cell_end = $report2configure->get_cell_end();
301  
302 #===================================================================================
303 #
304 # Runtime Actions, no need to speed up
305 #
306 #===================================================================================
307  
308 # === Header Output Action ============================================================
309 $report2configure->set_header_output_action(
310 1 ▶ sub {
Subroutine __ANON__: called CC=3 SCAR=11.0
311 1 my ($report, # instance_ref
312 $data_ref # data to output
313 ) = @_;
314  
315 1 my $header_string = $report->get_header_row_start();
316 1 my $c = 0;
317 1 foreach my $header_text (@{$data_ref}) {
318 4 $header_string .= $report->get_header_start()
319 .$header_text
320 .$report->get_header_end();
321 4 $c++;
322 }
323  
324 1 $header_string =~ s/$cell_end$//;
325 1 $header_string .= $report->get_header_row_end();
326  
327 1 ▶ print "### Header String:$header_string" if verbose($report, 2);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 2) 0 1 1
328
329 1 return $header_string;
330 3 });
331  
332 # === Start Table Output Action ============================================================
333 $report2configure->set_start_table_output_action(
334 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
335 1 my ($report, # instance_ref
336 $data_ref # data to output
337 ) = @_;
338  
339 1 return $report->get_file_start()
340 .$report->get_table_start()
341 .$report->get_header_output();
342
343
344 3 });
345  
346 # === End Table Output Action ============================================================
347 $report2configure->set_end_table_output_action(
348 1 ▶ sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
349 1 my ($report, # instance_ref
350 $data_ref # data to output
351 ) = @_;
352  
353 1 return $report->get_table_end()
354 .$report->get_file_end();
355 3 });
356  
357 #===================================================================================
358 #
359 # Runtime Mass Data Actions, be careful, don't slow it down !!
360 #
361 #===================================================================================
362  
363 3 my $previous_data_ref = '';
364  
365 # === Row Output Action ============================================================
366 $report2configure->set_row_output_action(
367 4 ▶ sub {
Subroutine __ANON__: called CC=5 SCAR=16.3
368 4 my ($report, # instance_ref
369 $data_ref # data to output
370 ) = @_;
371  
372 4 my $cell_output_actions_ref = $report->get_cell_output_actions();
373  
374 4 my $row_string = '';
375  
376 # --- Add something when group changes ----
377 4 my $row_group_changes_action = $report->get_row_group_changes_action();
378
379 4 ▶ $row_string .= $row_group_changes_action->($previous_data_ref, $data_ref)
Branch: 1 branch
BranchTrueFalseTotal
if $row_group_changes_action 0 4 4
380 if $row_group_changes_action;
381  
382 4 $previous_data_ref = $data_ref;
383  
384 # --- start new row -----------------------
385 4 $row_string .= $report->get_row_start();
386 4 foreach my $action (@$cell_output_actions_ref) {
387 18 ▶ print "### action $action\n" if verbose($report, 4);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 4) 0 18 18
388 18 $row_string .= $action->($data_ref);
389 }
390  
391 4 $row_string =~ s/$cell_end$//;
392 4 $row_string .= $report->get_row_end();
393  
394 4 ▶ print "### Row String:$row_string" if verbose($report, 2);
Branch: 1 branch
BranchTrueFalseTotal
if verbose($report, 2) 0 4 4
395
396 4 return $row_string;
397 3 });
398  
399 3 return $report2configure;
400 }
401 1 1;
402  
403 =head1 NAME
404  
405 C<Report::Porf::Table::Simple::CsvReportConfigurator>
406  
407 Configures a Report::Porf::Table::Simple to write out Csv files.
408  
409 Part of Perl Open Report Framework (Porf).
410  
411 =head1 Documentation
412  
413 See Report::Porf::Framework.pm for documentation of features and usage.
414  
415 =head1 LICENSE AND COPYRIGHT
416  
417 Copyright (c) 2013 by Ralf Peine, Germany. All rights reserved.
418  
419 This library is free software; you can redistribute it and/or modify
420 it under the same terms as Perl itself, either Perl version 5.6.0 or,
421 at your option, any later version of Perl 5 you may have available.
422  
423 =head1 DISCLAIMER OF WARRANTY
424  
425 This library is distributed in the hope that it will be useful,
426 but without any warranty; without even the implied warranty of
427 merchantability or fitness for a particular purpose.
428  
429 =cut
« Porf/Table/Simple/TextReportConfigurator.pm ↑ Summary Porf/examples/example_array.pl »