1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
|
package 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<Tie::Array::CSV>, 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<hold_row> 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<http://github.com/jberger/Tie-Array-CSV>
=head1 AUTHOR
Joel Berger, E<lt>joel.a.berger@gmail.comE<gt>
=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
|