Text::CSV_PP::Iterator - Provide fetchrow_hashref() for CSV files
Text::CSV_PP::Iterator
use Text::CSV_PP::Iterator; my($parser) = Text::CSV_PP::Iterator -> new ({ column_names => [qw/One Two Three Four Five/], file_name => 'no.heading.in.file.csv', }); my($hashref); while ($hashref = $parser -> fetchrow_hashref() ) { print map{"$_ => $$hashref{$_}. "} sort keys %$hashref; print "\n"; }
Text::CSV_PP::Iterator is a pure Perl module.
It is a convenient wrapper around Text::CSV_PP. Points of interest:
o Text::CSV_PP::Iterator reads the file for you, using Iterator::IO. Warning: Iterator::IO V 0.02 has 3 bugs in it, where it does not call throw() properly. I've reported this via http://rt.cpan.org o All of Text::CSV_PP's new() parameters are supported by the fact that Text::CSV_PP::Iterator subclasses Text::CSV_PP o All data is returned as a hashref just like DBI's fetchrow_hashref(), using Text::CSV_PP::Iterator's only method, fetchrow_hashref() o The module reads the column headers from the first record in the file, or ... o The column headers can be passed in to new() if the file has none o Non-existent file errors throw the exception Iterator::X::IO_Error, which stringifies to a nice error message if you don't catch it o EOF returns undef to allow this neat construct: while ($hashref = $parser -> fetchrow_hashref() ){...} o Dependencies: - Iterator::IO - Text::CSV_PP o Example code: t/test.t demonstrates: - How to call fetchrow_hashref in isolation and in a loop - How to call fetchrow_hashref in eval{...} and catch exceptions
This module is available both as a Unix-style distro (*.tgz) and an ActiveState-style distro (*.ppd). The latter is shipped in a *.zip file.
See http://savage.net.au/Perl-modules.html for details.
See http://savage.net.au/Perl-modules/html/installing-a-module.html for help on unpacking and installing each type of distro.
new(...) returns a Text::CSV_PP::Iterator object.
This is the class's contructor.
Usage: Text::CSV_PP::Iterator -> new({...}).
This method takes a hashref of parameters. Only the file_name parameter is mandatory.
For each parameter you wish to use, call new as new({param_1 => value_1, ...}).
This is the name of the file that this module will read for you.
One record will be returned each time you call fetchrow_hashref().
fetchrow_hashref()
There is no default value for file_name.
This parameter is mandatory.
Returns an hashref ref of column data from the next record in the input file.
See the file t/test.t in the distro.
There are quite a few modules on CPAN which offer ways of processing CSV (and similar) files:
The original, and pure-Perl, way of doing things.
The major drawback is the lack of options to new().
new()
A pure-Perl version of the next module, and the parent of my module.
Allows the column separator to be surrounded by tabs or spaces. Nice.
Does not allow the column headers to be provided to new().
A compiled module, with many options.
Does not allow the column separator to be surrounded by tabs or spaces.
I always use this module if I have a compiler available. But that was before I wrote the current module.
Requires the external, compiled, library libcsv, which is written in C.
libcsv
I did not test this module.
This is a wrapper around the compiled code in Text::CSV_XS.
Text::CSV_XS
This module has a fake META.yml, which does not list any dependencies. However, when you try to install it, you get:
META.yml
- ERROR: Test::Exception is not installed - ERROR: IO::Scalar is not installed - ERROR: Class::Accessor is not installed - ERROR: Readonly is not installed - ERROR: List::MoreUtils is not installed * Optional prerequisite Text::TabularDisplay is not installed * Optional prerequisite Readonly::XS is not installed
A different way of viewing CSV files.
It supports some of the same options as Text::CSV_XS.
This module has a huge, and I do mean huge, number of methods. If only they worked...
Unfortunately, in one set of tests this module kept overwriting my input file, which is very nasty.
In another set, the method print_header() did not work. Now, that method calls format_header(), which looks for the field $self->{header}, but you have to have called read_header(), which does not set $self->{header}. Rather read_header() is aliased to bind_header(), which calls bind_fields(), which does not set $self->{header} either. It sets $self->{field_pos}. Oh, dear. Forget it.
print_header()
format_header()
read_header()
bind_header()
bind_fields()
Text::CSV_PP::Iterator was written by Ron Savage <ron@savage.net.au> in 2007.
Home page: http://savage.net.au/index.html
Australian copyright (c) 2007, Ron Savage. All Programs of mine are 'OSI Certified Open Source Software'; you can redistribute them and/or modify them under the terms of The Artistic License, a copy of which is available at: http://www.opensource.org/licenses/index.html
To install Text::CSV_PP::Iterator, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Text::CSV_PP::Iterator
CPAN shell
perl -MCPAN -e shell install Text::CSV_PP::Iterator
For more information on module installation, please visit the detailed CPAN module installation guide.