Koha::CSV - Wrapper around Text::CSV_XS with Koha-specific defaults
my $csv = Koha::CSV->new();
$csv->combine(@fields);
my $line = $csv->string();
# Override defaults
my $csv = Koha::CSV->new( { sep_char => ';', always_quote => 1 } );
This class wraps Text::CSV_XS providing consistent defaults and methods used across Koha for CSV generation and parsing.
Configuration is inherited from Koha system preferences where applicable, but can be overridden per instance.
my $csv = Koha::CSV->new();
my $csv = Koha::CSV->new({ sep_char => ';', always_quote => 1 });
Creates a new Koha::CSV object. Accepts optional parameters to override defaults.
Parameters: - sep_char: CSV delimiter (defaults to CSVDelimiter system preference) - always_quote: Whether to quote all fields (defaults to 0) - eol: End of line character (defaults to "\n")
Note: binary and formula parameters are fixed for security and cannot be overridden.
$csv->combine(@fields);
Combines fields into a CSV line. Returns true on success.
my $line = $csv->string();
Returns the combined CSV line as a string.
my $line = $csv->add_row(@fields);
# or
my $line = $csv->add_row(\@fields);
Convenience method that combines fields and returns the CSV line as a string. Accepts either an array or arrayref of fields.
$csv->parse($line);
Parses a CSV line. Returns true on success.
my @fields = $csv->fields();
Returns the parsed fields from the last parse() call.
$csv->print($fh, \@fields);
Prints fields to a filehandle as a CSV line.
my $fields = $csv->getline($fh);
Reads and parses a line from a filehandle. Returns arrayref of fields.
my $error = $csv->error_diag();
Returns error diagnostics from the last operation.