The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

Tie::Array::CSV - A tied array which combines the power of Tie::File and Text::CSV

SYNOPSIS

 use strict; use warnings;
 use Tie::Array::CSV;
 tie my @file, 'Tie::Array::CSV', 'filename';

 print $file[0][2];
 $file[3][5] = "Camel";

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:

This module was inspired by Tie::CSV_File 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.

OPTIONS

As with any tied array, the construction uses the tie function.

 tie my @file, 'Tie::Array::CSV', 'filename';

would tie the lexically scoped array @file to the file filename using this module. Following these three arguements to tie, one may optionally pass a hashref containing additional configuration. Currently the only options are "pass-through" options, sent to the constructors of the different modules used internally, read more about them in those module's documentation.

  • tie_file - hashref of options which are passed to the Tie::File constructor

  • text_csv - hashref of options which are passed to the Text::CSV constructor

example:

 tie my @file, 'Tie::Array::CSV', 'filename', { 
   tie_file => {}, 
   text_csv => { sep_char => ';' },
 };

ERRORS

For simplicity this module croaks on all errors, which are trappable using a $SIG{__DIE__} handler.

CAVEATS

  • Much of the functionality of normal arrays is mimicked using Tie::Array. The interaction of this with Tie::File should be mentioned in that certain actions may be very inefficient. For example, (un)shift-ing the first row of data will probably involve Tie::Array asking Tie::File to move each row up one line, one-by-one. As a note, the intra-row (un)shift does not suffer this problem.

  • Some effort had been made to allow for fields which contain linebreaks. Linebreaks would change line numbers used for row access by Tie::File. This, unfortunately, moved the module far from its stated goals, and therefore far less powerful for its intended purposes. The decsion has been made (for now) not to support such files.

SEE ALSO

SOURCE REPOSITORY

http://github.com/jberger/Tie-Array-CSV

AUTHOR

Joel Berger, <joel.a.berger@gmail.com>

COPYRIGHT AND LICENSE

Copyright (C) 2011 by Joel Berger

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.