annotate lib/csv.pm @ 1:52798007c6b2 draft default tip

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