Catman...er::CSV(3pm)

πŸ“¦
version 1.2025 Β· 4 versions
, in ubuntu resolute, ubuntu stonking

πŸ’– Catmandu::Exporter::CSV πŸ’–

okay bestie, gather round, because we're talking about Catmandu::Exporter::CSV β€” aka a CSV exporter β€” and honestly? it's giving spreadsheet royalty. πŸ‘‘


✨ SYNOPSIS ✨

first up, the command line era, as always:

# On the command line

$ catmandu convert XSL to CSV < data.xls

$ catmandu convert JSON to CSV --fix myfixes.txt --sep_char ';' < data.json

# Provide a hint to the exporter which fields to export from the JSON
# input. By default the CSV exporter will export the fields that are
# found in the first JSON record.
$ catmandu convert JSON to CSV --fields "id,title,year" < data.json

and now the Perl script girlies:

# In a Perl script

use Catmandu;

my $exporter = Catmandu->exporter('CSV',
            fix => 'myfix.txt',
            quote_char => '"',
            sep_char => ',',
            escape_char => '"' ,
            always_quote => 1,
            header => 1);

$exporter->fields("f1,f2,f3");
$exporter->fields([qw(f1 f2 f3)]);

$exporter->add_many($arrayref);
$exporter->add_many($iterator);
$exporter->add_many(sub { });

$exporter->add($hashref);

printf "exported %d items\n" , $exporter->count;

look at her go πŸ¦‹


🌸 DESCRIPTION 🌸

so this Catmandu::Exporter takes your items and turns them into rows of comma-separated values (you know her, you love her: CSV). the serialization is based on Text::CSV, no cap. πŸ’…

if the header option is set, you get a lil header line with the field names at the top β€” iconic behaviour honestly. and if you wanna know how to configure the field mapping and the column names, go read Catmandu::TabularExporter, she has the tea. β˜•

oh and heads up: newlines and tabulator values inside your field values get escaped as \n, \r, and \t. so your CSV stays cute and un-broken. ✨

hint bestie: by default the exporter outputs all the fields found in the first record of the data input. wanna change that? just set the fields option of the exporter and she'll do what you say. 🫢

πŸŽ€ CONFIGURATION πŸŽ€

the options. the whole options. nothing but the options. let's go. ⭐

  • file β€” write the output to a local file, given either by its path or by a file handle. OR, plot twist: pass a scalar reference and it writes to a string instead, or pass a code reference and it writes to a callback function. choose your fighter. πŸ’–
  • fh β€” write the output to an IO::Handle. if you don't specify it, Catmandu::Util::io steps in and builds the output handle from the file argument, or just uses STDOUT. easy.
  • fix β€” an ARRAY of one or more fixes or file scripts that get applied to the exported items. glow-up pass, basically. ✨
  • encoding β€” the binmode of the output stream fh. defaults to ":utf8" btw, she's already set up for you. 🌈
  • sep_char β€” the column separator. defaults to "," (comma girl summer).
  • quote_char β€” the quotation character. defaults to """.
  • escape_char β€” the character used for escaping inside a quoted field. defaults to """.
  • fields β€” go see Catmandu::TabularExporter, she explains it. πŸ“–
  • columns β€” also see Catmandu::TabularExporter. same deal.
  • header β€” include a header line with the column names. enabled by default, because duh. πŸ‘‘

πŸ¦‹ METHODS πŸ¦‹

want the full list of methods? okay overachiever, we love that for you. go read:

  • Catmandu::TabularExporter
  • Catmandu::Exporter
  • Catmandu::Addable
  • Catmandu::Fixable
  • Catmandu::Counter
  • Catmandu::Logger

πŸ‘€ SEE ALSO πŸ‘€

more reading material, because we stay educated:

  • Catmandu::Importer::CSV, Catmandu::Exporter::Table
  • Catmandu::Exporter::XLS

and that's the whole page bestie πŸ’œ β€” this lil masterpiece shipped with perl v5.40.1 and was dated 2026-01-24, so go forth and export some absolute slay. periodt. πŸ’…βœ¨

πŸ“¦ where this page comes from