blib/lib/Parse/CSV/Colnames.pm

filter: Statementstmt 96.9% 32 / 33 Branchbran 66.6% 4 / 6 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 90.9% 10 / 11 Podpod 100% 5 / 5 totaltotal 92.7% 51 / 55 CC 4 SCAR 5.0
CC
4
Cov
92%
colnames
3.0
pushcolnames
2.0
print
2.0
CRAP 1.6
↑ Summary
1 package Parse::CSV::Colnames;
2  
3 =pod
4  
5 =head1 NAME
6  
7 Parse::CSV::Colnames - Highly flexible CSV parser including column names (field names) manipulation
8  
9 =head1 NOTE
10  
11 This Module derives from L<Parse::CSV> by Adam Kennedy inheriting its methods.
12 The main extensions are methods for column names manipulation and some simple method-fixes.
13  
14 =head1 SYNOPSIS
15  
16 Column names manipulation makes only sense if the fields-parameter is auto, i.e. column names are in the first line.
17  
18 # Parse a colon-separated variables file from a handle as a hash
19 # based on headers from the first line.
20 my $objects = Parse::CSV::Colnames->new(
21 handle => $io_handle,
22 sep_char => ';',
23 fields => 'auto',
24 # select only rows where column name fieldname is "value"
25 filter => sub { if($_->{fieldname} eq "value")
26 {$_} else
27 {undef}
28 }
29 );
30  
31 # get column names
32 my @fn=$objects->colnames
33 # you want lower case field names
34 @fn=map {lc} @fn;
35 # you want field names without blanks
36 @fn=map { s/\s+//g} @fn;
37 # set column names
38 $objects->colnames(@fn);
39  
40 while ( my $object = $objects->fetch ) {
41 $object->do_something;
42 }
43  
44 =head1 DESCRIPTION
45  
46 This module is only an extension of L<Parse::CSV>
47  
48 For a detailed description of all methods see L<Parse::CSV>
49  
50 For a detailed description of the underlying csv-parser see L<Text::CSV_XS>
51  
52  
53 =cut
54  
55 2 ▶use 5.005;
Subroutine BEGIN: called CC=1 SCAR=0.0
56 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
57 2 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
58 #use IO::File ();
59 #use Text::CSV_XS ();
60 #use Params::Util qw{ _STRING _ARRAY _HASH0 _CODELIKE _HANDLE };
61  
62 2 ▶use Parse::CSV;
Subroutine BEGIN: called CC=1 SCAR=0.0
63 2 our @ISA=("Parse::CSV");
64  
65 2 ▶use vars qw{$VERSION};
Subroutine BEGIN: called CC=1 SCAR=0.0
66 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
67 2 $VERSION = '0.07';
68 }
69  
70  
71  
72  
73  
74  
75  
76 =pod
77  
78 =head1 Fixed METHODS
79  
80 These methods have not work in the parent module L<Parse::CSV> yet, because Adam Kennedy is very busy.
81  
82 =head2 combine
83  
84 $status = $csv->combine(@columns);
85  
86 The C<combine> method is passed through
87 to the underlying L<Text::CSV_XS> object. See example 3.
88  
89 It sets the fields and constructs the corresponding csv string from the arguments. You can read this array with the C<fields> method.
90  
91 =cut
92  
93 4 ▶sub combine {
Subroutine combine: called CC=1 SCAR=0.0
Pod documented
94 4 shift->{csv_xs}->combine(@_);
95 }
96  
97 =pod
98  
99 =head2 string (deprecated)
100  
101 $line = $csv->string;
102  
103 The C<string> method is passed through
104 to the underlying L<Text::CSV_XS> object. See example 3 and example 4.
105  
106 It returns the parsed string or the corresponding combine-setting.
107  
108 =cut
109  
110 #sub string {
111 # shift->{csv_xs}->string(@_);
112 #}
113  
114 =pod
115  
116 =head2 print
117  
118 $status = $csv->print($io, $colref);
119  
120 The C<print> method is passed through
121 to the underlying L<Text::CSV_XS> object. See example 1.
122  
123 It prints the string of the corresponding @$colref directly to an IO handle.
124  
125  
126 =cut
127  
128 0 ▶sub print {
Subroutine print: not called CC=1 SCAR=6.9
Pod documented
129 0 shift->{csv_xs}->print(@_);
130 }
131  
132 =pod
133  
134 =head1 Added METHODS
135  
136 =head2 fields (deprecated)
137  
138 @fields = $csv->fields;
139  
140 The C<fields> method is passed through
141 to the underlying L<Text::CSV_XS> object.
142  
143 It returns the input to C<combine>-method or the actual row as an array.
144  
145 =cut
146  
147 #sub fields {
148 # shift->{csv_xs}->fields;
149 #}
150  
151 =pod
152  
153 =head2 colnames
154  
155 @colnames = $csv->colnames("fn1","fn2") # sets colnames
156 or
157 @colnames = $csv->colnames; # gets colnames
158  
159 The C<colnames> method sets or gets colnames (=C<fields>-param).
160 So you can rename the colnames (hash-keys in L<Parse::CSV::Colnames> object).
161  
162 =cut
163  
164 3 ▶sub colnames {
Subroutine colnames: called CC=3 SCAR=11.0
Pod documented
165 3 my $self=shift;
166 3 ▶ $self->{fields}=$self->{names} if(exists($self->{names})); # quick and dirty
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"names"} 3 0 3
167 3 ▶ @{$self->{fields}}=@_ if(@_);
Branch: 1 branch
BranchTrueFalseTotal
if @_ 1 2 3
168 3 return @{$self->{fields}};
169 }
170  
171 =pod
172  
173 =head2 pushcolnames
174  
175 @colnames = $csv->pushcolnames("fn1","fn2")
176  
177 The C<pushcolnames> method adds colnames at the end of $csv->colnames (=C<fields>-param).
178 You can do that if the C<filter>-method adds some new fields at the end of fields-array in L<Parse::CSV::Colnames> object .
179 Please consider that these colnames or fields are not
180 in the underlying L<Text::CSV_XS> object. See example 1 and example 4.
181  
182 =cut
183  
184 1 ▶sub pushcolnames {
Subroutine pushcolnames: called CC=2 SCAR=7.0
Pod documented
185 1 my $self=shift;
186 1 ▶ $self->{fields}=$self->{names} if(exists($self->{names})); # quick and dirty
Branch: 1 branch
BranchTrueFalseTotal
if exists $self->{"names"} 1 0 1
187 1 push @{$self->{fields}},@_;
188 1 return @{$self->{fields}};
189 }
190  
191 =pod
192  
193 =head2 pushcombine
194  
195 @colnames = $csv->pushcombine("fn1","fn2")
196  
197 The C<pushcombine> method adds fields at the end of the actual row (=C<fields>-method) and constructs the corresponding csv string.
198 You can read the result with the C<fields>-method.
199 The pushcombine and pushcolnames belong together. See example 4.
200  
201 =cut
202  
203 1 ▶sub pushcombine {
Subroutine pushcombine: called CC=1 SCAR=0.0
Pod documented
204 1 my $self=shift;
205 1 $self->combine($self->fields,@_);
206 }
207  
208 2 1;
209  
210 =pod
211  
212 =head1 EXAMPLES
213  
214 You can test these examples with copy and paste
215  
216 =cut
217  
218 =pod
219  
220 =head2 Example 1
221  
222 Using C<< csv->print >>, C<< csv->pushcolnames >>
223  
224  
225 #!/usr/bin/perl
226  
227 use strict;
228 use warnings;
229 use Parse::CSV::Colnames;
230 my $fh=\*DATA;
231 my $fhout=\*STDOUT; # only for demo
232 my $csv = Parse::CSV::Colnames->new(
233 #file => "testnamen.csv",
234 handle => $fh,
235 sep_char => ';',
236 fields => 'auto',
237 binary => 1, # for german umlauts and utf
238 filter => sub { $_->{country}="Germany";
239 $_->{product}=$_->{factor1}*$_->{factor2};
240 # select only rows where column name product>0
241 if($_->{product}>0) {
242 $_;
243 } else {
244 undef
245 }
246 }
247 );
248  
249 # add colnames at the end
250 $csv->pushcolnames(qw(product country));
251 # get column names
252 my @fn=$csv->colnames;
253 # you want lower case field names
254 @fn=map {lc} @fn;
255 # you want field names without blanks
256 map { s/\s+//g} @fn;
257 # set column names
258 $csv->colnames(@fn);
259  
260 # headerline for direct output
261 $csv->print($fhout,[$csv->colnames]); # print header-line
262 print "\n";
263  
264  
265 while(my $line=$csv->fetch) {
266 # csv direct output
267 $csv->print($fhout,[$csv->fields,$line->{product},$line->{country}]); # only input-fields are printed with method fields
268 print "\n";
269 }
270  
271 __DATA__
272 Name;Given Name;factor1;factor2
273 Hurtig;Hugo;5.4;4.6
274 Schnallnichts;Carlo;6.4;4.6
275 Weissnich;Carola;7.4;4.6
276 Leer;Hinnerk;0;4.6
277 Keine Ahnung;Maximilian;8.4;4.6
278
279 =cut
280  
281 =pod
282  
283 =head2 Example 2
284  
285 Building new fields by hand with map
286  
287  
288  
289 #!/usr/bin/perl
290  
291 use strict;
292 use warnings;
293 use Parse::CSV::Colnames;
294 my $fh=\*DATA;
295 my $csv = Parse::CSV::Colnames->new(
296 #file => "testnamen.csv",
297 handle => $fh,
298 sep_char => ';',
299 fields => 'auto',
300 binary => 1, # for german umlauts
301 filter => sub { $_->{country}="Germany";
302 $_->{product}=$_->{factor1}*$_->{factor2};
303 # select only rows where column name product>0
304 if($_->{product}>0) {
305 $_;
306 } else {
307 undef
308 }
309 }
310 );
311 #add new fieldname at the end
312 $csv->pushcolnames(qw(product));
313 # get column names
314 my @fn=$csv->colnames;
315 # you want lower case field names
316 @fn=map {lc} @fn;
317 # you want field names without blanks
318 map { s/\s+//g} @fn;
319 # set column names
320 $csv->colnames(@fn);
321  
322 # headerline with only 2 fields
323 my @outcolnames1=(qw(givenname product));
324 print join(";",@outcolnames1) . "\n";
325  
326  
327 while(my $line=$csv->fetch) {
328 print join(";",map {$line->{$_}} @outcolnames1) . "\n";
329  
330 }
331  
332 __DATA__
333 Name;Given Name;factor1;factor2
334 Hurtig;Hugo;5.4;4.6
335 Schnallnichts;Carlo;6.4;4.6
336 Weissnich;Carola;7.4;4.6
337 Leer;Hinnerk;0;4.6
338 Keine Ahnung;Maximilian;8.4;4.6
339  
340 =cut
341  
342  
343 =pod
344  
345 =head2 Example 3
346  
347 Using C<< csv->combine >> and C<< csv->string >>
348  
349  
350 #!/usr/bin/perl
351  
352 use strict;
353 use warnings;
354 use Parse::CSV::Colnames;
355 my $fh=\*DATA;
356 my $csv = Parse::CSV::Colnames->new(
357 #file => "testnamen.csv",
358 handle => $fh,
359 sep_char => ';',
360 fields => 'auto',
361 binary => 1, # for german umlauts
362 filter => sub { $_->{country}="Germany";
363 $_->{product}=$_->{factor1}*$_->{factor2};
364 # select only rows where column name product>0
365 if($_->{product}>0) {
366 $_;
367 } else {
368 undef
369 }
370 }
371 );
372 $csv->pushcolnames(qw(product country));
373 # get column names
374 my @fn=$csv->colnames;
375 # you want lower case field names
376 @fn=map {lc} @fn;
377 # you want field names without blanks
378 map { s/\s+//g} @fn;
379 # set column names
380 $csv->colnames(@fn);
381  
382 # headerline
383 my @outcolnames2=(qw(givenname product country));
384 $csv->combine(@outcolnames2);
385 print $csv->string . "\n";
386  
387  
388 while(my $line=$csv->fetch) {
389 # csv output
390 $csv->combine(map {$line->{$_}} @outcolnames2);
391 print $csv->string . "\n";
392  
393  
394 }
395  
396  
397 __DATA__
398 Name;Given Name;factor1;factor2
399 Hurtig;Hugo;5.4;4.6
400 Schnallnichts;Carlo;6.4;4.6
401 Weissnich;Carola;7.4;4.6
402 Leer;Hinnerk;0;4.6
403 Keine Ahnung;Maximilian;8.4;4.6
404  
405 =cut
406  
407  
408 =pod
409  
410 =head2 Example 4
411  
412 Using C<< csv->pushcombine >> , C<< csv->pushcolnames >> and C<< csv->string >>
413  
414  
415 #!/usr/bin/perl
416  
417 use strict;
418 use warnings;
419 use Parse::CSV::Colnames;
420 my $fh=\*DATA;
421 my $csv = Parse::CSV::Colnames->new(
422 #file => "testnamen.csv",
423 handle => $fh,
424 sep_char => ';',
425 fields => 'auto',
426 binary => 1, # for german umlauts
427 filter => sub { $_->{country}="Germany";
428 $_->{product}=$_->{factor1}*$_->{factor2};
429 # select only rows where column name product>0
430 if($_->{product}>0) {
431 $_;
432 } else {
433 undef
434 }
435 }
436 );
437 $csv->pushcolnames(qw(product country));
438 # get column names
439 my @fn=$csv->colnames;
440 # you want lower case field names
441 @fn=map {lc} @fn;
442 # you want field names without blanks
443 map { s/\s+//g} @fn;
444 # set column names
445 $csv->colnames(@fn);
446  
447 # headerline
448 $csv->combine($csv->colnames);
449 print $csv->string . "\n";
450  
451  
452 while(my $line=$csv->fetch) {
453 # csv output
454 $csv->pushcombine(map {$line->{$_}} qw(product country));
455 # is like
456 $csv->pushcombine($line->{product},$line->{country});
457
458 print $csv->string . "\n";
459  
460  
461 }
462  
463  
464 __DATA__
465 Name;Given Name;factor1;factor2
466 Hurtig;Hugo;5.4;4.6
467 Schnallnichts;Carlo;6.4;4.6
468 Weissnich;Carola;7.4;4.6
469 Leer;Hinnerk;0;4.6
470 Keine Ahnung;Maximilian;8.4;4.6
471  
472 =cut
473  
474 =pod
475  
476 =head1 TODO
477  
478 Creating Methods C<popcolnames> and C<popcombine>. These methods delete the last fieldnames (column names) or fields. (I will add these methods if anybody wants this)
479  
480 Creating Methods C<(un)shiftcolnames> and C<(un)shiftcombine>. These methods add/delete the first fieldnames (column names) or fields. (I will add these methods if anybody wants this)
481  
482 Integrating methods C<getline_hr> and C<column_names> of the underlying object L<Text::CSV_XS>.
483  
484 =head1 SUPPORT
485  
486 Bugs should always be reported via the CPAN bug tracker at
487  
488 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Parse-CSV-Colnames>
489  
490  
491 =head1 AUTHORS
492  
493 Uwe Sarnowski E<lt>uwes at cpan.orgE<gt>
494  
495 Author of the parent modul L<Parse::CSV> : Adam Kennedy
496  
497  
498 =head1 SEE ALSO
499  
500 L<Parse::CSV>, L<Text::CSV_XS>
501  
502 =head1 COPYRIGHT
503  
504 Copyright 2011 Uwe Sarnowski
505  
506 This program is free software; you can redistribute
507 it and/or modify it under the same terms as Perl itself.
508  
509 The full text of the license can be found in the
510 LICENSE file included with this module.
511  
512 =cut
↑ Summary