CSV.pm untested

filter: Statementstmt 0.0% 0 / 153 Branchbran 0.0% 0 / 30 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 14 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 225 CC 16 SCAR 37.1
CC
16
Cov
0%
iterate_each_line
72.0
iterate_each_line_from_string
72.0
data_hash
6.0
CRAP 40.9
1 package File::Convert::CSV;
2  
3 use 5.008007;use strict;use warnings;
4  
5 our $VERSION = '0.03';
6  
7 use IO::Extended qw(:all);
8  
9 use Data::Iter qw(:all);
10  
11 use Carp qw(confess croak cluck carp);
12  
13 use Class::Debugable;
14  
15 use Data::Dump qw(dump);
16  
17 use Path::Class;
18  
19 Class::Maker::class
20 {
21 isa => [qw( Class::Debugable )],
22  
23 public =>
24 {
25 scalar => [qw( file separator has_header raw line skip_pattern skip_callback )],
26  
27 array => [qw( header data_array data_fields data_file )],
28  
29 # hash => [qw( fields )],
30 },
31  
32 default =>
33 {
34 has_header => 1,
35  
36 separator => "\t",
37  
38 skip_pattern => qr/^#/,
39
40 skip_callback => sub {}
41
42 },
43 };
44  
45 sub data_hash
46 {
47 my $this = shift;
48  
49 return unless $this->has_header;
50  
51 return transform_array_to_hash( scalar $this->data_array );
52 }
53  
54 sub preformat_data_file : method
55 {
56 my $this = shift;
57 }
58  
59  
60 sub iterate_each_line
61 {
62 my $this = shift;
63  
64 my $file = Path::Class::File->new( $this->file )->absolute;
65  
66 my $callback_line = shift || sub { } ;
67  
68 my @args = @_;
69  
70 println STDERR __PACKAGE__, " iterate_each_line args = ", join( ', ', @args );
71  
72 my $separator = $this->separator;
73  
74 my $skip_pattern = $this->skip_pattern;
75  
76 $this->d_warn( "\n\nImporting from file %S...", $file );
77
78 open( FILE, $file ) or die "$! $file";
79
80 my $cnt=0;
81  
82 my $line=0;
83
84 my $limit = -1;
85
86 my @slurp = <FILE>;
87  
88 $this->d_warn( "Input file %S has %d lines", $this->file, scalar @slurp );
89  
90 close( FILE );
91  
92 $this->data_file( \@slurp );
93  
94 $this->preformat_data_file();
95  
96 $this->d_warn_above( 10, "DATA_FILE %s", dump( $this->data_file ) );
97  
98 for ( @{ $this->data_file } )
99 {
100 $this->d_warn_above( 10, "LINE: %S", $_ );
101  
102 s/\r$//gi;
103
104 chomp;
105  
106 $line++;
107  
108 if( /$skip_pattern/ )
109 {
110 $this->d_warn( "skipping line %d: %s", $line, $_ );
111  
112 $this->skip_callback->();
113  
114 next;
115 }
116  
117 $cnt++;
118  
119 $this->raw = $_;
120
121 $this->line = $line;
122
123 if( $cnt == 1 && $this->has_header )
124 {
125 if( $this->has_header )
126 {
127 $this->header( [ split /$separator/ ] );
128 }
129
130 $this->d_warn( "csv imported file header (rows) %s", dump( $this->header ) );
131 }
132 else
133 {
134  
135 if ( $limit > 0 )
136 {
137 last if $cnt >= $limit;
138 }
139
140 $this->data_fields( [ split /$separator/ ] );
141
142 $this->data_array( [] );
143  
144 if( $this->has_header )
145 {
146 for ( iter scalar $this->header )
147 {
148 push @{ $this->data_array }, VALUE(), $this->data_fields->[ COUNTER() ];
149 }
150 }
151
152 $this->d_warn( "DATA_HASH %s", dump( $this->data_hash ) );
153  
154 $this->d_warn( "DATA_ARRAY %s", dump( $this->data_array ) );
155  
156 $this->d_warn( "DATA_FIELDS %s", dump( $this->data_fields ) );
157  
158 $callback_line->( $this, @args );
159
160 if ( 0 == $cnt % 200 )
161 {
162 $this->d_print( "Processing line nr $line\n" );
163 }
164 }
165 }
166  
167 $this->d_warn( "finished." );
168
169 close( FILE );
170 }
171  
172 sub iterate_each_line_from_string
173 {
174 my $this = shift;
175  
176 my $string = shift; # string
177  
178 my $callback_line = shift || sub { } ;
179  
180 my @args = @_;
181  
182 println STDERR __PACKAGE__, " iterate_each_line args = ", join( ', ', @args );
183  
184 my $separator = $this->separator;
185  
186 my $skip_pattern = $this->skip_pattern;
187  
188 $this->d_warn( "\n\nImporting from string of %d bytes...", length $string );
189
190 my $cnt=0;
191  
192 my $line=0;
193
194 my $limit = -1;
195
196 my @slurp = split( /\n/, $string );
197  
198 $this->d_warn( "Input file %S has %d lines", $this->file, scalar @slurp );
199  
200 close( FILE );
201  
202 $this->data_file( \@slurp );
203  
204 $this->preformat_data_file();
205  
206 $this->d_warn_above( 10, "DATA_FILE %s", dump( $this->data_file ) );
207  
208 for ( @{ $this->data_file } )
209 {
210 $this->d_warn_above( 10, "LINE: %S", $_ );
211  
212 s/\r$//gi;
213
214 chomp;
215  
216 $line++;
217  
218 if( /$skip_pattern/ )
219 {
220 $this->d_warn( "skipping line %d: %s", $line, $_ );
221  
222 $this->skip_callback->();
223  
224 next;
225 }
226  
227 $cnt++;
228  
229 $this->raw = $_;
230
231 $this->line = $line;
232
233 if( $cnt == 1 && $this->has_header )
234 {
235 if( $this->has_header )
236 {
237 $this->header( [ split /$separator/ ] );
238 }
239
240 $this->d_warn( "csv imported file header (rows) %s", dump( $this->header ) );
241 }
242 else
243 {
244  
245 if ( $limit > 0 )
246 {
247 last if $cnt >= $limit;
248 }
249
250 $this->data_fields( [ split /$separator/ ] );
251
252 $this->data_array( [] );
253  
254 if( $this->has_header )
255 {
256 for ( iter scalar $this->header )
257 {
258 push @{ $this->data_array }, VALUE(), $this->data_fields->[ COUNTER() ];
259 }
260 }
261
262 $this->d_warn( "DATA_HASH %s", dump( $this->data_hash ) );
263  
264 $this->d_warn( "DATA_ARRAY %s", dump( $this->data_array ) );
265  
266 $this->d_warn( "DATA_FIELDS %s", dump( $this->data_fields ) );
267  
268 $callback_line->( $this, @args );
269
270 if ( 0 == $cnt % 200 )
271 {
272 $this->d_print( "Processing line nr $line\n" );
273 }
274 }
275 }
276  
277 $this->d_warn( "finished." );
278 }
279  
280 sub dot_to_comma_for_excel
281 {
282 my $this = shift;
283  
284 for( @_ )
285 {
286 $_ = $_.'';
287
288 $_ =~ s/\./,/;
289 }
290
291 return @_;
292 }
293  
294 1;
295  
296 __END__