DBIx::TableLoader::CSV - Easily load a CSV into a database table
version 1.102
my $dbh = DBI->connect(@connection_args); DBIx::TableLoader::CSV->new(dbh => $dbh, file => $path_to_csv)->load(); # interact with new database table full of data in $dbh
This is a subclass of DBIx::TableLoader that handles the common operations of reading a CSV file (using the powerful Text::CSV (which uses Text::CSV_XS if available)).
This module simplifies the task of transforming a CSV file into a database table. This functionality was the impetus for the parent module (DBIx::TableLoader).
In most cases simply calling load() is sufficient (see "load" in DBIx::TableLoader). The methods defined by this subclass are documented for completeness.
load()
Accepts all options described in "OPTIONS" in DBIx::TableLoader plus some CSV specific options.
See "OPTIONS".
Returns $csv->getline($io).
$csv->getline($io)
After the last row is returned this will check "eof" in Text::CSV and croak with the message from "error_diag" in Text::CSV as described by "SYNOPSIS" in Text::CSV. (If you wish to disable this behavior you can set ignore_csv_errors => 1 in the constructor.)
ignore_csv_errors => 1
If the name option is not provided, and the file option is, this returns the file basename.
name
file
Falls back to 'csv'.
'csv'
This is called automatically from the constructor to make things as simple and automatic as possible.
Load csv_class if it is not.
csv_class
Instantiate csv_class with csv_defaults and csv_opts.
csv_defaults
csv_opts
Open the file provided unless io is passed instead.
io
Discard the first row if columns is provided and no_header is not.
columns
no_header
There are many options available for configuration. Options specific to this module are listed below. Also see "OPTIONS" in DBIx::TableLoader for options from the base module.
Basic usage:
csv_opts - Hashref of options to pass to the new method of csv_class
new
See Text::CSV for its list of accepted options.
file - Path to a csv file
The file will be opened (unless io is provided) and its basename will be the default table name (which can be overwritten with the name option).
file_encoding - The encoding of the CSV file.
file_encoding
If specified this is appended to the open mode as :encoding(ENCODING).
open
:encoding(ENCODING)
Options for more customization/control:
csv - A Text::CSV compatible object instance
csv
If not supplied an instance will be created using $csv_class->new(\%csv_opts).
$csv_class->new(\%csv_opts)
csv_class - The class to instantiate if csv is not supplied
Defaults to Text::CSV (which will attempt to load Text::CSV_XS and fall back to Text::CSV_PP).
Text::CSV
csv_defaults - Hashref of default options for csv_class constructor
Includes { binary => 1 } (as encouraged by Text::CSV); To turn off the binary option you can pass { binary => 0 } to csv_opts. If you are using a different csv_class that does not accept the binary option you may need to overwrite this with an empty hash.
{ binary => 1 }
binary
{ binary => 0 }
file_open_layers - String of arbitrary PerlIO layers
file_open_layers
to apply when opening the file.
ignore_csv_errors - Boolean (defaults to false)
ignore_csv_errors
If Text::CSV fails to parse a row it will abort and skip the rest of the file. This module detects parser errors and will die with the message from "error_diag" in Text::CSV upon failure to read the whole file. (This behavior is similar to (but separate from) setting auto_diag => 2 in the csv options.) Set this option to a true value if you want to accept partially read CSV files rather than getting an error. Note that other exceptions can still be thrown (including failure to open the file or if a misconfigured parser or malformed CSV returns a row with an inconsistent number of columns).
die
auto_diag => 2
io - A filehandle or IO-like object from which to read CSV lines
This will be used as $csv->getline($io). When providing this option you can still provide file if you want the table name to be determined automatically (but no attempt will be made to open file).
name - Table name
If not given the table name will be set to the file basename or 'csv' if file is not provided.
no_header - Boolean
Usually the first row [header] of a CSV is the column names. If you specify columns this module assumes you are overwriting the usual header row so the first row of the CSV will be discarded. If there is no header row on the CSV (the first row is data), you must set no_header to true in order to preserve the first row of the CSV.
DBIx::TableLoader
Text::CSV::Auto - Alternative project automating CSV usage
You can find documentation for this module with the perldoc command.
perldoc DBIx::TableLoader::CSV
The following websites have more information about this module, and may be of help to you. As always, in addition to those websites please use your favorite search engine to discover more resources.
Search CPAN
The default CPAN search engine, useful to view POD in HTML format.
http://search.cpan.org/dist/DBIx-TableLoader-CSV
RT: CPAN's Bug Tracker
The RT ( Request Tracker ) website is the default bug/issue tracking system for CPAN.
http://rt.cpan.org/NoAuth/Bugs.html?Dist=DBIx-TableLoader-CSV
CPAN Ratings
The CPAN Ratings is a website that allows community ratings and reviews of Perl modules.
http://cpanratings.perl.org/d/DBIx-TableLoader-CSV
CPAN Testers
The CPAN Testers is a network of smokers who run automated tests on uploaded CPAN distributions.
http://www.cpantesters.org/distro/D/DBIx-TableLoader-CSV
CPAN Testers Matrix
The CPAN Testers Matrix is a website that provides a visual overview of the test results for a distribution on various Perls/platforms.
http://matrix.cpantesters.org/?dist=DBIx-TableLoader-CSV
CPAN Testers Dependencies
The CPAN Testers Dependencies is a website that shows a chart of the test results of all dependencies for a distribution.
http://deps.cpantesters.org/?module=DBIx::TableLoader::CSV
Please report any bugs or feature requests by email to bug-dbix-tableloader-csv at rt.cpan.org, or through the web interface at http://rt.cpan.org/NoAuth/ReportBug.html?Queue=DBIx-TableLoader-CSV. You will be automatically notified of any progress on the request by the system.
bug-dbix-tableloader-csv at rt.cpan.org
https://github.com/rwstauner/DBIx-TableLoader-CSV
git clone https://github.com/rwstauner/DBIx-TableLoader-CSV.git
Randy Stauner <rwstauner@cpan.org>
This software is copyright (c) 2011 by Randy Stauner.
This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.
To install DBIx::TableLoader::CSV, copy and paste the appropriate command in to your terminal.
cpanm
cpanm DBIx::TableLoader::CSV
CPAN shell
perl -MCPAN -e shell install DBIx::TableLoader::CSV
For more information on module installation, please visit the detailed CPAN module installation guide.