| 1 | package Tie::Array::CSV::HoldRow; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Tie::File; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | 2 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | 2 | ▶ | use Scalar::Util qw/weaken/; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 2 | ▶ | use Tie::Array::CSV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 2 | our @ISA = ('Tie::Array::CSV'); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | # This is essentially the same TIEARRAY method as T::A::CSV, | ||||||||||||||||||||||||||||
| 17 | # but initializes active_rows. This isn't strictly necessary, thanks to autoviv | ||||||||||||||||||||||||||||
| 18 | 2 | ▶ | sub TIEARRAY { | ||||||||||||||||||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | 2 | my $class = shift; | |||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 2 | my $self = $class->SUPER::TIEARRAY(@_); | |||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | $self->{active_rows} = {}, | ||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # rebless | ||||||||||||||||||||||||||||
| 26 | 2 | bless $self, $class; | |||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||
| 28 | 2 | return $self; | |||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||
| 31 | 9 | ▶ | sub FETCH { | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 32 | 9 | my $self = shift; | |||||||||||||||||||||||||||
| 33 | 9 | my $index = shift; | |||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | 9 | ▶ | if ($self->{active_rows}{$index}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 36 | 3 | return $self->{active_rows}{$index} | |||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 6 | my $line_array = $self->SUPER::FETCH($index); | |||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | weaken( | ||||||||||||||||||||||||||||
| 42 | 6 | $self->{active_rows}{$index} = $line_array | |||||||||||||||||||||||||||
| 43 | ); | ||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||
| 45 | 6 | return $line_array; | |||||||||||||||||||||||||||
| 46 | } | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | 0 | ▶ | sub STORE { | ||||||||||||||||||||||||||
|
Subroutine
STORE: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 49 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 50 | 0 | my ($index, $value) = @_; | |||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | 0 | $self->{file}[$index] = $self->_combine($value); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 2 | ▶ | sub SPLICE { | ||||||||||||||||||||||||||
|
Subroutine
SPLICE: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 56 | 2 | my $self = shift; | |||||||||||||||||||||||||||
| 57 | 2 | my $size = $self->FETCHSIZE; | |||||||||||||||||||||||||||
| 58 | 2 | ▶ | my $offset = @_ ? shift : 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 59 | 2 | ▶ | $offset += $size if $offset < 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 2 | ▶ | my $length = @_ ? shift : $size-$offset; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 2 | my @replace_rows = map { $self->_combine($_) } @_; | |||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | ## reindex active_rows ## | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | # assuming removing items | ||||||||||||||||||||||||||||
| 67 | my @active_rows = | ||||||||||||||||||||||||||||
| 68 | 5 | sort { $a <=> $b } | |||||||||||||||||||||||||||
| 69 | 6 | grep { defined $self->{active_rows}{$_} } | |||||||||||||||||||||||||||
| 70 | 2 | keys %{ $self->{active_rows} }; | |||||||||||||||||||||||||||
| 71 | 2 | my $delta = @replace_rows - $length; | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | # if instead adding items | ||||||||||||||||||||||||||||
| 74 | 2 | ▶ | if ($length < @replace_rows) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | # reverse ot avoid overwriting active items | ||||||||||||||||||||||||||||
| 76 | 1 | @active_rows = reverse @active_rows; | |||||||||||||||||||||||||||
| 77 | 1 | $delta = @replace_rows + $length; | |||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 2 | foreach my $index (@active_rows) { | |||||||||||||||||||||||||||
| 81 | # skip lines before those affected | ||||||||||||||||||||||||||||
| 82 | 5 | ▶ | next if ($index < $offset); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 4 | ▶ | if ($index >= $offset and $index < ($offset + $length)) { #items that are being removed | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$index >= $offset and $index < $offset + $length
MC/DC50%1/2
$index >= $offset and $index < $offset + $length
$index >= $offset
$index < $offset + $length
Truth table66%2/3
A and B
A= $index >= $offsetB= $index < $offset + $length
| |||||||||||||||||||||||||||||
| 85 | 1 | tied(@{$self->{active_rows}{$index}})->{line_num} = undef; | |||||||||||||||||||||||||||
| 86 | } else { #shifting affected items | ||||||||||||||||||||||||||||
| 87 | 3 | tied(@{$self->{active_rows}{$index}})->{line_num} = $index+$delta; | |||||||||||||||||||||||||||
| 88 | 3 | $self->{active_rows}{$index+$delta} = delete $self->{active_rows}{$index}; | |||||||||||||||||||||||||||
| 89 | } | ||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||
| 92 | ## end reindexing logic ## | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | 1 | my @return = map { $self->_parse($_) } | |||||||||||||||||||||||||||
| 95 | 2 | splice(@{ $self->{file} },$offset,$length,@replace_rows); | |||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | return @return | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | 2 | } | |||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 0 | ▶ | sub SHIFT { | ||||||||||||||||||||||||||
|
Subroutine
SHIFT: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 102 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 103 | 0 | my ($return) = $self->SPLICE(0,1); | |||||||||||||||||||||||||||
| 104 | 0 | return $return; | |||||||||||||||||||||||||||
| 105 | } | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | 1 | ▶ | sub UNSHIFT { scalar shift->SPLICE(0,0,@_) } | ||||||||||||||||||||||||||
|
Subroutine
UNSHIFT: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||
| 109 | 0 | ▶ | sub PUSH { | ||||||||||||||||||||||||||
|
Subroutine
PUSH: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 110 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 111 | 0 | my $i = $self->FETCHSIZE; | |||||||||||||||||||||||||||
| 112 | 0 | ▶ | $self->STORE($i++, shift) while (@_); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | 0 | ▶ | sub POP { | ||||||||||||||||||||||||||
|
Subroutine
POP: not called CC=2 SCAR=17.9
| |||||||||||||||||||||||||||||
| 116 | 0 | my $self = shift; | |||||||||||||||||||||||||||
| 117 | 0 | my $newsize = $self->FETCHSIZE - 1; | |||||||||||||||||||||||||||
| 118 | 0 | my $val; | |||||||||||||||||||||||||||
| 119 | 0 | ▶ | if ($newsize >= 0) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | 0 | $val = $self->FETCH($newsize); | |||||||||||||||||||||||||||
| 121 | 0 | $self->STORESIZE($newsize); | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | 0 | return $val; | |||||||||||||||||||||||||||
| 124 | } | ||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub CLEAR { shift->STORESIZE(0) } | ||||||||||||||||||||||||||
|
Subroutine
CLEAR: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 127 | |||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub EXTEND { } | ||||||||||||||||||||||||||
|
Subroutine
EXTEND: not called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | package Tie::Array::CSV::HoldRow::Row; | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 2 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | 2 | ▶ | use Tie::Array::CSV; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 135 | 2 | our @ISA = ('Tie::Array::CSV::Row'); | |||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | 6 | ▶ | sub TIEARRAY { | ||||||||||||||||||||||||||
|
Subroutine
TIEARRAY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 138 | 6 | my $class = shift; | |||||||||||||||||||||||||||
| 139 | 6 | my $self = $class->SUPER::TIEARRAY(@_); | |||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | # rebless | ||||||||||||||||||||||||||||
| 142 | 6 | bless $self, $class; | |||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 6 | $self->{need_update} = 0; | |||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||
| 146 | 6 | return $self; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | |||||||||||||||||||||||||||||
| 149 | # _update now marks for deferred update | ||||||||||||||||||||||||||||
| 150 | 1 | ▶ | sub _update { | ||||||||||||||||||||||||||
|
Subroutine
_update: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 151 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 152 | 1 | $self->{need_update} = 1; | |||||||||||||||||||||||||||
| 153 | } | ||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||
| 155 | 1 | ▶ | sub _deferred_update { | ||||||||||||||||||||||||||
|
Subroutine
_deferred_update: called CC=2 SCAR=8.4
| |||||||||||||||||||||||||||||
| 156 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 157 | 1 | ▶ | unless (defined $self->{line_num}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 158 | 0 | carp "Attempted to write out from a severed row"; | |||||||||||||||||||||||||||
| 159 | 0 | return undef; | |||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | |||||||||||||||||||||||||||||
| 162 | 1 | $self->SUPER::_update(); | |||||||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 6 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||
| 166 | 6 | my $self = shift; | |||||||||||||||||||||||||||
| 167 | 6 | ▶ | $self->_deferred_update if $self->{need_update} == 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | __END__ | ||||||||||||||||||||||||||||