From 202844a12433fe5234126e4f8c4b542c43559107 Mon Sep 17 00:00:00 2001 From: Bernd Blessmann Date: Wed, 19 Sep 2012 12:43:20 +0200 Subject: [PATCH] Auf leere Header bei Multiplex-Daten testen und ... Kosmetik und Code-Vereinfachung --- SL/Helper/Csv.pm | 38 ++++++++++--------------------------- SL/Helper/Csv/Dispatcher.pm | 10 +++++----- 2 files changed, 15 insertions(+), 33 deletions(-) diff --git a/SL/Helper/Csv.pm b/SL/Helper/Csv.pm index 4a8bc752e..be2e87135 100644 --- a/SL/Helper/Csv.pm +++ b/SL/Helper/Csv.pm @@ -7,6 +7,7 @@ use version 0.77; use Carp; use IO::File; use Params::Validate qw(:all); +use List::MoreUtils qw(all); use Text::CSV_XS; use Rose::Object::MakeMethods::Generic scalar => [ qw( file encoding sep_char quote_char escape_char header profile @@ -108,21 +109,15 @@ sub _check_multiplexed { if ($self->profile) { my @profile = @{ $self->profile }; if (scalar @profile > 1) { - my $info_ok = 1; # Each profile needs a class and a row_ident - foreach my $p (@profile) { - if ( !defined $p->{class} || !defined $p->{row_ident} ) { - $info_ok = 0; - last; - } - } + my $info_ok = all { defined $_->{class} && defined $_->{row_ident} } @profile; # If header is given, there need to be a header for each profile + # and no empty headers. if ($info_ok && $self->header) { my @header = @{ $self->header }; - if (scalar @profile != scalar @header) { - $info_ok = 0; - } + $info_ok = $info_ok && scalar @profile == scalar @header; + $info_ok = $info_ok && all { scalar @$_ > 0} @header; } $self->is_multiplexed($info_ok); return $info_ok; @@ -158,26 +153,14 @@ sub _check_header { # data with a discouraged but valid byte order mark # if not removed, the first header field will not be recognized if ($header) { - foreach my $h (@{ $header }) { - if ($h && $h->[0] && $self->encoding =~ /utf-?8/i) { - $h->[0] =~ s/^\x{FEFF}//; - } + my $h = $header->[0]; + if ($h && $h->[0] && $self->encoding =~ /utf-?8/i) { + $h->[0] =~ s/^\x{FEFF}//; } } # check, if all header fields are parsed well - my $all_ok = 1; - if ($header) { - foreach my $h (@{ $header }) { - if (!$h) { - $all_ok = 0; - last; - } - } - } else { - $all_ok = 0; - } - return unless $all_ok; + return unless $header && all { $_ } @$header; # Special case: human stupidity # people insist that case sensitivity doesn't exist and try to enter all @@ -207,7 +190,6 @@ sub _parse_data { my $row = $self->_csv->getline($self->_io); if ($row) { my $header = $self->_header_by_row($row); - $self->_csv->column_names(@{ $header }); my %hr; @hr{@{ $header }} = @$row; push @data, \%hr; @@ -243,7 +225,7 @@ sub _header_by_row { if ($self->is_multiplexed) { my $i = 0; foreach my $profile (@{ $self->profile }) { - if (@{ $row }[0] eq $profile->{row_ident}) { + if ($row->[0] eq $profile->{row_ident}) { return $header[$i]; } $i++; diff --git a/SL/Helper/Csv/Dispatcher.pm b/SL/Helper/Csv/Dispatcher.pm index 17d52d751..ce5780916 100644 --- a/SL/Helper/Csv/Dispatcher.pm +++ b/SL/Helper/Csv/Dispatcher.pm @@ -55,7 +55,7 @@ sub _class_by_line { } } } else { - $class = @{ $self->_csv->profile }[0]->{class}; + $class = $self->_csv->profile->[0]->{class}; } return $class; @@ -70,13 +70,13 @@ sub _specs_by_line { foreach my $p (@{ $self->_csv->profile }) { my $row_ident = $p->{row_ident}; if ($line->{datatype} eq $row_ident) { - $spec = @{ $self->_specs }[$i]; + $spec = $self->_specs->[$i]; last; } $i++; } } else { - $spec = @{ $self->_specs }[0]; + $spec = $self->_specs->[0]; } return $spec; @@ -128,8 +128,8 @@ sub parse_profile { foreach my $h (@{ $self->_csv->header }) { $header = $h; if ($self->_csv->profile) { - $profile = @{ $self->_csv->profile }[$i]->{profile}; - $class = @{ $self->_csv->profile }[$i]->{class}; + $profile = $self->_csv->profile->[$i]->{profile}; + $class = $self->_csv->profile->[$i]->{class}; } my $spec = $self->_parse_profile(profile => $profile, -- 2.20.1