in reply to Select DB data into CSV file

Hi 1nickt,

Thanks for the mention. I linked DBIx::CSV to Text::Table::CSV instead of Text::CSV_XS directly because DBIx::CSV is just a special case of DBIx::TextTableAny to output to various forms of text tables, which CSV is one of.

I wrote a short blog post on DBIx::CSV here and noted that you can also do something like:

use Text::CSV; my $csv = Text::CSV->new; ... $csv->print(\*STDOUT, $sth->{NAME}); while (my $row = $sth->fetchrow_arrayref) { $csv->say(\*STDOUT, $row); }

which doesn't require any additional module and not much extra code anyway. But since you do not use DBI directly, that's a bit of a problem as well.

I haven't looked into DBIx::Class to know if it's easily extensible to do what you want. But how about a patch-based solution like Patch::DBI::WriteCSV (use this if the link doesn't exist yet). As long as something like DBIx::Class calls one of DBI's fetchrow_{array,arrayref,hashref} or fetchall_{arrayref,hashref}, it will work. I've tested it with the simple sample database from the DBIx::Class documentation. This code:

Patch::DBI::WriteCSV->import; my @all_artists = $schema->resultset('Artist')->all; Patch::DBI::WriteCSV->unimport;
will produce something like:
artistid,name
1,Enya
2,"Mariah Carey"

If you don't want to depend on my module, you can just use the underlying code and integrate that to your own code base. As for the "one-person ecosystem" remark: there's a line between NIH syndrome and "not finding what I want/need on CPAN" and I can say that I am still on the second camp. But I can't help if what I want/need is not on CPAN and thus write a module for it :-) I do use other people's modules. Of my 1512 distributions on CPAN, I depend on 610 modules written by others.

% lcpan dists --author PERLANCAR | xargs lcpan deps | grep -v PERLANCAR | wc -l

Replies are listed 'Best First'.
Re^2: Select DB data into CSV file
by 1nickt (Canon) on Dec 20, 2018 at 17:36 UTC

    LOL perlancar, you are amazing. I didn't expect to get a new distro out of my question! Thanks for your reply which I will study this evening, very kind.

    ( P.S. The blog posted you linked to was how I found DBIx::CSV in the first place ;-) )


    The way forward always starts with a minimal test.