in reply to Text::CSV_XS and encoding

Just to let you all know

This monk has found some rest in this monastary.... thank you all _o_

Putting it all together:

my @medewerkers; open(my $FH, '<:encoding(utf8)', \$res->content) || die("could not + open result as file: $!"); my $csv = Text::CSV->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; return \@medewerkers;

Replies are listed 'Best First'.
Re^2: Text::CSV_XS and encoding
by Tux (Canon) on Sep 18, 2018 at 06:49 UTC

    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 :)


    Enjoy, Have FUN! H.Merijn