in reply to Re: Text::CSV_XS and encoding
in thread Text::CSV_XS and encoding
As Text::CSV is back in sync with Text::CSV_XS, you can shorten that a lot :)
→#!/usr/bin/env perl use 5.14.2; use warnings; use Text::CSV "csv"; use Data::Peek; my $content = <<"EOC"; id;naaam;datum in dienst;functie 1;Jip;2001-04-14;Chef lege dozen 2;Janneke;2013-10-01;Miep kraak EOC my @medewerkers; open my $FH, "<:encoding(utf8)", \$content or die "could not open result as file-handle: $!"; my $csv = Text::CSV_XS->new ({ sep_char => ";", binary => 1, auto_diag => 1, }); $csv->header ($FH, { detect_bom => 1 }); while (my $row = $csv->getline_hr ($FH)) { push @medewerkers, $row; } close $FH; DDumper \@medewerkers;
[ { 'datum in dienst' => '2001-04-14', functie => 'Chef lege dozen', id => '1', naaam => 'Jip' }, { 'datum in dienst' => '2013-10-01', functie => 'Miep kraak', id => '2', naaam => 'Janneke' } ]
Now shorten that to
#!pro/bin/perl use 5.14.2; use warnings; use Text::CSV "csv"; use Data::Peek; my $content = <<"EOC"; id;naaam;datum in dienst;functie 1;Jip;2001-04-14;Chef lege dozen 2;Janneke;2013-10-01;Miep kraak EOC DDumper csv (in => \$content, bom => 1);
Or, if you want to be more explicit
csv (in => \$content, bom => 1, sep => ";");Which would reduce your function to
sub csv_content { my $res = shift; return csv (in => \$res->content, bom => 1, sep => ";"); } # csv_content
Add some error handling and you're done :)
|
|---|