=head1 NAME Tie::Array::CSV - A tied array which combines the power of Tie::File and Text::CSV =head1 SYNOPSIS use strict; use warnings; use Tie::Array::CSV; tie my @file, 'Tie::Array::CSV', 'filename'; print $file[0][2]; $file[3][5] = "Camel"; =head1 DESCRIPTION This module allows an array to be tied to a CSV file for reading and writing. The array is a standard Perl 2D array (i.e. an array of array references) which gives access to the row and column of the user's choosing. This is done using the well established modules: =over =item * L =over =item * arbitrary line access =item * low memory use even for large files =back =item * L =over =item * row parsing =item * row updating =item * uses the speedy L if installed =back =back This module was inspired by L which (sadly) hasn't been maintained. It also doesn't attempt to do any of the parsing (as that module did), but rather passes all of the heavy lifting to other modules. Note that while the L prevents the need to read in the entire file, while in use, a parsed row IS held in memory. =head1 CONSTRUCTORS Since version 0.04 both constructors allow the options that version 0.03 only offered for the C constructor. The constructors must be passed a file name, either as the first argument, or as the value to the option key C. Options may be passed as key-value pairs or as a hash reference. This yields the many ways of calling the constructors shown below, one for every taste. N.B. Should a lone argument filename and a C option key both be passed to the constructor, the lone argument wins. =head2 C Constructor As with any tied array, the construction uses the C function. Basic usage is as follows: tie my @file, 'Tie::Array::CSV', 'filename'; which would tie the lexically scoped array C<@file> to the file C using this module. Following the first two arguements to C, one may optionally pass a key-value pairs or a hashref containing additional configuration or even file specification. tie my @file, 'Tie::Array::CSV', 'filename', { opt_key => val, ... }; tie my @file, 'Tie::Array::CSV', 'filename', opt_key => val, ... ; tie my @file, 'Tie::Array::CSV', { file => 'filename', opt_key => val, ... }; tie my @file, 'Tie::Array::CSV', file => 'filename', opt_key => val, ... ; Of course, the magical Perl C can be scary for some, for those people there is the ... =head2 C Constructor [ Added in version 0.03 ] my $array = Tie::Array::CSV->new( 'filename' ); my $array = Tie::Array::CSV->new( 'filename', { opt_key => val, ... }); my $array = Tie::Array::CSV->new( 'filename', opt_key => val, ... ); my $array = Tie::Array::CSV->new( file => 'filename', opt_key => val, ... ); my $array = Tie::Array::CSV->new( { file => 'filename', opt_key => val, ... } ); It only returns a reference to the Cd array due to a limitations in how C magic works. =head2 Options =over =item * C - alternative method for specifing the file to C. This is overridden by a lone filename or handle passed as the first argument to the constructor. =item * C - hashref of options which are passed to the L constructor =item * C - either: =over =item * hashref of options which are passed to the L constructor =item * an object which satisfies C<< isa('Text::CSV') >> (added in version 0.05) =back =item * C - for ease of use, a C option may be specified, which is passed to the L constructor. This option overrides a corresponding entry in the C pass-through hash. =back Equivalent examples: tie my @file, 'Tie::Array::CSV', 'filename', { tie_file => {}, text_csv => { sep_char => ';' }, }; tie my @file, 'Tie::Array::CSV', 'filename', sep_char => ';'; Note that as of version 0.05 the functionality from the former C option has been separated into its own subclass module L. If deferring row operations is of interest to you, please see that module. =head1 ERRORS For simplicity this module Cs on all almost all errors, which are trappable using a C<$SIG{__DIE__}> handler. Modifing a severed row object issues a warning. =head1 CAVEATS =over =item * Much of the functionality of normal arrays is mimicked using L. The interaction of this with L should be mentioned in that certain actions may be very inefficient. For example, C<(un)shift>-ing the first row of data will probably involve L asking L to move each row up one line, one-by-one. As a note, the intra-row C<(un)shift> does not suffer this problem. =item * At one time, some effort was been made to allow for fields which contain linebreaks. Quickly it became clear that linebreaks would change line numbers used for row access by L. Attempts to compensate for this, unfortunately, moved the module far from its stated goals, and therefore far less powerful for its intended purposes. The decision has been made (for now) not to support such files. =back =head1 SEE ALSO =over =item * L - inspiration for this module, but problematic =back =head1 SOURCE REPOSITORY L =head1 AUTHOR Joel Berger, Ejoel.a.berger@gmail.comE =head1 CONTRIBUTORS Mithaldu - Christian Walde (cpan:MITHALDU) =head1 COPYRIGHT AND LICENSE Copyright (C) 2013 by Joel Berger This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.