Small inconsistency or, at least, ambigious.
KES777 opened this issue · comments
I have a question on SO and people advice to use your module.
Module's synopsis has:
my $aoa = csv (in => "data.csv" ); # as array of array
my $aoh = csv (in => "data.csv", headers => "auto"); # as array of hash
# Write array of arrays as csv file
csv (in => $aoa, out => "file.csv", sep_char=> ";");
If I do not provide out
the result and call is made in scalar context the $aoa
or $aoh
will be returned.
if I provide out
the result will be saved there.
So call result and out
is two different things. First is the data how it was read from csv
file into memory, and out
is our csv
formatted data.
Why when I do $data = csv( in => $aoa, out => 'file.csv', headers => 'auto' )
I get in my $data csv
formatted data instead of $aoh
? nowhere in the doc I did not find that $data
should return csv
formatted array.
The doc clearly says
.... It returns an array- or hash-reference on parsing (or
"undef" on fail) or **the numeric value of "error_diag" on writing.**
https://github.com/makamaka/Text-CSV/blob/master/lib/Text/CSV.pm#L1669-L1671
If I provide data for the in
. is it always parsed?
If you provide out
, then in
should be a reference to an array or a hash (or a code reference that produces a reference to an array or a hash). If you only have a CSV string, then you need to parse it first (maybe with another csv
function).
Even if I do csv
call without out
parameter
my $data = [
[ 'id', 'name', 'value' ],
[ 23, 'foo', 77 ],
[ 44, 'bar', 'dfd' ],
]
$data = csv( in => $aoa, headers => 'auto' );
$data
has csv formatted data, but I expect it has aoa
because I did call in scalar context. I expect csv
only at out
Read the manual carefully.
in
Used to specify the source. "in" can be a file name (e.g. "file.csv"),
which will be opened for reading and closed when finished, a file handle
(e.g. $fh or "FH"), a reference to a glob (e.g. "\*ARGV"), the glob
itself (e.g. *STDIN), or a reference to a scalar (e.g. "\q{1,2,"csv"}").
In the case above, in => $aoa
is not correct, and predefined $data
is meaningless because csv( in => ... )
without out
(nor headers
) returns aoa
, that means an array (reference) of array (references), like your predefined $data
.
If you don't understand how to use csv
function, try Object interface ( my $csv = Text::CSV->new; $csv->read ...
etc), which should be much more straightforward for you.