Bei Importdaten das UTF8 BOM ignorieren.
authorSven Schöling <s.schoeling@linet-services.de>
Tue, 31 Jul 2012 12:57:49 +0000 (14:57 +0200)
committerSven Schöling <s.schoeling@linet-services.de>
Tue, 31 Jul 2012 12:57:49 +0000 (14:57 +0200)
behebt Bug 1872

SL/Helper/Csv.pm
t/helper/csv.t

index 3132b28..5b629ee 100644 (file)
@@ -111,6 +111,14 @@ sub _check_header {
     ]) unless $header;
   }
 
+  # Special case: utf8 BOM.
+  # certain software (namely MS Office and notepad.exe insist on prefixing
+  # data with a discouraged but valid byte order mark
+  # if not removed, the first header field will not be recognized
+  if ($header && $header->[0] && $self->encoding =~ /utf-?8/i) {
+    $header->[0] =~ s/^\x{FEFF}//;
+  }
+
   return unless $header;
   return $self->header([ map { lc } @$header ]);
 }
index 4e7ef12..63fc858 100644 (file)
@@ -1,4 +1,4 @@
-use Test::More tests => 40;
+use Test::More tests => 41;
 
 use lib 't';
 
@@ -285,11 +285,21 @@ is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive hea
 #####
 
 $csv = SL::Helper::Csv->new(
-  file   => \"Kaffee",
-  header => [ 'Description' ],
-  class  => 'SL::DB::Part',
+file   => \"Kaffee",
+header => [ 'Description' ],
+class  => 'SL::DB::Part',
 );
 $csv->parse;
 is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'case insensitive header as param works';
 
+#####
+
+$csv = SL::Helper::Csv->new(
+  file   => \"\x{FEFF}description\nKaffee",
+  class  => 'SL::DB::Part',
+  encoding => 'utf8',
+);
+$csv->parse;
+is_deeply $csv->get_data, [ { description => 'Kaffee' } ], 'utf8 BOM works (bug 1872)';
+
 # vim: ft=perl