I had quite a long discussion with Lady_Aleena in the chatterbox, as I wanted to fully understand her wishes. She had a number of complaints about Text::CSV (and Text::CSV_XS) being to complicated for simple end-user tasks.
We - as module authors - should always take remarks like that serious, even if the end user might not exactly be the target audience we had in mind when writing functionality.
<cbstream> [Lady_Aleena] My problem is that modules like Text::CSV_XS +doesn't open the files for me too. <cbstream> [Lady_Aleena] Tux, I might use Text::CSV if it becomes a on +e liner.
I've heard remarks like that before, but so far always ignored them, as the function/method to do so is so simple that including something like that in the module itself feels like bloat.
Now that I understand what Lady_Aleena actually wants with her data - to directly create a hash of hashes from a CSV-like file, I tried to come up with a far more generic function. Having rfc7111 fragments now, that only makes more sense :)
After a few iterations, I came up with a function that support all basic needs, yet still allows a lot of flexibility. Without going into the implementation, what I currently have done supports:
my $AoA = csv2list (file => "file.csv"); my $AoA = csv2list (data => $io, sep_char => "|"); my $AoA = csv2list (file => "file.txt", sep_char => "|", fragment => " +col=3;5-6;0-*"); my $AoH = csv2list (file => "file.csv", headers => "auto"); my $AoH = csv2list (data => $io, sep_char => "|", headers=> [ "Name", "Hobby", "Age" ]);
When I apply that to the code on Lady_Aleena's scratchpad, the difference would be somethink like
la.txt: Jan|Birdwatching|7 LA|coding|25 Tux|skiing|52 code: my @gdr = qw( Name Hobby Age ); my %la = lady_aleena (file => "la.txt", headings => \@hdr); my $aoh = csv2list (file => "la.txt", sep_char => "|", headers => \@hd +r); my %hoh = map { $_->{Name} => $_ } @{$aoh}; result for both: { Jan => { Age => 7, Hobby => 'Birdwatching', Name => 'Jan' }, LA => { Age => 25, Hobby => 'coding', Name => 'LA' }, Tux => { Age => 52, Hobby => 'skiing', Name => 'Tux' } }
I'll let this sink in a bit. Obviously a function like this has a lot of potential, but should it be auto-exported? And is it flexible enough as it is like this?
|
|---|