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
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.