NAME

Data::Toolkit::Connector::CSV

DESCRIPTION

Connector for reading CSV files.

SYNOPSIS

$conn2 = Data::Toolkit::Connector::CSV->new();
open SOURCE, "<mydata.csv";
my $csvParser = Text::CSV_XS->new();
$conn2->parser( $csvParser );
$conn2->datasource( sub { return <SOURCE> } );
$conn2->columns( ['one','two','three'] );
while ($entry = $conn2->next()) {
   print $entry->dump(), "\n";
}

DEPENDENCIES

Carp
Clone
Text::CSV_XS    for testing

Constructor

new

my $csvConn = Data::Toolkit::Connector::CSV->new();

Creates an object of type Data::Toolkit::Connector::CSV

Methods

parser

Define the CSV parser for the connector to use. This should be an object of type Text::CSV_XS or similar.

my $res = $csvConn->parser( Text::CSV_XS->new() );

Returns the object that it is passed.

datasource

Specify a data source. This should be a reference to a procedure that returns one line of text per call.

If the parameter is undef, data will be read using the magic "<>" token

my $res = $csvConn->datasource( sub { return <SOURCE>; } );

Returns the object that it is passed.

columns

Specify the names of the columns in the CSV file

my $arrayRef = $csvConn->columns( ['firstname','surname','mail','phone'] );
my $arrayRef = $csvConn->columns();

Returns the list of columns as an array or array reference

colsFromFile

Read the column names from the first line of the CSV file

my $entry = $csvConn->colsFromFile();

Returns the list of columns on success, or undef on failure.

It is an error to call this method without first calling the datasource and parser methods. Doing so will cause an exception to be thrown.

next

Read the next entry from the CSV file

my $entry = $csvConn->next();

The result is a Data::Toolkit::Entry object if there is data left in the file, otherwise it is undef.

linecount

Return the number of the line that we are currently processing

$count = $csvConn->linecount();

currentline

Return the line that we are currently processing

$count = $csvConn->currentline();

Debugging methods

debug

Set and/or get the debug level for Data::Toolkit::Connector

my $currentDebugLevel = Data::Toolkit::Connector::CSV->debug();
my $newDebugLevel = Data::Toolkit::Connector::CSV->debug(1);

Any non-zero debug level causes the module to print copious debugging information.

Note that this is a package method, not an object method. It should always be called exactly as shown above.

All debug information is reported using "carp" from the Carp module, so if you want a full stack backtrace included you can run your program like this:

perl -MCarp=verbose myProg

Author

Andrew Findlay

Skills 1st Ltd

andrew.findlay@skills-1st.co.uk

http://www.skills-1st.co.uk/