H.Merijn Brand, the Text::CSV_XS maintainer has been dicussing possibilities for adding parsing rows as hashrefs to that module through this RT ticket.
As fate would have it, our efforts to implement it crossed paths, and we now both have fairly complete but somewhat different patches for the feature. A couple points to get feedback on:
Which you do find clearer for setting the column names to use as the hash keys:
column_names()
or
hr_keys()
I have already been confused about whether "hr" stood for "header row" or "hashref", so I vote for the former.
The second point, which is currently in neither patch, is "how you design the interface to automatically setting the column names from the first row of the CSV?"
Parse::CSV uses new( fields => 'auto' ), but involving new() won't work for Text::CSV_XS.
I was thinking of perhaps:
$csv->column_names_from_line($io);
Which would simply mean:
$csv->column_names( $csv->getline($io) );
We would leave it up to documentation to make sure users called this first thing.
Alternately, you could have a function that stores the current file position, rewinds and reads the first row, and then returns to the current position. That seems more fragile to me, and I can oly imagine there are some non-rewindable filehandles out there for which it wouldn't work.
You can leave feedback here and/or in the RT ticket.
Thanks!
Mark
Feedback (Score:1)
No contest:
column_names.Definitely no rewinding magic. In fact, I wouldn’t even include a
column_names_from_linesugar method, because all it does is save five keystrokes on a rare operation for the price of concealing how that works. And that’s the only reason users might forget about having to call that method first thing.If you just tell them that they need to set up the column names manually (and here’s an easy way to take them from the first line of the file), then it’s p
Re: (Score:1)
column_names (Score:1)
I agree that column_names is a much better than the other.
column_names gets my vote! (Score:1)
But have you seen the interface that Text::xSV [cpan.org] uses?
Personally I quite like the read_header() function.
Re: Text::xSV (Score:1)
I think because it doesn't have "CSV" in the module name, distribution name or description, it's difficult to find by searching by on CPAN. At least it was for me.
I did not that it is not an "XS" module, so would be slower than Text::CSV_XS.
Re: (Score:1)
http://search.cpan.org/~rsavage/Text-CSV_PP-Iterator-1.00/ [cpan.org]
which also discusses various similar modules.
column_names it is ... (Score:1)
I liked the idea of hr_** for its double meaning: Hash Ref and Header Row, but that might be professional brain deformation from my side.
While I was designing this, I also had DBI in mind, and the obvious next step to try is bind_columns ().
With the new column_names (), it would be nice to do a DBI like bind_keys () so fields are stored in the same scalar over and over again, instead of creating a new scalar on parsing for every field line after line again.
This *could* mean
Enjoy, have FUN! H.Merijn
Tie::Handle::CSV (Score:1)
Done, plus Bind_columns. Go fetch 0.40 (Score:1)
You've now got it, and I also give you bind_columns!
I value feedback, and probably some improvements on the docs, like adding the new stuff to the SYNOPSIS
Enjoy, have FUN! H.Merijn