comparison lib/csv.pm @ 0:be582bcd6585 draft

Master branch Updating - - Fxx
author fgiacomoni
date Thu, 04 Oct 2018 10:37:14 -0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:be582bcd6585
1 package lib::csv ;
2
3 use strict;
4 use warnings ;
5 use Exporter ;
6 use Carp ;
7
8 use Text::CSV ;
9
10 use Data::Dumper ;
11
12 use vars qw($VERSION @ISA @EXPORT %EXPORT_TAGS);
13
14 our $VERSION = "1.0";
15 our @ISA = qw(Exporter);
16 our @EXPORT = qw( get_csv_object get_value_from_csv );
17 our %EXPORT_TAGS = ( ALL => [qw( get_csv_object get_value_from_csv )] );
18
19 =head1 NAME
20
21 My::Module - An example module
22
23 =head1 SYNOPSIS
24
25 use My::Module;
26 my $object = My::Module->new();
27 print $object->as_string;
28
29 =head1 DESCRIPTION
30
31 This module does not really exist, it
32 was made for the sole purpose of
33 demonstrating how POD works.
34
35 =head1 METHODS
36
37 Methods are :
38
39 =head2 METHOD new
40
41 ## Description : new
42 ## Input : $self
43 ## Ouput : bless $self ;
44 ## Usage : new() ;
45
46 =cut
47
48 sub new {
49 ## Variables
50 my $self={};
51 bless($self) ;
52 return $self ;
53 }
54 ### END of SUB
55
56 =head2 METHOD get_csv_object
57
58 ## Description : builds a csv object and etablishes format
59 ## Input : $separator
60 ## Output : $csv
61 ## Usage : my ( $csv ) = get_csv_object( $separator ) ;
62
63 =cut
64 ## START of SUB
65 sub get_csv_object {
66 ## Retrieve Values
67 my $self = shift ;
68 my ( $separator ) = @_ ;
69
70 # my $csv = Text::CSV->new({'sep_char' => "$separator"});
71 my $csv = Text::CSV->new ( {'sep_char' => "$separator", binary => 1 } ) # should set binary attribute.
72 or die "Cannot use CSV: ".Text::CSV->error_diag ();
73
74 return($csv) ;
75 }
76 ## END of SUB
77
78 =head2 METHOD get_value_from_csv
79
80 ## Description : extract a targeted column in a csv file
81 ## Input : $csv, $file, $column, $is_header
82 ## Output : $value
83 ## Usage : my ( $value ) = get_value_from_csv( $csv, $file, $column, $is_header ) ;
84
85 =cut
86 ## START of SUB
87 sub get_value_from_csv {
88 ## Retrieve Values
89 my $self = shift ;
90 my ( $csv, $file, $column, $is_header ) = @_ ;
91
92 my @value = () ;
93
94 ## Adapte the number of the colunm : (nb of column to position in array)
95 $column = $column - 1 ;
96
97 open (CSV, "<", $file) or die $! ;
98
99 my $line = 0 ;
100
101 while (<CSV>) {
102 $line++ ;
103 chomp $_ ;
104 # file has a header
105 if ( defined $is_header ) { if ($line == 1) { next ; } }
106 # parsing the targeted column
107 if ( $csv->parse($_) ) {
108 my @columns = $csv->fields();
109 push ( @value, $columns[$column] ) ;
110 }
111 else {
112 my $err = $csv->error_input;
113 die "Failed to parse line: $err";
114 }
115 }
116 close CSV;
117 return(\@value) ;
118 }
119 ## END of SUB
120
121 =head2 METHOD get_value_from_csv_multi_header
122
123 ## Description : extract a targeted column in a csv file
124 ## Input : $csv, $file, $column, $is_header, $nb_header
125 ## Output : $value
126 ## Usage : my ( $value ) = get_value_from_csv_multi_header( $csv, $file, $column, $is_header, $nb_header ) ;
127
128 =cut
129 ## START of SUB
130 sub get_value_from_csv_multi_header {
131 ## Retrieve Values
132 my $self = shift ;
133 my ( $csv, $file, $column, $is_header, $nb_header ) = @_ ;
134
135 my @value = () ;
136
137 ## Adapte the number of the colunm : (nb of column to position in array)
138 $column = $column - 1 ;
139
140 open (CSV, "<", $file) or die $! ;
141
142 my $line = 0 ;
143
144 while (<CSV>) {
145 $line++ ;
146 chomp $_ ;
147 # file has a header
148 if ( defined $is_header and $is_header eq 'yes') { if ($line <= $nb_header) { next ; } }
149 # parsing the targeted column
150 if ( $csv->parse($_) ) {
151 my @columns = $csv->fields();
152 push ( @value, $columns[$column] ) ;
153 }
154 else {
155 my $err = $csv->error_input;
156 die "Failed to parse line: $err";
157 }
158 }
159 close CSV;
160 return(\@value) ;
161 }
162 ## END of SUB
163
164 =head2 METHOD parse_csv_object
165
166 ## Description : parse_all csv object and return a array of rows
167 ## Input : $csv, $file
168 ## Output : $csv_matrix
169 ## Usage : my ( $csv_matrix ) = parse_csv_object( $csv, $file ) ;
170
171 =cut
172 ## START of SUB
173 sub parse_csv_object {
174 ## Retrieve Values
175 my $self = shift ;
176 my ( $csv, $file ) = @_ ;
177
178 my @csv_matrix = () ;
179
180 open my $fh, "<:encoding(utf8)", $$file or die "Can't open csv file $$file: $!";
181
182 while ( my $row = $csv->getline( $fh ) ) {
183 push @csv_matrix, $row;
184 }
185 $csv->eof or $csv->error_diag();
186 close $fh;
187
188 return(\@csv_matrix) ;
189 }
190 ## END of SUB
191
192 =head2 METHOD parse_allcsv_object
193
194 ## Description : parse_all csv object and return a array of rows with or without header
195 ## Input : $csv, $file, $keep_header
196 ## Output : $csv_matrix
197 ## Usage : my ( $csv_matrix ) = parse_csv_object( $csv, $file, $keep_header ) ;
198
199 =cut
200 ## START of SUB
201 sub parse_allcsv_object {
202 ## Retrieve Values
203 my $self = shift ;
204 my ( $csv, $file, $keep_header ) = @_ ;
205
206 my @csv_matrix = () ;
207 my $line = 1 ;
208
209 open my $fh, "<:encoding(utf8)", $$file or die "Can't open csv file $$file: $!";
210
211 while ( my $row = $csv->getline( $fh ) ) {
212 if ( ( $keep_header eq 'n' ) and ($line == 1) ) { }
213 else { push @csv_matrix, $row; }
214 $line ++ ;
215 }
216 my $status = $csv->eof or $csv->error_diag();
217 close $fh;
218
219 return(\@csv_matrix, $status) ;
220 }
221 ## END of SUB
222
223
224 =head2 METHOD write_csv_from_arrays
225
226 ## Description : write a csv file from list of rows
227 ## Input : $csv, $file_name, $rows
228 ## Output : $csv_file
229 ## Usage : my ( $csv_file ) = write_csv_from_arrays( $csv, $file_name, $rows ) ;
230
231 =cut
232 ## START of SUB
233 sub write_csv_from_arrays {
234 ## Retrieve Values
235 my $self = shift ;
236 my ( $csv, $file_name, $rows ) = @_ ;
237
238 my $fh = undef ;
239 $csv->eol ("\n"); ## end-of-line string to add to rows
240 open $fh, ">:encoding(utf8)", "$file_name" or die "$file_name: $!";
241
242 my $status = $csv->print ($fh, $_) for @{$rows};
243 close $fh or die "$file_name: $!";
244
245 return(\$file_name) ;
246 }
247 ## END of SUB
248
249 1 ;
250
251
252 __END__
253
254 =head1 SUPPORT
255
256 You can find documentation for this module with the perldoc command.
257
258 perldoc csv.pm
259
260 =head1 Exports
261
262 =over 4
263
264 =item :ALL is get_csv_object, get_value_from_csv
265
266 =back
267
268 =head1 AUTHOR
269
270 Franck Giacomoni E<lt>franck.giacomoni@clermont.inra.frE<gt>
271
272 =head1 LICENSE
273
274 This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
275
276 =head1 VERSION
277
278 version 1 : 23 / 10 / 2013
279
280 version 2 : ??
281
282 =cut