Tie-Array-CSV-0.08000755000765000024 013546656375 13130 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/Build.PL000444000765000024 116413546656375 14563 0ustar00joelstaff000000000000use strict; use warnings; use Module::Build 0.3601; my $builder = Module::Build->new( module_name => 'Tie::Array::CSV', dist_author => 'Joel Berger', license => 'perl', requires => { 'perl' => 5.006001, 'Tie::File' => 0, 'Text::CSV' => 0, 'Scalar::Util' => 0, }, configure_requires => { 'Module::Build' => 0.3601, }, meta_merge => { resources => { repository => "http://github.com/jberger/Tie-Array-CSV", bugtracker => "http://github.com/jberger/Tie-Array-CSV/issues", }, no_index => { file => [ 'README.pod' ], }, } ); $builder->create_build_script; Tie-Array-CSV-0.08/Changes000444000765000024 167713546656375 14573 0ustar00joelstaff000000000000Revision history for Perl module Tie::Array::CSV. 0.08 2019-10-07 - Add ability to replace entire row (plicease++) 0.07 2013-03-25 - Fix bug on directly accessing a line beyond the end of the current length 0.06 2013-02-06 - Force Tie::File to use \n as line separator, fixing tests on Win32 (Mithaldu++) - Use Module::Build directly 0.05 2012-02-08 - 'hold_row' logic separated into T::A::CSV::HoldRow subclass - T::A::CSV::HR::Row objects are more aware of their host's activities - Text::CSV object may be passed to constructor - Use Module::Build rather than EU::MM (still using dzil) 0.04 2011-12-18 - 'tie' constructor can accept same options as 'new' - 'col_sep' added as class option (vs simply passthrough) - 'hold_row' option added (deferred row opts) 0.03 2011-10-21 - 'new' constructor added, brings extra calling options 0.02 2011-10-21 - T::A::CSV behaves more like an array in odd cases 0.01 2011-08-12 - Initial release Tie-Array-CSV-0.08/MANIFEST000444000765000024 37613546656375 14404 0ustar00joelstaff000000000000Build.PL Changes lib/Tie/Array/CSV.pm lib/Tie/Array/CSV/HoldRow.pm MANIFEST This list of files META.json META.yml README.pod t/20-basic.t t/30-construction.t t/35-csv_obj.t t/40-empty_row.t t/65-holdrow_singletons.t t/70-holdrow_basic.t t/80-set_row.t Tie-Array-CSV-0.08/META.json000444000765000024 305213546656375 14706 0ustar00joelstaff000000000000{ "abstract" : "A tied array which combines the power of Tie::File and Text::CSV", "author" : [ "Joel Berger" ], "dynamic_config" : 1, "generated_by" : "Module::Build version 0.4224", "license" : [ "perl_5" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : 2 }, "name" : "Tie-Array-CSV", "no_index" : { "file" : [ "README.pod" ] }, "prereqs" : { "configure" : { "requires" : { "Module::Build" : "0.3601" } }, "runtime" : { "requires" : { "Scalar::Util" : "0", "Text::CSV" : "0", "Tie::File" : "0", "perl" : "5.006001" } } }, "provides" : { "Tie::Array::CSV" : { "file" : "lib/Tie/Array/CSV.pm", "version" : "0.08" }, "Tie::Array::CSV::HoldRow" : { "file" : "lib/Tie/Array/CSV/HoldRow.pm" }, "Tie::Array::CSV::HoldRow::Row" : { "file" : "lib/Tie/Array/CSV/HoldRow.pm" }, "Tie::Array::CSV::Row" : { "file" : "lib/Tie/Array/CSV.pm" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "http://github.com/jberger/Tie-Array-CSV/issues" }, "license" : [ "http://dev.perl.org/licenses/" ], "repository" : { "url" : "http://github.com/jberger/Tie-Array-CSV" } }, "version" : "0.08", "x_serialization_backend" : "JSON::PP version 2.97001" } Tie-Array-CSV-0.08/META.yml000444000765000024 201113546656375 14530 0ustar00joelstaff000000000000--- abstract: 'A tied array which combines the power of Tie::File and Text::CSV' author: - 'Joel Berger' build_requires: {} configure_requires: Module::Build: '0.3601' dynamic_config: 1 generated_by: 'Module::Build version 0.4224, CPAN::Meta::Converter version 2.150010' license: perl meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: '1.4' name: Tie-Array-CSV no_index: file: - README.pod provides: Tie::Array::CSV: file: lib/Tie/Array/CSV.pm version: '0.08' Tie::Array::CSV::HoldRow: file: lib/Tie/Array/CSV/HoldRow.pm Tie::Array::CSV::HoldRow::Row: file: lib/Tie/Array/CSV/HoldRow.pm Tie::Array::CSV::Row: file: lib/Tie/Array/CSV.pm requires: Scalar::Util: '0' Text::CSV: '0' Tie::File: '0' perl: '5.006001' resources: bugtracker: http://github.com/jberger/Tie-Array-CSV/issues license: http://dev.perl.org/licenses/ repository: http://github.com/jberger/Tie-Array-CSV version: '0.08' x_serialization_backend: 'CPAN::Meta::YAML version 0.018' Tie-Array-CSV-0.08/README.pod000444000765000024 1307613546656375 14755 0ustar00joelstaff000000000000=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 Christian Walde (Mithaldu) Graham Ollis (plicease) =head1 COPYRIGHT AND LICENSE Copyright (C) 2013 by L and L. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. Tie-Array-CSV-0.08/lib000755000765000024 013546656375 13676 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/lib/Tie000755000765000024 013546656375 14417 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/lib/Tie/Array000755000765000024 013546656375 15475 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/lib/Tie/Array/CSV.pm000444000765000024 2406113546656375 16646 0ustar00joelstaff000000000000package Tie::Array::CSV; use strict; use warnings; our $VERSION = '0.08'; $VERSION = eval $VERSION; use Carp; use Tie::File; use Text::CSV; use Scalar::Util qw/blessed/; use Tie::Array; our @ISA = ('Tie::Array'); sub parse_opts { my $class = shift; croak "Must specify a file" unless @_; my ($file, %opts); # handle one arg as either hashref (of opts) or file if (@_ == 1) { if (ref $_[0] eq 'HASH') { %opts = %{ shift() }; } else { $file = shift; } } # handle file and hashref of opts if (@_ == 2 and ref $_[1] eq 'HASH') { $file = shift; %opts = %{ shift() }; } # handle file before hash of opts if (@_ % 2) { $file = shift; } # handle hash of opts if (@_) { %opts = @_; } # handle file passed has hash(ref) value to 'file' key if (!$file and defined $opts{file}) { $file = delete $opts{file}; } # file wasn't specified as lone arg or as a hash opt croak "Must specify a file" unless $file; # parse specific options if (exists $opts{sep_char}) { $opts{text_csv}{sep_char} = delete $opts{sep_char}; } return ($file, \%opts); } sub new { my $class = shift; my ($file, $opts) = $class->parse_opts(@_); my @self; tie @self, $class, $file, $opts; return \@self; } sub TIEARRAY { my $class = shift; my ($file, $opts) = $class->parse_opts(@_); my @tiefile; tie @tiefile, 'Tie::File', $file, recsep => "\n", %{ $opts->{tie_file} || {} } or croak "Cannot tie file $file"; my $csv; if (blessed $opts->{text_csv} and $opts->{text_csv}->isa('Text::CSV')) { $csv = $opts->{text_csv}; } else { $csv = Text::CSV->new($opts->{text_csv} || {}) or croak "CSV (new) error: " . Text::CSV->error_diag(); } my $self = { file => \@tiefile, csv => $csv, }; bless $self, $class; return $self; } sub FETCH { my $self = shift; my $index = shift; my $line = $self->{file}[$index]; my $rowclass = ref($self) . '::Row'; tie my @line, $rowclass, { file => $self->{file}, line_num => $index, fields => $self->_parse($line), csv => $self->{csv}, }; return \@line; } sub STORE { my $self = shift; my ($index, $value) = @_; $self->{file}[$index] = $self->_combine($value); } sub FETCHSIZE { my $self = shift; return scalar @{ $self->{file} }; } sub STORESIZE { my $self = shift; my $new_size = shift; $#{ $self->{file} } = $new_size - 1; } sub EXISTS { my $self = shift; my ($index) = shift; return exists $self->{file}[$index]; } sub DELETE { my $self = shift; my $index = shift; return $self->SPLICE($index,1); } sub _parse { my $self = shift; my ($line) = @_; $line = '' unless defined $line; return [$self->{csv}->fields] if $self->{csv}->parse($line); croak "CSV parse error: " . $self->{csv}->error_diag; } sub _combine { my $self = shift; my ($value) = @_; return $self->{csv}->string if $self->{csv}->combine( ref $value ? @$value : ($value) ); croak "CSV combine error: " . $self->{csv}->error_diag(); } package Tie::Array::CSV::Row; use Carp; use Tie::Array; our @ISA = ('Tie::Array'); use overload '@{}' => sub{ $_[0]{fields} }; sub TIEARRAY { my $class = shift; my $self = shift; bless $self, $class; return $self; } sub FETCH { my $self = shift; my $index = shift; return $self->{fields}[$index]; } sub STORE { my $self = shift; my ($index, $value) = @_; $self->{fields}[$index] = $value; $self->_update; } sub FETCHSIZE { my $self = shift; return scalar @{ $self->{fields} }; } sub STORESIZE { my $self = shift; my $new_size = shift; my $return = ( $#{ $self->{fields} } = $new_size - 1 ); $self->_update; return $return; } sub SHIFT { my $self = shift; my $value = shift @{ $self->{fields} }; $self->_update; return $value; } sub UNSHIFT { my $self = shift; my $value = shift; unshift @{ $self->{fields} }, $value; $self->_update; return $self->FETCHSIZE(); } sub DELETE { my $self = shift; my $index = shift; my $return = splice @{ $self->{fields} }, $index, 1; $self->_update; return $return; } sub EXISTS { my $self = shift; my $index = shift; return exists $self->{fields}[$index]; } sub _update { my $self = shift; if(@{ $self->{fields} }) { $self->{csv}->combine(@{ $self->{fields} }) or croak "CSV combine error: " . $self->{csv}->error_diag(); $self->{file}[$self->{line_num}] = $self->{csv}->string; } else { $self->{file}[$self->{line_num}] = ''; } } __END__ __POD__ =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 Christian Walde (Mithaldu) Graham Ollis (plicease) =head1 COPYRIGHT AND LICENSE Copyright (C) 2013 by L and L. This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. =cut Tie-Array-CSV-0.08/lib/Tie/Array/CSV000755000765000024 013546656375 16130 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/lib/Tie/Array/CSV/HoldRow.pm000444000765000024 1152013546656375 20220 0ustar00joelstaff000000000000package Tie::Array::CSV::HoldRow; use strict; use warnings; use Carp; use Tie::File; use Text::CSV; use Scalar::Util qw/weaken/; use Tie::Array::CSV; our @ISA = ('Tie::Array::CSV'); # This is essentially the same TIEARRAY method as T::A::CSV, # but initializes active_rows. This isn't strictly necessary, thanks to autoviv sub TIEARRAY { my $class = shift; my $self = $class->SUPER::TIEARRAY(@_); $self->{active_rows} = {}, # rebless bless $self, $class; return $self; } sub FETCH { my $self = shift; my $index = shift; if ($self->{active_rows}{$index}) { return $self->{active_rows}{$index} } my $line_array = $self->SUPER::FETCH($index); weaken( $self->{active_rows}{$index} = $line_array ); return $line_array; } sub STORE { my $self = shift; my ($index, $value) = @_; $self->{file}[$index] = $self->_combine($value); } sub SPLICE { my $self = shift; my $size = $self->FETCHSIZE; my $offset = @_ ? shift : 0; $offset += $size if $offset < 0; my $length = @_ ? shift : $size-$offset; my @replace_rows = map { $self->_combine($_) } @_; ## reindex active_rows ## # assuming removing items my @active_rows = sort { $a <=> $b } grep { defined $self->{active_rows}{$_} } keys %{ $self->{active_rows} }; my $delta = @replace_rows - $length; # if instead adding items if ($length < @replace_rows) { # reverse ot avoid overwriting active items @active_rows = reverse @active_rows; $delta = @replace_rows + $length; } foreach my $index (@active_rows) { # skip lines before those affected next if ($index < $offset); if ($index >= $offset and $index < ($offset + $length)) { #items that are being removed tied(@{$self->{active_rows}{$index}})->{line_num} = undef; } else { #shifting affected items tied(@{$self->{active_rows}{$index}})->{line_num} = $index+$delta; $self->{active_rows}{$index+$delta} = delete $self->{active_rows}{$index}; } } ## end reindexing logic ## my @return = map { $self->_parse($_) } splice(@{ $self->{file} },$offset,$length,@replace_rows); return @return } sub SHIFT { my $self = shift; my ($return) = $self->SPLICE(0,1); return $return; } sub UNSHIFT { scalar shift->SPLICE(0,0,@_) } sub PUSH { my $self = shift; my $i = $self->FETCHSIZE; $self->STORE($i++, shift) while (@_); } sub POP { my $self = shift; my $newsize = $self->FETCHSIZE - 1; my $val; if ($newsize >= 0) { $val = $self->FETCH($newsize); $self->STORESIZE($newsize); } return $val; } sub CLEAR { shift->STORESIZE(0) } sub EXTEND { } package Tie::Array::CSV::HoldRow::Row; use Carp; use Tie::Array::CSV; our @ISA = ('Tie::Array::CSV::Row'); sub TIEARRAY { my $class = shift; my $self = $class->SUPER::TIEARRAY(@_); # rebless bless $self, $class; $self->{need_update} = 0; return $self; } # _update now marks for deferred update sub _update { my $self = shift; $self->{need_update} = 1; } sub _deferred_update { my $self = shift; unless (defined $self->{line_num}) { carp "Attempted to write out from a severed row"; return undef; } $self->SUPER::_update(); } sub DESTROY { my $self = shift; $self->_deferred_update if $self->{need_update} == 1; } __END__ __POD__ =head1 NAME Tie::Array::CSV::HoldRow - A Tie::Array::CSV subclass for deferring row operations =head1 SYNOPSIS use strict; use warnings; use Tie::Array::CSV::HoldRow; tie my @file, 'Tie::Array::CSV::HoldRow', 'filename'; print $file[0][2]; $file[3][5] = "Camel"; =head1 DESCRIPTION This module is an experimental subclass of L, see usage information in that documentation. While the usage is the same, the timing of the file IO is different. As opposed to the base module, the file is not updated while the reference to the row is still in scope. Note that for both modules, the parsed row is still held in memory while the row is in scope, the ONLY difference is that the file reflects changes immediately when C is false. To reiterate, this option only affects file IO, not memory usage. When multiple rows are kept alive/removed/modified there was the possibility that conflicting directives could be given to a single physical line. To combat this possibility, as of version 0.05, all (living) child row objects are made aware of line number changes in the parent (outer array) should these occur. Futher if a row object is alive, but the parent object removes that line, the row object is remains intact, but the links between the row object and parent/file are severed. =head1 SOURCE REPOSITORY L =head1 AUTHOR Joel Berger, Ejoel.a.berger@gmail.comE =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. =cut Tie-Array-CSV-0.08/t000755000765000024 013546656375 13373 5ustar00joelstaff000000000000Tie-Array-CSV-0.08/t/20-basic.t000444000765000024 412313546656375 15215 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use Text::CSV; use_ok( 'Tie::Array::CSV' ); my $test_data = <new(); is_deeply( $parser->getline($fh), ["joel berger", "peon", 1010101 ], "File was updated 1" ); is_deeply( $parser->getline($fh), ["larry wall", "general", 42 ], "File was updated 2" ); is_deeply( $parser->getline($fh), [ "tom christiansen", "major", 1101 ], "File was updated 3" ); } done_testing(); Tie-Array-CSV-0.08/t/30-construction.t000444000765000024 631313546656375 16672 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use Text::CSV; use_ok( 'Tie::Array::CSV' ); my @test_data = ( ['name', 'rank', 'serial number'], ['joel berger', 'plebe', 1010101], ['larry wall', 'general', 1], ['damian conway', 'colonel', 1001], ); sub make_test_string { my $sep = shift || ','; my $string; for (@test_data) { $string .= join($sep, @$_) . "\n"; } return $string; } { # test constructors with only file arguments my ($fh, $file) = tempfile(); print $fh make_test_string(); { tie my @csv, 'Tie::Array::CSV', $fh; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, no args'); } { tie my @csv, 'Tie::Array::CSV', {file => $fh}; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, no args'); } { tie my @csv, 'Tie::Array::CSV', file => $fh; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, no args'); } { my $csv = Tie::Array::CSV->new($fh); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, no args'); } { my $csv = Tie::Array::CSV->new( {file => $fh} ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, no args'); } { my $csv = Tie::Array::CSV->new( file => $fh ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, no args'); } } { # test contructors with sep_char arguments my ($fh, $file) = tempfile(); print $fh make_test_string(';'); { tie my @csv, 'Tie::Array::CSV', $fh, { text_csv => { sep_char => ';' } }; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char'); } { tie my @csv, 'Tie::Array::CSV', $fh, text_csv => { sep_char => ';' }; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char'); } { tie my @csv, 'Tie::Array::CSV', $fh, { sep_char => ';' }; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char'); } { tie my @csv, 'Tie::Array::CSV', $fh, sep_char => ';'; isa_ok( tied(@csv), 'Tie::Array::CSV' ); is_deeply( \@csv, \@test_data, 'tie contructor, opts->text_csv->sep_char'); } { my $csv = Tie::Array::CSV->new($fh, { text_csv => { sep_char => ';' } } ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, opts->text_csv->sep_char'); } { my $csv = Tie::Array::CSV->new($fh, text_csv => { sep_char => ';' } ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char'); } { my $csv = Tie::Array::CSV->new($fh, { sep_char => ';' } ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char'); } { my $csv = Tie::Array::CSV->new($fh, sep_char => ';' ); isa_ok( tied(@$csv), 'Tie::Array::CSV' ); is_deeply( $csv, \@test_data, 'new contructor, text_csv->sep_char'); } } done_testing(); Tie-Array-CSV-0.08/t/35-csv_obj.t000444000765000024 156113546656375 15572 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use_ok( 'Tie::Array::CSV' ); my $test_data = <SUPER::parse(@_); } } { my ($fh, $file) = tempfile(); print $fh $test_data; my $csv_obj = My::Text::CSV->new(); my @csv; ok( tie(@csv, 'Tie::Array::CSV', $fh, {text_csv => $csv_obj}), "Tied CSV" ); is( scalar @csv, 4, "Report correct number of rows" ); is( scalar @{$csv[0]}, 3, "Report correct number of columns" ); ok( $csv_obj->my_parsed_lines, "Text::CSV subclass" ); } done_testing(); Tie-Array-CSV-0.08/t/40-empty_row.t000444000765000024 102113546656375 16155 0ustar00joelstaff000000000000use strict; use warnings; use Test::More; use File::Temp; use Tie::Array::CSV; { my $file = File::Temp->new; tie my @array, 'Tie::Array::CSV', $file; eval { my $tmp = $array[0][0] }; ok ! $@, 'Accessing empty row lives'; } { my $file = File::Temp->new; tie my @array, 'Tie::Array::CSV', $file; eval { $array[0][0] = 1 }; ok ! $@, 'Assigning to empty row lives'; eval { $array[4][0] = 1 }; ok ! $@, 'Assigning to later empty row lives'; is $array[4][0], 1, 'Assignment successful'; } done_testing; Tie-Array-CSV-0.08/t/65-holdrow_singletons.t000444000765000024 335613546656375 20077 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use Text::CSV; my $parser = Text::CSV->new(); use_ok( 'Tie::Array::CSV::HoldRow' ); my $test_data = <{active_rows}{1}, "on destruction of row object, active_row entry is undef"); { # unshifting one item my $row_2 = $csv[2]; unshift(@csv, [qw/ a b c /]); is( tied(@$row_2)->{line_num}, 3, "after unshifting row knows new line number" ); my $row_3 = $csv[3]; is( $row_2 . "", $row_3 . "", "after unshifting still get correct singleton"); } { # splicing my $row_0 = $csv[0]; my $old_row_1 = $csv[1]; my $row_2 = $csv[2]; my ($spliced) = splice(@csv, 1, 1); is( tied(@$row_0)->{line_num}, 0, "after splicing unaffected row line number is unaffected" ); is( tied(@$row_2)->{line_num}, 1, "after splicing affected row knows new line number" ); is( ref $spliced, 'ARRAY', "splice returns an arrayref" ); is( scalar @$spliced, 3, "spliced arrayref is of correct length" ); ok( ! defined tied(@$old_row_1)->{line_num}, "spliced rows have line numbers removed, severing them" ); my $row_1 = $csv[1]; is( $row_2 . "", $row_1 . "", "after splicing still get correct singleton"); } } done_testing(); Tie-Array-CSV-0.08/t/70-holdrow_basic.t000444000765000024 222213546656375 16756 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use Text::CSV; my $parser = Text::CSV->new(); use_ok( 'Tie::Array::CSV::HoldRow' ); my $test_data = < 1 my ($fh, $file) = tempfile(); print $fh $test_data; my @csv; ok( tie(@csv, 'Tie::Array::CSV::HoldRow', $fh), "Tied CSV" ); { my $row = $csv[0]; push @$row, 'favorite color'; seek $fh, 0, 0; is_deeply( $parser->getline($fh), ['name', 'rank', 'serial number' ], "Row is held" ); } seek $fh, 0, 0; is_deeply( $parser->getline($fh), ['name', 'rank', 'serial number', 'favorite color' ], "File is updated after row object goes out of scope" ); } { # hold_row => 0 my ($fh, $file) = tempfile(); print $fh $test_data; my @csv; ok( tie(@csv, 'Tie::Array::CSV', $fh, hold_row => 0), "Tied CSV" ); my $row = $csv[0]; push @$row, 'favorite color'; seek $fh, 0, 0; is_deeply( $parser->getline($fh), ['name', 'rank', 'serial number', 'favorite color' ], "File is updated immmediately" ); } done_testing(); Tie-Array-CSV-0.08/t/80-set_row.t000444000765000024 155613546656375 15633 0ustar00joelstaff000000000000 use strict; use warnings; use Test::More; use File::Temp qw/tempfile/; use Text::CSV; use_ok( 'Tie::Array::CSV' ); my $test_data = <new(); is_deeply( $parser->getline($fh), ["name", "rank", "serial number" ], "File was updated 1" ); is_deeply( $parser->getline($fh), ["plicease", "tinkerer", 47 ], "File was updated 1" ); is_deeply( $parser->getline($fh), [''], "File was updated 2" ); is_deeply( $parser->getline($fh), ["damian conway", "colonel", 1001 ], "File was updated 3" ); } done_testing();