Parse-CSV-Colnames
view release on metacpan or search on metacpan
NAME
Parse::CSV::Colnames - Highly flexible CSV parser including column names
(field names) manipulation
NOTE
This Module derives from Parse::CSV by Adam Kennedy inheriting its
methods. The main extensions are methods for column names manipulation
and some simple method-fixes.
SYNOPSIS
Column names manipulation makes only sense if the fields-parameter is
auto, i.e. column names are in the first line.
# Parse a colon-separated variables file from a handle as a hash
# based on headers from the first line.
my $objects = Parse::CSV::Colnames->new(
handle => $io_handle,
sep_char => ';',
fields => 'auto',
# select only rows where column name fieldname is "value"
filter => sub { if($_->{fieldname} eq "value")
{$_} else
{undef}
}
);
# get column names
my @fn=$objects->colnames
# you want lower case field names
@fn=map {lc} @fn;
# you want field names without blanks
@fn=map { s/\s+//g} @fn;
# set column names
$objects->colnames(@fn);
while ( my $object = $objects->fetch ) {
$object->do_something;
}
DESCRIPTION
This module is only an extension of Parse::CSV
For a detailed description of all methods see Parse::CSV
For a detailed description of the underlying csv-parser see Text::CSV_XS
Fixed METHODS
These methods have not work in the parent module Parse::CSV yet, because
Adam Kennedy is very busy.
combine
$status = $csv->combine(@columns);
The "combine" method is passed through to the underlying Text::CSV_XS
object. See example 3.
It sets the fields and constructs the corresponding csv string from the
arguments. You can read this array with the "fields" method.
string
$line = $csv->string;
The "string" method is passed through to the underlying Text::CSV_XS
object. See example 3 and example 4.
It returns the parsed string or the corresponding combine-setting.
print
$status = $csv->print($io, $colref);
The "print" method is passed through to the underlying Text::CSV_XS
object. See example 1.
It prints the string of the corresponding @$colref directly to an IO
handle.
Added METHODS
fields
@fields = $csv->fields;
The "fields" method is passed through to the underlying Text::CSV_XS
object.
It returns the input to "combine"-method or the actual row as an array.
colnames
@colnames = $csv->colnames("fn1","fn2") # sets colnames
or
@colnames = $csv->colnames; # gets colnames
The "colnames" method sets or gets colnames (="fields"-param). So you
can rename the colnames (hash-keys in Parse::CSV::Colnames object).
pushcolnames
@colnames = $csv->pushcolnames("fn1","fn2")
The "pushcolnames" method adds colnames at the end of $csv->colnames
(="fields"-param). You can do that if the "filter"-method adds some new
fields at the end of fields-array in Parse::CSV::Colnames object .
Please consider that these colnames or fields are not in the underlying
Text::CSV_XS object. See example 1 and example 4.
pushcombine
@colnames = $csv->pushcombine("fn1","fn2")
The "pushcombine" method adds fields at the end of the actual row
(="fields"-method) and constructs the corresponding csv string. You can
read the result with the "fields"-method. The pushcombine and
pushcolnames belong together. See example 4.
( run in 2.294 seconds using v1.01-cache-2.11-cpan-d8267643d1d )