| 1 | # | ||
| 2 | # $Id$ | ||
| 3 | # | ||
| 4 | # file::csv Brik | ||
| 5 | # | ||
| 6 | package Metabrik::File::Csv; | ||
| 7 | 2 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 2 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | |||
| 10 | 2 | ▶ | use base qw(Metabrik); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | |||
| 12 | sub brik_properties { | ||
| 13 | return { | ||
| 14 | revision => '$Revision$', | ||
| 15 | tags => [ qw(unstable) ], | ||
| 16 | author => 'GomoR <GomoR[at]metabrik.org>', | ||
| 17 | license => 'http://opensource.org/licenses/BSD-3-Clause', | ||
| 18 | attributes => { | ||
| 19 | input => [ qw(file) ], | ||
| 20 | output => [ qw(file) ], | ||
| 21 | first_line_is_header => [ qw(0|1) ], | ||
| 22 | separator => [ qw(character) ], | ||
| 23 | escape => [ qw(character) ], | ||
| 24 | header => [ qw($column_header_list) ], | ||
| 25 | encoding => [ qw(utf8|ascii) ], | ||
| 26 | overwrite => [ qw(0|1) ], | ||
| 27 | append => [ qw(0|1) ], | ||
| 28 | write_header => [ qw(0|1) ], | ||
| 29 | use_quoting => [ qw(0|1) ], | ||
| 30 | use_locking => [ qw(0|1) ], | ||
| 31 | unbuffered => [ qw(0|1) ], | ||
| 32 | encoded_fields => [ qw(fields) ], | ||
| 33 | object_fields => [ qw(fields) ], | ||
| 34 | _csv => [ qw(INTERNAL) ], | ||
| 35 | _fd => [ qw(INTERNAL) ], | ||
| 36 | _sb => [ qw(INTERNAL) ], | ||
| 37 | _sc => [ qw(INTERNAL) ], | ||
| 38 | }, | ||
| 39 | attributes_default => { | ||
| 40 | first_line_is_header => 1, | ||
| 41 | separator => ',', | ||
| 42 | escape => '"', | ||
| 43 | encoding => 'utf8', | ||
| 44 | overwrite => 0, | ||
| 45 | append => 1, | ||
| 46 | write_header => 1, | ||
| 47 | use_quoting => 0, | ||
| 48 | use_locking => 0, | ||
| 49 | unbuffered => 0, | ||
| 50 | }, | ||
| 51 | commands => { | ||
| 52 | read => [ qw(input_file|OPTIONAL) ], | ||
| 53 | write => [ qw(csv_struct output_file|OPTIONAL) ], | ||
| 54 | get_column_values => [ qw($data column_name|column_int) ], | ||
| 55 | read_next => [ qw(input_file|OPTIONAL) ], | ||
| 56 | }, | ||
| 57 | require_modules => { | ||
| 58 | 'Data::Dump' => [ ], | ||
| 59 | 'Text::CSV_XS' => [ ], | ||
| 60 | 'Metabrik::File::Read' => [ ], | ||
| 61 | 'Metabrik::File::Write' => [ ], | ||
| 62 | 'Metabrik::String::Base64' => [ ], | ||
| 63 | 'Metabrik::String::Compress' => [ ], | ||
| 64 | }, | ||
| 65 | }; | ||
| 66 | } | ||
| 67 | |||
| 68 | sub brik_init { | ||
| 69 | my $self = shift; | ||
| 70 | |||
| 71 | my $sb = Metabrik::String::Base64->new_from_brik_init($self) or return; | ||
| 72 | my $sc = Metabrik::String::Compress->new_from_brik_init($self) or return; | ||
| 73 | $self->_sb($sb); | ||
| 74 | $self->_sc($sc); | ||
| 75 | |||
| 76 | return $self->SUPER::brik_init; | ||
| 77 | } | ||
| 78 | |||
| 79 | sub read { | ||
| 80 | my $self = shift; | ||
| 81 | my ($input) = @_; | ||
| 82 | |||
| 83 | $input ||= $self->input; | ||
| 84 | $self->brik_help_run_undef_arg('read', $input) or return; | ||
| 85 | $self->brik_help_run_file_not_found('read', $input) or return; | ||
| 86 | |||
| 87 | my $csv = Text::CSV_XS->new({ | ||
| 88 | binary => 1, | ||
| 89 | sep_char => $self->separator, | ||
| 90 | allow_loose_quotes => 1, | ||
| 91 | allow_loose_escapes => 1, | ||
| 92 | escape_char => $self->escape, | ||
| 93 | }) or return $self->log->error('read: Text::CSV_XS new failed'); | ||
| 94 | |||
| 95 | my $fr = Metabrik::File::Read->new_from_brik_init($self) or return; | ||
| 96 | $fr->encoding($self->encoding); | ||
| 97 | my $fd = $fr->open($input) or return; | ||
| 98 | |||
| 99 | # When some content is too complex to be stored as a standard CSV cell, | ||
| 100 | # we should encode it as base64. | ||
| 101 | my $sb = $self->_sb; | ||
| 102 | my $sc = $self->_sc; | ||
| 103 | my $encoded_fields = $self->encoded_fields; | ||
| 104 | if (defined($encoded_fields)) { | ||
| 105 | my $str = join(',', @$encoded_fields); | ||
| 106 | $encoded_fields = { map { $_ => 1 } @$encoded_fields }; | ||
| 107 | $self->log->debug("read: will decode field(s) [$str] in encoded format"); | ||
| 108 | } | ||
| 109 | my $object_fields = $self->object_fields; | ||
| 110 | if (defined($object_fields)) { | ||
| 111 | my $str = join(',', @$object_fields); | ||
| 112 | $object_fields = { map { $_ => 1 } @$object_fields }; | ||
| 113 | $self->log->debug("read: will decode field(s) [$str] in object format"); | ||
| 114 | } | ||
| 115 | |||
| 116 | my $object_re = qr/^OBJECT:(.*)$/; | ||
| 117 | my $base64_re = qr/^BASE64:(.*)$/; # Keep for backward compat. | ||
| 118 | |||
| 119 | my $sep = $self->separator; | ||
| 120 | my $headers; | ||
| 121 | my $count; | ||
| 122 | my $first_line = 1; | ||
| 123 | my @rows = (); | ||
| 124 | while (my $row = $csv->getline($fd)) { | ||
| 125 | # The CSV file has a header, we output an array of hashes | ||
| 126 | if ($self->first_line_is_header) { | ||
| 127 | if ($first_line) { # This is first line | ||
| 128 | $headers = $row; | ||
| 129 | $count = scalar @$row - 1; | ||
| 130 | $first_line = 0; | ||
| 131 | $self->header($headers); | ||
| 132 | next; | ||
| 133 | } | ||
| 134 | |||
| 135 | my $h; | ||
| 136 | # We have to decode some fields | ||
| 137 | if ($encoded_fields || $object_fields) { | ||
| 138 | for (0..$count) { | ||
| 139 | my $k = $headers->[$_]; | ||
| 140 | my $v = $row->[$_]; | ||
| 141 | next unless defined($v); | ||
| 142 | # Decode only if it has been asked and the value is not empty. | ||
| 143 | # Decode the encode format | ||
| 144 | if ($encoded_fields && exists($encoded_fields->{$k}) && length($v)) { | ||
| 145 | my $decoded = $sb->decode($v); | ||
| 146 | if (! defined($decoded)) { | ||
| 147 | $self->log->error("read: decode encoded format failed, ". | ||
| 148 | "skipping data with length [".length($v)."]"); | ||
| 149 | next; | ||
| 150 | } | ||
| 151 | my $gunzipped = $sc->gunzip($decoded); | ||
| 152 | if (! defined($gunzipped)) { | ||
| 153 | $self->log->error("read: gunzip failed, skipping ". | ||
| 154 | "decoded data with length [".length($decoded)."]"); | ||
| 155 | next; | ||
| 156 | } | ||
| 157 | $v = $$gunzipped; | ||
| 158 | } | ||
| 159 | # Decode the object format | ||
| 160 | if ($object_fields && exists($object_fields->{$k}) && length($v) | ||
| 161 | && ($v =~ $object_re || $v =~ $base64_re)) { | ||
| 162 | my $decoded = $sb->decode($1); | ||
| 163 | if (! defined($decoded)) { | ||
| 164 | $self->log->error("read: decode object format failed, ". | ||
| 165 | "skipping data with length [".length($v)."]"); | ||
| 166 | next; | ||
| 167 | } | ||
| 168 | $v = eval($decoded); | ||
| 169 | } | ||
| 170 | $h->{$k} = $v; | ||
| 171 | } | ||
| 172 | } | ||
| 173 | # Or not. | ||
| 174 | else { | ||
| 175 | for (0..$count) { | ||
| 176 | $h->{$headers->[$_]} = $row->[$_]; | ||
| 177 | } | ||
| 178 | } | ||
| 179 | push @rows, $h; | ||
| 180 | } | ||
| 181 | # The CSV has no header, we output an array of arrays | ||
| 182 | else { | ||
| 183 | push @rows, $row; | ||
| 184 | } | ||
| 185 | } | ||
| 186 | |||
| 187 | if (! $csv->eof) { | ||
| 188 | my $error_str = "".$csv->error_diag(); | ||
| 189 | $self->log->error("read: incomplete: error [$error_str]"); | ||
| 190 | return \@rows; | ||
| 191 | } | ||
| 192 | |||
| 193 | $fr->close; | ||
| 194 | |||
| 195 | return \@rows; | ||
| 196 | } | ||
| 197 | |||
| 198 | # | ||
| 199 | # We only handle ARRAY of HASHes format (aoh) for writing | ||
| 200 | # | ||
| 201 | sub write { | ||
| 202 | my $self = shift; | ||
| 203 | my ($csv_struct, $output) = @_; | ||
| 204 | |||
| 205 | $output ||= $self->output; | ||
| 206 | $self->brik_help_run_undef_arg('write', $csv_struct) or return; | ||
| 207 | $self->brik_help_run_invalid_arg('write', $csv_struct, 'ARRAY') or return; | ||
| 208 | $self->brik_help_run_empty_array_arg('write', $csv_struct, 'ARRAY') or return; | ||
| 209 | $self->brik_help_run_undef_arg('write', $output) or return; | ||
| 210 | |||
| 211 | if (ref($csv_struct->[0]) ne 'HASH') { | ||
| 212 | return $self->log->error("write: csv structure does not contain HASHes"); | ||
| 213 | } | ||
| 214 | |||
| 215 | my $fw = Metabrik::File::Write->new_from_brik_init($self) or return; | ||
| 216 | $fw->output($output); | ||
| 217 | $fw->encoding($self->encoding); | ||
| 218 | $fw->overwrite($self->overwrite); | ||
| 219 | $fw->append($self->append); | ||
| 220 | $fw->use_locking($self->use_locking); | ||
| 221 | $fw->unbuffered($self->unbuffered); | ||
| 222 | |||
| 223 | # When some content is too complex to be stored as a standard CSV cell, | ||
| 224 | # we should encode it as base64. | ||
| 225 | my $sb = $self->_sb; | ||
| 226 | my $sc = $self->_sc; | ||
| 227 | my $encoded_fields = $self->encoded_fields; | ||
| 228 | if (defined($encoded_fields)) { | ||
| 229 | my $str = join(',', @$encoded_fields); | ||
| 230 | $encoded_fields = { map { $_ => 1 } @$encoded_fields }; | ||
| 231 | $self->log->debug("write: will encode field(s) [$str] in encoded format"); | ||
| 232 | } | ||
| 233 | my $object_fields = $self->object_fields; | ||
| 234 | if (defined($object_fields)) { | ||
| 235 | my $str = join(',', @$object_fields); | ||
| 236 | $object_fields = { map { $_ => 1 } @$object_fields }; | ||
| 237 | $self->log->debug("write: will encode field(s) [$str] in object format"); | ||
| 238 | } | ||
| 239 | |||
| 240 | # | ||
| 241 | # Set header ordering | ||
| 242 | # | ||
| 243 | my %order = (); | ||
| 244 | my @header = (); | ||
| 245 | # Order headers either by using user provided one or our own default ordering. | ||
| 246 | if ($self->header) { | ||
| 247 | @header = @{$self->header}; | ||
| 248 | my $idx = 0; | ||
| 249 | for my $k (@header) { | ||
| 250 | $order{$k} = $idx; | ||
| 251 | $idx++; | ||
| 252 | } | ||
| 253 | } | ||
| 254 | # If user didn't provide her own header, we use first element from struct. | ||
| 255 | else { | ||
| 256 | my $first = $csv_struct->[0]; | ||
| 257 | @header = sort { $a cmp $b } keys %$first; | ||
| 258 | my $idx = 0; | ||
| 259 | for my $k (@header) { | ||
| 260 | $order{$k} = $idx; | ||
| 261 | $idx++; | ||
| 262 | } | ||
| 263 | } | ||
| 264 | |||
| 265 | my $header_count = @header; | ||
| 266 | |||
| 267 | my $is_new_file = (! -f $output); | ||
| 268 | my $fd = $fw->open or return; | ||
| 269 | |||
| 270 | my $written = ''; | ||
| 271 | |||
| 272 | # Write header if this is a new file and user asked for it. | ||
| 273 | if ($self->write_header && ($is_new_file || $self->overwrite)) { | ||
| 274 | my $data = join($self->separator, @header)."\n"; | ||
| 275 | my $r = $fw->write($data); | ||
| 276 | if (! defined($r)) { | ||
| 277 | return; | ||
| 278 | } | ||
| 279 | $written .= $data; | ||
| 280 | } | ||
| 281 | |||
| 282 | my $separator = $self->separator; | ||
| 283 | my $escape = $self->escape; | ||
| 284 | |||
| 285 | local $Data::Dump::INDENT = ""; # No indentation shorten length | ||
| 286 | local $Data::Dump::TRY_BASE64 = 0; # Never encode in base64 | ||
| 287 | |||
| 288 | # Write the structure to file. | ||
| 289 | for my $this (@$csv_struct) { | ||
| 290 | my @fields = (); | ||
| 291 | # We have to decode some fields | ||
| 292 | if ($encoded_fields || $object_fields) { | ||
| 293 | for my $key (keys %$this) { | ||
| 294 | # We may have some unwanted data in this HASH, we skip it. | ||
| 295 | next if (! defined($order{$key})); | ||
| 296 | my $k = $key; | ||
| 297 | my $v = $this->{$key}; | ||
| 298 | next unless defined($v); | ||
| 299 | # Encode only if it has been asked and the value is not empty. | ||
| 300 | if ($encoded_fields && exists($encoded_fields->{$k}) && length($v)) { | ||
| 301 | # Gzip to handle UTF-like encodings, cause Base64 does not like that. | ||
| 302 | my $gzipped = $sc->gzip($v); | ||
| 303 | if (! defined($gzipped)) { | ||
| 304 | $self->log->error("write: gzip failed, skipping"); | ||
| 305 | next; | ||
| 306 | } | ||
| 307 | $v = $sb->encode($$gzipped); | ||
| 308 | if (! defined($v)) { | ||
| 309 | $self->log->error("write: encode in encoded format failed, skipping"); | ||
| 310 | next; | ||
| 311 | } | ||
| 312 | } | ||
| 313 | # Encode only if it has been asked and the value is not empty. | ||
| 314 | if ($object_fields && exists($object_fields->{$k}) && length($v)) { | ||
| 315 | # Encode ARRAYs and HASHes only if they are not empty. | ||
| 316 | # Do not encode simple strings. | ||
| 317 | if (ref($v) eq 'ARRAY' && @$v > 0 | ||
| 318 | || ref($v) eq 'HASH' && keys %$v > 0) { | ||
| 319 | $v = Data::Dump::dump($v); $v =~ s{\n}{}g; | ||
| 320 | $v = 'OBJECT:'.$sb->encode($v); | ||
| 321 | if (! defined($v)) { | ||
| 322 | $self->log->error("write: encode in object format failed, skipping"); | ||
| 323 | next; | ||
| 324 | } | ||
| 325 | } | ||
| 326 | # If this is a simple string, we do not encode at all. | ||
| 327 | elsif (ref($v) eq '' && length($v)) { | ||
| 328 | } | ||
| 329 | # And for empty objects, we set them to empty string. | ||
| 330 | else { | ||
| 331 | $v = ""; | ||
| 332 | } | ||
| 333 | } | ||
| 334 | $fields[$order{$key}] = $v; | ||
| 335 | } | ||
| 336 | } | ||
| 337 | # Or not. | ||
| 338 | else { | ||
| 339 | for my $key (keys %$this) { | ||
| 340 | # We may have some unwanted data in this HASH, we skip it. | ||
| 341 | next if (! defined($order{$key})); | ||
| 342 | $fields[$order{$key}] = $this->{$key}; | ||
| 343 | } | ||
| 344 | } | ||
| 345 | |||
| 346 | @fields = map { defined($_) ? $_ : '' } @fields; | ||
| 347 | |||
| 348 | # If this entry has less fields than the header, we add null entries. | ||
| 349 | my $field_count = @fields; | ||
| 350 | if ($field_count < $header_count) { | ||
| 351 | my $diff = $header_count - $field_count; | ||
| 352 | for (1..$diff) { | ||
| 353 | push @fields, ''; | ||
| 354 | } | ||
| 355 | } | ||
| 356 | |||
| 357 | if ($self->use_quoting) { | ||
| 358 | for (@fields) { | ||
| 359 | s/"/${escape}"/g; | ||
| 360 | $_ = '"'.$_.'"'; | ||
| 361 | } | ||
| 362 | } | ||
| 363 | |||
| 364 | my $data = join($separator, @fields)."\n"; | ||
| 365 | |||
| 366 | my $r = $fw->write($data); | ||
| 367 | if (! defined($r)) { | ||
| 368 | next; | ||
| 369 | } | ||
| 370 | |||
| 371 | $written .= $data; | ||
| 372 | } | ||
| 373 | |||
| 374 | $fw->close; | ||
| 375 | |||
| 376 | if (! length($written)) { | ||
| 377 | return $self->log->error("write: nothing to write"); | ||
| 378 | } | ||
| 379 | |||
| 380 | return $written; | ||
| 381 | } | ||
| 382 | |||
| 383 | sub get_column_values { | ||
| 384 | my $self = shift; | ||
| 385 | my ($data, $column) = @_; | ||
| 386 | |||
| 387 | $self->brik_help_run_undef_arg('get_column_values', $data) or return; | ||
| 388 | $self->brik_help_run_invalid_arg('get_column_values', $data, 'ARRAY') or return; | ||
| 389 | $self->brik_help_run_undef_arg('get_column_values', $column) or return; | ||
| 390 | |||
| 391 | my @results = (); | ||
| 392 | # CSV structure is an ARRAYREF of HASHREFs | ||
| 393 | if ($self->first_line_is_header) { | ||
| 394 | if (@{$self->header} == 0) { | ||
| 395 | return $self->log->error("get_column_values: no CSV header found"); | ||
| 396 | } | ||
| 397 | |||
| 398 | for my $row (@$data) { | ||
| 399 | if (ref($row) ne 'HASH') { | ||
| 400 | $self->log->warning("get_column_values: row is not a HASHREF"); | ||
| 401 | next; | ||
| 402 | } | ||
| 403 | if (exists($row->{$column})) { | ||
| 404 | push @results, $row->{$column}; | ||
| 405 | } | ||
| 406 | } | ||
| 407 | } | ||
| 408 | # CSV structure is an ARRAYREF of ARRAYREFs | ||
| 409 | elsif ($column =~ m{^\d+$}) { | ||
| 410 | for my $row (@$data) { | ||
| 411 | if (ref($row) ne 'ARRAY') { | ||
| 412 | $self->log->warning("get_column_values: row is not an ARRAYREF"); | ||
| 413 | next; | ||
| 414 | } | ||
| 415 | if (exists($row->[$column])) { | ||
| 416 | push @results, $row->[$column]; | ||
| 417 | } | ||
| 418 | } | ||
| 419 | } | ||
| 420 | |||
| 421 | return \@results; | ||
| 422 | } | ||
| 423 | |||
| 424 | sub read_next { | ||
| 425 | my $self = shift; | ||
| 426 | my ($input) = @_; | ||
| 427 | |||
| 428 | $input ||= $self->input; | ||
| 429 | $self->brik_help_run_undef_arg('read_next', $input) or return; | ||
| 430 | $self->brik_help_run_file_not_found('read_next', $input) or return; | ||
| 431 | |||
| 432 | my $csv = $self->_csv; | ||
| 433 | my $fd = $self->_fd; | ||
| 434 | if (! defined($csv)) { | ||
| 435 | $self->log->debug('read_next: first call, create _csv'); | ||
| 436 | $csv = Text::CSV_XS->new({ | ||
| 437 | binary => 1, | ||
| 438 | sep_char => $self->separator, | ||
| 439 | allow_loose_quotes => 1, | ||
| 440 | allow_loose_escapes => 1, | ||
| 441 | escape_char => $self->escape, | ||
| 442 | }) or return $self->log->error('read_next: Text::CSV_XS new failed'); | ||
| 443 | $self->_csv($csv); | ||
| 444 | |||
| 445 | my $fr = Metabrik::File::Read->new_from_brik_init($self) or return; | ||
| 446 | $fr->encoding($self->encoding); | ||
| 447 | $fd = $fr->open($input) or return; | ||
| 448 | $self->_fd($fd); | ||
| 449 | |||
| 450 | if ($self->first_line_is_header) { | ||
| 451 | my $header = $csv->getline($fd); | ||
| 452 | $self->header($header); | ||
| 453 | } | ||
| 454 | } | ||
| 455 | |||
| 456 | # When some content is too complex to be stored as a standard CSV cell, | ||
| 457 | # we should encode it as base64. | ||
| 458 | my $sb = $self->_sb; | ||
| 459 | my $sc = $self->_sc; | ||
| 460 | my $encoded_fields = $self->encoded_fields; | ||
| 461 | if (defined($encoded_fields)) { | ||
| 462 | my $str = join(',', @$encoded_fields); | ||
| 463 | $encoded_fields = { map { $_ => 1 } @$encoded_fields }; | ||
| 464 | $self->log->debug("read_next: will decode field(s) [$str] in base64"); | ||
| 465 | } | ||
| 466 | my $object_fields = $self->object_fields; | ||
| 467 | if (defined($object_fields)) { | ||
| 468 | my $str = join(',', @$object_fields); | ||
| 469 | $object_fields = { map { $_ => 1 } @$object_fields }; | ||
| 470 | $self->log->debug("read_next: will decode field(s) [$str] in object format"); | ||
| 471 | } | ||
| 472 | |||
| 473 | my $object_re = qr/^OBJECT:(.*)$/; | ||
| 474 | my $base64_re = qr/^BASE64:(.*)$/; # Keep for backward compat. | ||
| 475 | |||
| 476 | my $row = $csv->getline($fd); | ||
| 477 | |||
| 478 | # If a header is given as an Attribute, we use it to return a HASH | ||
| 479 | my $header = $self->header; | ||
| 480 | if (defined($header)) { | ||
| 481 | my $h = {}; | ||
| 482 | my $i = 0; | ||
| 483 | # We have to decode some fields | ||
| 484 | if ($encoded_fields || $object_fields) { | ||
| 485 | for (@$header) { | ||
| 486 | my $k = $_; | ||
| 487 | my $v = $row->[$i++]; | ||
| 488 | next unless defined($v); | ||
| 489 | # Decode only if it has been asked and the value is not empty. | ||
| 490 | # Decode the encode format | ||
| 491 | if ($encoded_fields && exists($encoded_fields->{$k}) && length($v)) { | ||
| 492 | my $decoded = $sb->decode($v); | ||
| 493 | if (! defined($decoded)) { | ||
| 494 | $self->log->error("read_next: decode failed, skipping data with ". | ||
| 495 | "with length [".length($v)."]"); | ||
| 496 | next; | ||
| 497 | } | ||
| 498 | my $gunzipped = $sc->gunzip($decoded); | ||
| 499 | if (! defined($gunzipped)) { | ||
| 500 | $self->log->error("read_next: gunzip failed, skipping ". | ||
| 501 | "decoded data with length [".length($decoded)."]"); | ||
| 502 | next; | ||
| 503 | } | ||
| 504 | $v = $$gunzipped; | ||
| 505 | } | ||
| 506 | # Decode the object format | ||
| 507 | if ($object_fields && exists($object_fields->{$k}) && length($v) | ||
| 508 | && ($v =~ $object_re || $v =~ $base64_re)) { | ||
| 509 | my $decoded = $sb->decode($1); | ||
| 510 | if (! defined($decoded)) { | ||
| 511 | $self->log->error("read_next: decode object format failed, ". | ||
| 512 | "skipping data with length [".length($v)."]"); | ||
| 513 | next; | ||
| 514 | } | ||
| 515 | $v = eval($decoded); | ||
| 516 | } | ||
| 517 | $h->{$k} = $v; | ||
| 518 | } | ||
| 519 | } | ||
| 520 | # Or not. | ||
| 521 | else { | ||
| 522 | for (@$header) { | ||
| 523 | $h->{$_} = $row->[$i++]; | ||
| 524 | } | ||
| 525 | } | ||
| 526 | $row = $h; | ||
| 527 | } | ||
| 528 | |||
| 529 | if ($csv->eof) { | ||
| 530 | $self->log->debug('read_next: eof reached'); | ||
| 531 | $self->_fd(undef); | ||
| 532 | $self->_csv(undef); | ||
| 533 | return 0; | ||
| 534 | } | ||
| 535 | |||
| 536 | return $row; | ||
| 537 | } | ||
| 538 | |||
| 539 | 1; | ||
| 540 | |||
| 541 | __END__ | ||