NAME

Ekorn::Pipeline::CSV - read and write CSV and TSV, as sources, stages or sinks

SYNOPSIS

use Ekorn::Pipeline::CSV qw(:all);

# Sources: a row at a time, as arrays, or as hashes keyed by the
# header's names.
my $rows   = Ekorn::Pipeline->new(@stages)->iterator(from_csv(file => 'plays.csv'));
my $tracks = Ekorn::Pipeline->new(@stages)
    ->iterator(from_csv(file => 'tracks.csv', header => 1, munge => 'db'));

# TSV, or any other separator, by Text::CSV's options.
my $units = Ekorn::Pipeline->new(@stages)
    ->iterator(from_csv(file => 'units.tsv', header => 'skip', csv => {sep_char => "\t"}));

# Sinks: each row a line, giving how many; hashes in the header's
# order, or the run's columns, read from a header.
my $n = Ekorn::Pipeline->new(@stages, to_csv(file => 'out.csv', header => [qw(title plays)]))
    ->(@rows);
my($m) = Ekorn::Pipeline->new(@stages, to_csv(file => 'copy.csv', header => 1))
    ->iterator(from_csv(file => 'tracks.csv', header => 1))->all;

# Stages: parsing texts, and making lines.
my @parsed = Ekorn::Pipeline->new(from_csv(header => 1))->(@texts);
my @lines  = Ekorn::Pipeline->new(to_csv())->(@rows);

DESCRIPTION

Two functions, one for each direction, each making a different part by the options it's given, as Ekorn::Pipeline::JSON's do. Given a file or an fh, from_csv makes a source and to_csv a sink; given neither, each makes a stage. Each part is made afresh for each run, so a source read twice reads its header twice, and a run's parser is its own, unless you give one of your own.

The parser is Text::CSV's: Text::CSV_XS where it's installed, Text::CSV_PP otherwise. Ekorn::Pipeline only recommends Text::CSV, so it may not be installed: it's loaded when from_csv or to_csv is first called, which croaks if it can't be, and this module, and Ekorn::Pipeline::Tooling, load without it.

A file is read and written as UTF-8 unless encoding says otherwise, so its fields are characters. Text that won't decode, or a character its encoding can't hold, croaks at its row, rather than being written as something else. A file is opened as the run starts, emptied if it's written, and closed once the run has ended, however it ended; a filehandle is read and written as it is, with its own layers, and left open, for its opener to close.

A row that won't parse croaks, saying where, rather than being taken for the end of the input. A byte order mark that starts the input is dropped, unless the file is read raw.

Nothing is exported by default; ask for each, or for :all, or import them from Ekorn::Pipeline::Tooling's :csv.

FUNCTIONS

from_csv

from_csv(file => $path, %options)
from_csv(fh => $fh, %options)
from_csv(%options)

Given a file or an fh, a source of the rows read from it, a row at a time as values are pulled, so a stage that stops the run early stops the reading too. Each row is an array reference of its fields, or, with header true, a hash reference keyed by the header's names. When a header is read, with header true or skip, the run's context, which "run_context" in Ekorn::Pipeline gives a stage, is a hash reference whose columns are its names, as DBIx::Squirrel's sources' are:

my $column = sub ($row) {
    my $columns = Ekorn::Pipeline::run_context()->{columns};
    ...
};

Given neither, a stage parsing each value, a text of any number of rows, such as a file's contents, and passing on its rows. A quoted field may hold a newline, so the stage parses the whole value, not a line at a time. With a header, each value's first row is its header. A stage can't give the run a context.

to_csv

to_csv(file => $path, %options)
to_csv(fh => $fh, %options)
to_csv(%options)

Given a file or an fh, a sink writing each row as a line, and giving how many rows it wrote, not counting the header. A row is an array reference of its fields, or a hash reference, whose fields are written in the header's order; a field that's undefined is written empty. With a header, its line is written first, even if no row comes.

Given neither, a stage passing on each row's line, without its end of line, ready for "to_file" in Ekorn::Pipeline::Sinks, after the header's line, if there's a header.

Fields are quoted as Text::CSV quotes them: those holding the separator, a quote, a newline, a space or some other characters, such as "a b".

OPTIONS

file

The path of a file to read or write.

fh

An open filehandle to read or write.

csv

The parser: a hash reference of options for Text::CSV's new, such as {sep_char => "\t"} for TSV, or {eol => "\r\n"}, given over the defaults binary on and auto_diag off, as this module reports errors itself; or an object of your own, such as a Text::CSV_XS you've set up, used as it is.

Reading, true, false or skip. False, the default, reads every row as data, as an array reference. True reads the first row as the header, and each row after it as a hash reference keyed by its names: a short row's missing fields are undef, and a long row's extra fields are dropped. A header naming a column twice croaks, as one field's value would be lost. skip reads the first row as the header, and each row after it as an array reference.

Writing, true, false or an array reference of names. False, the default, writes no header, and each row must be an array reference. Names write them as the header, and order a hash's fields. True takes the names from the run's context's columns, as from_csv gives them, so

Ekorn::Pipeline->new(@stages, to_csv(file => 'copy.csv', header => 1))
    ->iterator(from_csv(file => 'tracks.csv', header => 1));

writes the columns it read, in the order it read them, and croaks if the run has no context naming its columns.

munge

Reading only, with a header: how its names are changed before they're used, as Text::CSV's munge_column_names does. none, the default, uses them as they are; lc or uc makes them lower or upper case; db makes them names a database takes, lower case, with each run of characters that aren't word characters an underscore, and none leading, so Track Name is track_name. A hash reference maps each name it has to a name of your own, leaving the rest as they are; a code reference is called with each name, as its argument and in $_, and returns the name to use. The names are munged before they're checked for a name given twice, and in the run's context too.

encoding

For a file only: the encoding its text is in, as Encode names it, such as latin1; UTF-8 by default; or raw, to read and write its fields as bytes, as they are, with a byte order mark kept. Written raw, a field holding a character that isn't a byte croaks.

DIAGNOSTICS

Can't find Text::CSV, which %s needs; install it from CPAN

from_csv or to_csv, named in the message, was called, and Text::CSV isn't installed. Ekorn::Pipeline only recommends it.

Can't load Text::CSV, which %s needs: %s

Text::CSV is installed, but failed to load; the second part says why.

Expected options as name => value pairs

A function was given an odd number of arguments.

Expected the options %s, not %s

A function was given an option it doesn't know, named after "not"; the first part lists those it does.

Expected file to be a path

file was undefined.

Expected a file or a filehandle, not both

A function was given both file and fh.

Expected an open filehandle

fh was something other than an open filehandle.

Expected encoding only with a file

encoding was given with a filehandle, or for a stage: neither opens a file.

Expected an encoding Perl knows, not %s

encoding wasn't raw, or an encoding Encode knows.

Expected header to be true, false or 'skip'

from_csv's header was something else.

Expected header to be true, false or an array reference of names

to_csv's header was something else, or an array holding something other than names.

Expected munge only with a header

munge was given, but header was false: there are no names to munge.

Expected munge to be lc, uc, db, none, a hash reference or a code reference

munge was something else.

Expected csv to be a hash reference of options, or a parser object

csv was something else.

Expected csv options the parser knows: %s

csv's options included one Text::CSV doesn't have; the rest of the message is Text::CSV's.

Expected a header naming each column once, not "%s" twice

from_csv, with header true, read a header naming a column twice, after munging, so one field's value would be lost. Read it with header skip, or munge the names apart.

Can't parse CSV from %s, record %d, field %d: %s

A row wasn't CSV, as the parser is set up to read it: the first part is the file's path, "the filehandle", or "a value", for a stage; then which record, counting the header as the first, and which field; and Text::CSV's reason.

Can't read %s: %s
Can't write %s: %s

A file couldn't be opened, read, written or closed, or a filehandle written; or its text wouldn't decode, or a character wouldn't encode, as its encoding says. The second part says why.

Expected a row as an array or a hash reference

to_csv was given something else as a row.

Expected a header naming the columns, to write a hash

to_csv was given a hash reference as a row, without a header to order its fields.

Expected the run's context to name its columns, for to_csv's header

to_csv's header was true, but the run had no context, or one without columns, such as a pushed run's.

Can't write a row as CSV: field %d is a reference

A row's field, counting from 1, was a reference, which CSV can't hold.

Can't write a row as CSV: field %d holds a wide character, and the file is written raw

A row's field, counting from 1, held a character that isn't a byte, in a file written with encoding raw.

Can't write a row as CSV: %s

The parser wouldn't combine a row's fields, saying why.

SEE ALSO

Ekorn::Pipeline, Ekorn::Pipeline::JSON, which reads and writes JSON the same way, and Text::CSV.

AUTHOR

Iain Campbell <cpanic@cpan.org>

LICENSE

Copyright (c) 2026 by Iain Campbell.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.