| 1 | package Text::CSV::Track; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 3 | our $VERSION = '1.00'; | |||||||||||||||||||||||||||
| 4 | 3 | ▶ | use 5.006; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 3 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | 3 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 3 | ▶ | use base qw(Class::Accessor::Fast); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | 3 | __PACKAGE__->mk_accessors( | |||||||||||||||||||||||||||
| 11 | qw( | ||||||||||||||||||||||||||||
| 12 | file_name | ||||||||||||||||||||||||||||
| 13 | _file_fh | ||||||||||||||||||||||||||||
| 14 | _rh_value_of | ||||||||||||||||||||||||||||
| 15 | _lazy_init | ||||||||||||||||||||||||||||
| 16 | ignore_missing_file | ||||||||||||||||||||||||||||
| 17 | full_time_lock | ||||||||||||||||||||||||||||
| 18 | auto_store | ||||||||||||||||||||||||||||
| 19 | _no_lock | ||||||||||||||||||||||||||||
| 20 | ignore_badly_formated | ||||||||||||||||||||||||||||
| 21 | _csv_format | ||||||||||||||||||||||||||||
| 22 | header_lines | ||||||||||||||||||||||||||||
| 23 | footer_lines | ||||||||||||||||||||||||||||
| 24 | hash_names | ||||||||||||||||||||||||||||
| 25 | single_column | ||||||||||||||||||||||||||||
| 26 | trunc | ||||||||||||||||||||||||||||
| 27 | replace_new_lines_with | ||||||||||||||||||||||||||||
| 28 | identificator_column_number | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | sep_char | ||||||||||||||||||||||||||||
| 31 | escape_char | ||||||||||||||||||||||||||||
| 32 | quote_char | ||||||||||||||||||||||||||||
| 33 | always_quote | ||||||||||||||||||||||||||||
| 34 | binary | ||||||||||||||||||||||||||||
| 35 | type | ||||||||||||||||||||||||||||
| 36 | ) | ||||||||||||||||||||||||||||
| 37 | ); | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | 3 | ▶ | use FindBin; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | 3 | ▶ | use Text::CSV_XS; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 3 | ▶ | use Carp::Clan; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | 3 | ▶ | use English qw(-no_match_vars); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 44 | 3 | ▶ | use Fcntl ':flock'; # import LOCK_* constants | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | 3 | ▶ | use Fcntl ':seek'; # import SEEK_* constants | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 46 | 3 | ▶ | use List::MoreUtils qw { first_index }; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | 3 | ▶ | use IO::Handle; #must be because file_fh->input_line_number function | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 62 | ▶ | sub new { | ||||||||||||||||||||||||||
|
Subroutine
new: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 51 | 62 | my $class = shift; | |||||||||||||||||||||||||||
| 52 | 62 | my $ra_arg = shift; | |||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | #build object from parent | ||||||||||||||||||||||||||||
| 55 | 62 | my $self = $class->SUPER::new($ra_arg); | |||||||||||||||||||||||||||
| 56 | |||||||||||||||||||||||||||||
| 57 | #create empty pointers | ||||||||||||||||||||||||||||
| 58 | 62 | $self->{_rh_value_of} = {}; | |||||||||||||||||||||||||||
| 59 | 62 | ▶ | $self->{header_lines} = [] if not defined $self->{header_lines}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 60 | 62 | ▶ | $self->{footer_lines} = [] if not defined $self->{footer_lines}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 62 | return $self; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||
| 65 | 1557 | ▶ | sub output_row_of { | ||||||||||||||||||||||||||
|
Subroutine
output_row_of: called CC=12 SCAR=25.0
Pod
documented
| |||||||||||||||||||||||||||||
| 66 | 1557 | my $self = shift; | |||||||||||||||||||||||||||
| 67 | 1557 | my $identificator = shift; | |||||||||||||||||||||||||||
| 68 | 1557 | my $type = shift; | |||||||||||||||||||||||||||
| 69 | |||||||||||||||||||||||||||||
| 70 | #combine values for csv file | ||||||||||||||||||||||||||||
| 71 | 1557 | my @fields = $self->value_of($identificator); | |||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | #removed entry | ||||||||||||||||||||||||||||
| 74 | 1557 | ▶ | return undef if (@fields == 1) and (not defined $fields[0]); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@fields == 1 and not defined $fields[0]
MC/DC100%2/2
@fields == 1 and not defined $fields[0]
@fields == 1
not defined $fields[0]
Truth table100%3/3
A and not(B)
A= @fields == 1B= not defined $fields[0]
| |||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | #if in single column mode remove '1' from the start of the fields | ||||||||||||||||||||||||||||
| 77 | 1553 | ▶ | shift(@fields) if $self->single_column; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | #remove new lines | ||||||||||||||||||||||||||||
| 80 | 1553 | ▶ | if (defined $self->replace_new_lines_with) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 1553 | my $replacement = $self->replace_new_lines_with; | |||||||||||||||||||||||||||
| 82 | 1553 | foreach my $field (@fields) { | |||||||||||||||||||||||||||
| 83 | 1558 | ▶ | next if not defined $field; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | 1556 | $field =~ s{[\n\r]+}{$replacement}sg; | |||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | |||||||||||||||||||||||||||||
| 88 | #add identificator to the values | ||||||||||||||||||||||||||||
| 89 | 1553 | splice(@fields, $self->identificator_column_number, 0, $identificator); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1553 | ▶ | if ($type eq 'csv') { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 92 | 1549 | ▶ | croak "invalid value to store to an csv file - ", $self->_csv_format->error_input(),"\n" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | if (not $self->_csv_format->combine(@fields)); | ||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||
| 95 | 1548 | return $self->_csv_format->string(); | |||||||||||||||||||||||||||
| 96 | } | ||||||||||||||||||||||||||||
| 97 | elsif ($type eq 'xml') { | ||||||||||||||||||||||||||||
| 98 | 4 | my $xml_line = '<Row>'."\n"; | |||||||||||||||||||||||||||
| 99 | 4 | $xml_line .= ' <Cell><Data ss:Type="String">'.$identificator.'</Data></Cell>'."\n"; | |||||||||||||||||||||||||||
| 100 | 4 | foreach my $col_value ($self->value_of($identificator)) { | |||||||||||||||||||||||||||
| 101 | 4 | ▶ | $col_value = '' if not defined $col_value; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 102 | 4 | $xml_line .= ' <Cell><Data ss:Type="String">'.$col_value.'</Data></Cell>'."\n"; | |||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | 4 | $xml_line .= '</Row>'; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | 4 | return $xml_line; | |||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||
| 108 | else { | ||||||||||||||||||||||||||||
| 109 | 0 | croak "unknow output format"; | |||||||||||||||||||||||||||
| 110 | } | ||||||||||||||||||||||||||||
| 111 | } | ||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||
| 114 | 1 | ▶ | sub csv_line_of { | ||||||||||||||||||||||||||
|
Subroutine
csv_line_of: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 115 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 116 | 1 | my $identificator = shift; | |||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||
| 118 | 1 | return $self->output_row_of($identificator, 'csv'); | |||||||||||||||||||||||||||
| 119 | } | ||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 6948 | ▶ | sub value_of { | ||||||||||||||||||||||||||
|
Subroutine
value_of: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||||||||||||||||||
| 123 | 6948 | my $self = shift; | |||||||||||||||||||||||||||
| 124 | 6948 | my $identificator = shift; | |||||||||||||||||||||||||||
| 125 | 6948 | my $is_set = 0; #by default get | |||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | #if we have one more parameter then it is set | ||||||||||||||||||||||||||||
| 128 | 6948 | my $value; | |||||||||||||||||||||||||||
| 129 | 6948 | ▶ | if (@_ >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | 2733 | $is_set = 1; | |||||||||||||||||||||||||||
| 131 | 2733 | $value = \@_; | |||||||||||||||||||||||||||
| 132 | } | ||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | #check if we have identificator | ||||||||||||||||||||||||||||
| 135 | 6948 | ▶ | return if not $identificator; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||
| 137 | #value_of hash | ||||||||||||||||||||||||||||
| 138 | 6948 | my $rh_value_of = $self->_rh_value_of; | |||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||
| 140 | #lazy initialization is needed for get | ||||||||||||||||||||||||||||
| 141 | 6948 | ▶ | $self->_init() if not $is_set; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | #switch between set and get variant | ||||||||||||||||||||||||||||
| 144 | #set | ||||||||||||||||||||||||||||
| 145 | 6947 | ▶ | if ($is_set) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 146 | 2733 | $rh_value_of->{$identificator} = $value; | |||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||
| 148 | #get | ||||||||||||||||||||||||||||
| 149 | else { | ||||||||||||||||||||||||||||
| 150 | 4214 | ▶ | return undef if not defined $rh_value_of->{$identificator}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | #if we have more then one field return array | ||||||||||||||||||||||||||||
| 153 | 1642 | ▶ | if (@{$rh_value_of->{$identificator}} > 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 154 | 21 | return @{$rh_value_of->{$identificator}}; | |||||||||||||||||||||||||||
| 155 | } | ||||||||||||||||||||||||||||
| 156 | #otherwise return one and only value from array as scallar | ||||||||||||||||||||||||||||
| 157 | else { | ||||||||||||||||||||||||||||
| 158 | 1621 | return ${$rh_value_of->{$identificator}}[0]; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | } | ||||||||||||||||||||||||||||
| 161 | } | ||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||
| 164 | 8 | ▶ | sub hash_of { | ||||||||||||||||||||||||||
|
Subroutine
hash_of: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||||||||||||||||
| 165 | 8 | my $self = shift; | |||||||||||||||||||||||||||
| 166 | 8 | my $identificator = shift; | |||||||||||||||||||||||||||
| 167 | 8 | my $is_set = 0; #by default get | |||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||
| 169 | 8 | ▶ | croak "'hash_names' parameter not set" if not defined $self->hash_names; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 170 | 8 | my @hash_names = @{$self->hash_names}; | |||||||||||||||||||||||||||
| 171 | 8 | my @fields = $self->value_of($identificator); | |||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||
| 173 | #if we have one more parameter then it is set | ||||||||||||||||||||||||||||
| 174 | 8 | my $rh; | |||||||||||||||||||||||||||
| 175 | 8 | ▶ | if (@_ >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 3 | $is_set = 1; | |||||||||||||||||||||||||||
| 177 | 3 | $rh = shift; | |||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||
| 179 | 3 | ▶ | croak "not a hash reference as set argument" if ref $rh ne 'HASH'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | |||||||||||||||||||||||||||||
| 182 | 8 | ▶ | if ($is_set) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 3 | foreach my $key (keys %{$rh}) { | |||||||||||||||||||||||||||
| 184 | 5 | ▶ | my $index = first_index { $_ eq $key } @hash_names; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 185 | |||||||||||||||||||||||||||||
| 186 | 5 | ▶ | croak "no such hash key name '$key'" if $index == -1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 187 | |||||||||||||||||||||||||||||
| 188 | 4 | $fields[$index] = $rh->{$key}; | |||||||||||||||||||||||||||
| 189 | } | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | #save back the fields | ||||||||||||||||||||||||||||
| 192 | 2 | $self->value_of($identificator, @fields); | |||||||||||||||||||||||||||
| 193 | } | ||||||||||||||||||||||||||||
| 194 | else { | ||||||||||||||||||||||||||||
| 195 | 5 | my %hash; | |||||||||||||||||||||||||||
| 196 | 5 | foreach my $name (@hash_names) { | |||||||||||||||||||||||||||
| 197 | 22 | $hash{$name} = shift @fields; | |||||||||||||||||||||||||||
| 198 | } | ||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||
| 200 | 5 | return \%hash; | |||||||||||||||||||||||||||
| 201 | } | ||||||||||||||||||||||||||||
| 202 | } | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | |||||||||||||||||||||||||||||
| 205 | 1 | ▶ | sub store_as_xml { | ||||||||||||||||||||||||||
|
Subroutine
store_as_xml: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 206 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||
| 208 | 1 | return $self->store(1); | |||||||||||||||||||||||||||
| 209 | } | ||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||
| 212 | 36 | ▶ | sub store { | ||||||||||||||||||||||||||
|
Subroutine
store: called CC=11 SCAR=24.1
Pod
documented
| |||||||||||||||||||||||||||||
| 213 | 36 | my $self = shift; | |||||||||||||||||||||||||||
| 214 | 36 | my $store_as_xml = shift; | |||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||
| 216 | #lazy initialization | ||||||||||||||||||||||||||||
| 217 | 36 | $self->_init(); | |||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||
| 219 | #get local variables from self hash | ||||||||||||||||||||||||||||
| 220 | 36 | my $rh_value_of = $self->_rh_value_of; | |||||||||||||||||||||||||||
| 221 | 36 | my $file_name = $self->file_name; | |||||||||||||||||||||||||||
| 222 | 36 | my $full_time_lock = $self->full_time_lock; | |||||||||||||||||||||||||||
| 223 | 36 | my $file_fh = $self->_file_fh; | |||||||||||||||||||||||||||
| 224 | |||||||||||||||||||||||||||||
| 225 | 36 | ▶ | if (not $full_time_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 226 | 36 | ▶ | open($file_fh, "+>>", $file_name) or croak "can't write to file '$file_name' - $OS_ERROR"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 227 | |||||||||||||||||||||||||||||
| 228 | #lock and truncate the access store file | ||||||||||||||||||||||||||||
| 229 | 36 | ▶ | flock($file_fh, LOCK_EX) or croak "can't lock file '$file_name' - $OS_ERROR\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 230 | } | ||||||||||||||||||||||||||||
| 231 | |||||||||||||||||||||||||||||
| 232 | #loop through identificators and store to array only if all works fine file will be overwritten | ||||||||||||||||||||||||||||
| 233 | 36 | my @file_lines; | |||||||||||||||||||||||||||
| 234 | 36 | foreach my $identificator (sort $self->ident_list()) { | |||||||||||||||||||||||||||
| 235 | 1556 | my $file_line; | |||||||||||||||||||||||||||
| 236 | 1556 | ▶ | if (defined $store_as_xml) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 237 | 4 | $file_line = $self->output_row_of($identificator, 'xml'); | |||||||||||||||||||||||||||
| 238 | } | ||||||||||||||||||||||||||||
| 239 | else { | ||||||||||||||||||||||||||||
| 240 | 1552 | $file_line = $self->output_row_of($identificator, 'csv'); | |||||||||||||||||||||||||||
| 241 | } | ||||||||||||||||||||||||||||
| 242 | |||||||||||||||||||||||||||||
| 243 | #skip removed entries | ||||||||||||||||||||||||||||
| 244 | 1555 | ▶ | next if not $file_line; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 245 | |||||||||||||||||||||||||||||
| 246 | 1551 | push(@file_lines, $file_line."\n"); | |||||||||||||||||||||||||||
| 247 | } | ||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||
| 249 | #truncate the file so that we can store new results | ||||||||||||||||||||||||||||
| 250 | 35 | ▶ | truncate($file_fh, 0) or croak "can't truncate file '$file_name' - $OS_ERROR\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 251 | |||||||||||||||||||||||||||||
| 252 | #write header lines | ||||||||||||||||||||||||||||
| 253 | 35 | foreach my $header_line (@{$self->header_lines}) { | |||||||||||||||||||||||||||
| 254 | 31 | print {$file_fh} $header_line, "\n"; | |||||||||||||||||||||||||||
| 255 | } | ||||||||||||||||||||||||||||
| 256 | |||||||||||||||||||||||||||||
| 257 | #write csv lines | ||||||||||||||||||||||||||||
| 258 | 35 | foreach my $line (@file_lines) { | |||||||||||||||||||||||||||
| 259 | #print the line to csv file | ||||||||||||||||||||||||||||
| 260 | 1551 | print {$file_fh} $line; | |||||||||||||||||||||||||||
| 261 | } | ||||||||||||||||||||||||||||
| 262 | |||||||||||||||||||||||||||||
| 263 | #write footer lines | ||||||||||||||||||||||||||||
| 264 | 35 | foreach my $footer_line (@{$self->footer_lines}) { | |||||||||||||||||||||||||||
| 265 | 12 | print {$file_fh} $footer_line, "\n"; | |||||||||||||||||||||||||||
| 266 | } | ||||||||||||||||||||||||||||
| 267 | |||||||||||||||||||||||||||||
| 268 | 35 | close($file_fh); | |||||||||||||||||||||||||||
| 269 | } | ||||||||||||||||||||||||||||
| 270 | |||||||||||||||||||||||||||||
| 271 | #lazy initialization | ||||||||||||||||||||||||||||
| 272 | 4361 | ▶ | sub _init { | ||||||||||||||||||||||||||
|
Subroutine
_init: called CC=32 SCAR=34.7
| |||||||||||||||||||||||||||||
| 273 | 4361 | my $self = shift; | |||||||||||||||||||||||||||
| 274 | |||||||||||||||||||||||||||||
| 275 | 4361 | ▶ | return if $self->_lazy_init; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 276 | |||||||||||||||||||||||||||||
| 277 | #prevent from reexecuting | ||||||||||||||||||||||||||||
| 278 | 62 | $self->_lazy_init(1); | |||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||
| 280 | #default values | ||||||||||||||||||||||||||||
| 281 | 62 | ▶ | $self->replace_new_lines_with('|') if not exists $self->{'replace_new_lines_with'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 282 | 62 | ▶ | $self->binary(1) if not exists $self->{'binary'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 283 | 62 | ▶ | $self->identificator_column_number(0) if not exists $self->{'identificator_column_number'}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 284 | |||||||||||||||||||||||||||||
| 285 | #get local variables from self hash | ||||||||||||||||||||||||||||
| 286 | 62 | my $rh_value_of = $self->_rh_value_of; | |||||||||||||||||||||||||||
| 287 | 62 | my $file_name = $self->file_name; | |||||||||||||||||||||||||||
| 288 | 62 | my $ignore_missing_file = $self->ignore_missing_file; | |||||||||||||||||||||||||||
| 289 | 62 | my $full_time_lock = $self->full_time_lock; | |||||||||||||||||||||||||||
| 290 | 62 | my $_no_lock = $self->_no_lock; | |||||||||||||||||||||||||||
| 291 | 62 | my $header_lines_count; | |||||||||||||||||||||||||||
| 292 | my $header_lines_from_file; | ||||||||||||||||||||||||||||
| 293 | 62 | my $footer_lines_count; | |||||||||||||||||||||||||||
| 294 | 62 | my $footer_lines_from_file; | |||||||||||||||||||||||||||
| 295 | |||||||||||||||||||||||||||||
| 296 | 62 | ▶ | if (ref $self->{header_lines} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 297 | 50 | $header_lines_count = scalar @{$self->header_lines}; | |||||||||||||||||||||||||||
| 298 | 50 | $header_lines_from_file = 0; | |||||||||||||||||||||||||||
| 299 | } | ||||||||||||||||||||||||||||
| 300 | else { | ||||||||||||||||||||||||||||
| 301 | #initialize header_lines with array of empty strings if header_lines is number | ||||||||||||||||||||||||||||
| 302 | 12 | $header_lines_count = $self->{header_lines}; | |||||||||||||||||||||||||||
| 303 | 12 | $self->header_lines([ map {""} (1 .. $header_lines_count) ]); | |||||||||||||||||||||||||||
| 304 | 12 | $header_lines_from_file = 1; | |||||||||||||||||||||||||||
| 305 | } | ||||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 62 | ▶ | if (ref $self->{footer_lines} eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 308 | 56 | $footer_lines_count = scalar @{$self->footer_lines}; | |||||||||||||||||||||||||||
| 309 | 56 | $footer_lines_from_file = 0; | |||||||||||||||||||||||||||
| 310 | } | ||||||||||||||||||||||||||||
| 311 | else { | ||||||||||||||||||||||||||||
| 312 | #initialize footer_lines with array of empty strings if footer_lines is number | ||||||||||||||||||||||||||||
| 313 | 6 | $footer_lines_count = $self->{footer_lines}; | |||||||||||||||||||||||||||
| 314 | 6 | $self->footer_lines([ map {""} (1 .. $footer_lines_count) ]); | |||||||||||||||||||||||||||
| 315 | 6 | $footer_lines_from_file = 1; | |||||||||||||||||||||||||||
| 316 | } | ||||||||||||||||||||||||||||
| 317 | |||||||||||||||||||||||||||||
| 318 | #Text::CSV_XS variables | ||||||||||||||||||||||||||||
| 319 | 62 | ▶ | my $sep_char = defined $self->sep_char ? $self->sep_char : q{,}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 320 | 62 | ▶ | my $escape_char = defined $self->escape_char ? $self->escape_char : q{\\}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 321 | 62 | ▶ | my $quote_char = defined $self->quote_char ? $self->quote_char : q{"}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 322 | 62 | my $always_quote = $self->always_quote; | |||||||||||||||||||||||||||
| 323 | 62 | my $binary = $self->binary; | |||||||||||||||||||||||||||
| 324 | |||||||||||||||||||||||||||||
| 325 | #done with initialization if file_name empty | ||||||||||||||||||||||||||||
| 326 | 62 | ▶ | return if not $file_name; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 327 | |||||||||||||||||||||||||||||
| 328 | #define csv format | ||||||||||||||||||||||||||||
| 329 | 59 | $self->_csv_format(Text::CSV_XS->new({ | |||||||||||||||||||||||||||
| 330 | sep_char => $sep_char, | ||||||||||||||||||||||||||||
| 331 | escape_char => $escape_char, | ||||||||||||||||||||||||||||
| 332 | quote_char => $quote_char, | ||||||||||||||||||||||||||||
| 333 | always_quote => $always_quote, | ||||||||||||||||||||||||||||
| 334 | binary => $binary, | ||||||||||||||||||||||||||||
| 335 | })); | ||||||||||||||||||||||||||||
| 336 | |||||||||||||||||||||||||||||
| 337 | #default open mode is reading | ||||||||||||||||||||||||||||
| 338 | 59 | my $open_mode = '<'; | |||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||
| 340 | #if full_time_lock is set do open for writting | ||||||||||||||||||||||||||||
| 341 | 59 | ▶ | if ($full_time_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 342 | 3 | ▶ | if ($ignore_missing_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 343 | 0 | $open_mode = '+>>'; | |||||||||||||||||||||||||||
| 344 | } | ||||||||||||||||||||||||||||
| 345 | else { | ||||||||||||||||||||||||||||
| 346 | 3 | $open_mode = '+<'; | |||||||||||||||||||||||||||
| 347 | } | ||||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | #open file with old stored values and handle error | ||||||||||||||||||||||||||||
| 351 | 59 | my $file_fh; | |||||||||||||||||||||||||||
| 352 | 59 | ▶ | if (not open($file_fh, $open_mode, $file_name)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 353 | 7 | ▶ | if ($ignore_missing_file) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 354 | 6 | $OS_ERROR = undef; | |||||||||||||||||||||||||||
| 355 | 6 | return; | |||||||||||||||||||||||||||
| 356 | } | ||||||||||||||||||||||||||||
| 357 | else { | ||||||||||||||||||||||||||||
| 358 | 1 | croak "can't read file '$file_name' - $OS_ERROR"; | |||||||||||||||||||||||||||
| 359 | } | ||||||||||||||||||||||||||||
| 360 | } | ||||||||||||||||||||||||||||
| 361 | |||||||||||||||||||||||||||||
| 362 | #do exclusive lock if full time lock | ||||||||||||||||||||||||||||
| 363 | 52 | ▶ | if ($full_time_lock) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 364 | 3 | ▶ | flock($file_fh, LOCK_EX) or croak "can't lock file '$file_name' - $OS_ERROR\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 365 | 3 | seek($file_fh, 0, SEEK_SET); | |||||||||||||||||||||||||||
| 366 | } | ||||||||||||||||||||||||||||
| 367 | #internal flag. used from within the same module if file is already locked | ||||||||||||||||||||||||||||
| 368 | elsif ($_no_lock) { | ||||||||||||||||||||||||||||
| 369 | } | ||||||||||||||||||||||||||||
| 370 | #otherwise shared lock is enought | ||||||||||||||||||||||||||||
| 371 | else { | ||||||||||||||||||||||||||||
| 372 | 49 | ▶ | flock($file_fh, LOCK_SH) or croak "can't lock file '$file_name' - $OS_ERROR\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 373 | } | ||||||||||||||||||||||||||||
| 374 | |||||||||||||||||||||||||||||
| 375 | 52 | my $lines_count = 0; | |||||||||||||||||||||||||||
| 376 | 52 | ▶ | $lines_count++ while (<$file_fh>); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 377 | |||||||||||||||||||||||||||||
| 378 | #reset file position | ||||||||||||||||||||||||||||
| 379 | 52 | seek($file_fh, 0, SEEK_SET); | |||||||||||||||||||||||||||
| 380 | 52 | $file_fh->input_line_number(0); | |||||||||||||||||||||||||||
| 381 | |||||||||||||||||||||||||||||
| 382 | #create hash of identificator => 1 | ||||||||||||||||||||||||||||
| 383 | 52 | my %identificator_exist = map { $_ => 1 } $self->ident_list; | |||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||
| 385 | #parse lines and store values in the hash | ||||||||||||||||||||||||||||
| 386 | LINE: | ||||||||||||||||||||||||||||
| 387 | 52 | ▶ | while (my $line = <$file_fh>) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 388 | 2625 | chomp($line); | |||||||||||||||||||||||||||
| 389 | 2625 | $lines_count--; | |||||||||||||||||||||||||||
| 390 | |||||||||||||||||||||||||||||
| 391 | #skip header lines and save them for store() | ||||||||||||||||||||||||||||
| 392 | 2625 | ▶ | if ($header_lines_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 393 | #save header line if not defined | ||||||||||||||||||||||||||||
| 394 | 33 | ▶ | ${$self->header_lines}[$file_fh->input_line_number-1] = $line if $header_lines_from_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 395 | |||||||||||||||||||||||||||||
| 396 | #decrease header lines code so then we will know when there is an end of headers | ||||||||||||||||||||||||||||
| 397 | 33 | $header_lines_count--; | |||||||||||||||||||||||||||
| 398 | |||||||||||||||||||||||||||||
| 399 | 33 | next; | |||||||||||||||||||||||||||
| 400 | } | ||||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | #skip footer lines and save them for store() | ||||||||||||||||||||||||||||
| 403 | 2592 | ▶ | if ($lines_count < $footer_lines_count) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 404 | #save footer lines if not defined | ||||||||||||||||||||||||||||
| 405 | 15 | ▶ | ${$self->footer_lines}[$footer_lines_count - $lines_count - 1] = $line if $footer_lines_from_file; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 406 | |||||||||||||||||||||||||||||
| 407 | 15 | next; | |||||||||||||||||||||||||||
| 408 | } | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | #skip reading of values if in 'trunc' mode | ||||||||||||||||||||||||||||
| 411 | 2577 | ▶ | next if $self->trunc; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 412 | |||||||||||||||||||||||||||||
| 413 | #verify line. if incorrect skip with warning | ||||||||||||||||||||||||||||
| 414 | 2567 | ▶ | if (!$self->_csv_format->parse($line)) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 415 | 3 | my $msg = "badly formated '$file_name' csv line " . $file_fh->input_line_number() . " - '$line'.\n"; | |||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||
| 417 | #by default croak on bad line | ||||||||||||||||||||||||||||
| 418 | 3 | ▶ | croak $msg if not $self->ignore_badly_formated; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 419 | |||||||||||||||||||||||||||||
| 420 | #if ignore_badly_formated_lines is on just print warning | ||||||||||||||||||||||||||||
| 421 | 2 | warn $msg; | |||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||
| 423 | 2 | next; | |||||||||||||||||||||||||||
| 424 | } | ||||||||||||||||||||||||||||
| 425 | |||||||||||||||||||||||||||||
| 426 | #extract fields | ||||||||||||||||||||||||||||
| 427 | 2564 | my @fields = $self->_csv_format->fields(); | |||||||||||||||||||||||||||
| 428 | 2564 | my $identificator = splice(@fields, $self->identificator_column_number, 1); | |||||||||||||||||||||||||||
| 429 | |||||||||||||||||||||||||||||
| 430 | #if in single column mode insert '1' to the fields | ||||||||||||||||||||||||||||
| 431 | 2564 | ▶ | unshift(@fields, 1) if $self->single_column; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||
| 433 | #save present fields | ||||||||||||||||||||||||||||
| 434 | 2564 | my @old_fields = $self->value_of($identificator); | |||||||||||||||||||||||||||
| 435 | |||||||||||||||||||||||||||||
| 436 | #set the value from file | ||||||||||||||||||||||||||||
| 437 | 2564 | $self->value_of($identificator, @fields); | |||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||
| 439 | #set the value from before values from file was read !needed becouse of the strategy! | ||||||||||||||||||||||||||||
| 440 | 2564 | ▶ | $self->value_of($identificator, @old_fields) if $identificator_exist{$identificator}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 441 | } | ||||||||||||||||||||||||||||
| 442 | |||||||||||||||||||||||||||||
| 443 | #if full time lock then store file handle | ||||||||||||||||||||||||||||
| 444 | 51 | ▶ | if ($full_time_lock) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 445 | 3 | $self->_file_fh($file_fh); | |||||||||||||||||||||||||||
| 446 | } | ||||||||||||||||||||||||||||
| 447 | #otherwise release shared lock and close file | ||||||||||||||||||||||||||||
| 448 | else { | ||||||||||||||||||||||||||||
| 449 | 48 | ▶ | flock($file_fh, LOCK_UN) if not $_no_lock; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 450 | 48 | close($file_fh); | |||||||||||||||||||||||||||
| 451 | } | ||||||||||||||||||||||||||||
| 452 | } | ||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||
| 454 | |||||||||||||||||||||||||||||
| 455 | 105 | ▶ | sub ident_list { | ||||||||||||||||||||||||||
|
Subroutine
ident_list: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 456 | 105 | my $self = shift; | |||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||
| 458 | #lazy initialization | ||||||||||||||||||||||||||||
| 459 | 105 | $self->_init(); | |||||||||||||||||||||||||||
| 460 | |||||||||||||||||||||||||||||
| 461 | #get local variables from self hash | ||||||||||||||||||||||||||||
| 462 | 104 | my $rh_value_of = $self->_rh_value_of; | |||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||
| 464 | 104 | return keys %{$rh_value_of}; | |||||||||||||||||||||||||||
| 465 | } | ||||||||||||||||||||||||||||
| 466 | |||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||
| 468 | 130 | ▶ | sub header_lines { | ||||||||||||||||||||||||||
|
Subroutine
header_lines: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 469 | 130 | my $self = shift; | |||||||||||||||||||||||||||
| 470 | |||||||||||||||||||||||||||||
| 471 | #set | ||||||||||||||||||||||||||||
| 472 | 130 | ▶ | if (@_ >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 473 | 12 | $self->{header_lines} = shift; | |||||||||||||||||||||||||||
| 474 | } else | ||||||||||||||||||||||||||||
| 475 | #get | ||||||||||||||||||||||||||||
| 476 | { | ||||||||||||||||||||||||||||
| 477 | #if _header_lines then do lazy init and get the header lines from file | ||||||||||||||||||||||||||||
| 478 | 118 | ▶ | $self->_init if (ref $self->{header_lines} ne 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||
| 480 | 118 | return $self->{header_lines}; | |||||||||||||||||||||||||||
| 481 | } | ||||||||||||||||||||||||||||
| 482 | |||||||||||||||||||||||||||||
| 483 | } | ||||||||||||||||||||||||||||
| 484 | |||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||
| 486 | 119 | ▶ | sub footer_lines { | ||||||||||||||||||||||||||
|
Subroutine
footer_lines: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 487 | 119 | my $self = shift; | |||||||||||||||||||||||||||
| 488 | |||||||||||||||||||||||||||||
| 489 | #set | ||||||||||||||||||||||||||||
| 490 | 119 | ▶ | if (@_ >= 1) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 491 | 9 | $self->{footer_lines} = shift; | |||||||||||||||||||||||||||
| 492 | } else | ||||||||||||||||||||||||||||
| 493 | #get | ||||||||||||||||||||||||||||
| 494 | { | ||||||||||||||||||||||||||||
| 495 | #if footer_lines is not array then do lazy init and get the footer lines from file | ||||||||||||||||||||||||||||
| 496 | 110 | ▶ | $self->_init if (ref $self->{footer_lines} ne 'ARRAY'); | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 497 | |||||||||||||||||||||||||||||
| 498 | 110 | return $self->{footer_lines}; | |||||||||||||||||||||||||||
| 499 | } | ||||||||||||||||||||||||||||
| 500 | } | ||||||||||||||||||||||||||||
| 501 | |||||||||||||||||||||||||||||
| 502 | |||||||||||||||||||||||||||||
| 503 | 62 | ▶ | sub finish { | ||||||||||||||||||||||||||
|
Subroutine
finish: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||||||||||||||||
| 504 | 62 | my $self = shift; | |||||||||||||||||||||||||||
| 505 | |||||||||||||||||||||||||||||
| 506 | #call store if in auto_store mode | ||||||||||||||||||||||||||||
| 507 | 62 | ▶ | $self->store() if $self->auto_store; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 508 | |||||||||||||||||||||||||||||
| 509 | #get local variables from self hash | ||||||||||||||||||||||||||||
| 510 | 62 | my $file_fh = $self->_file_fh; | |||||||||||||||||||||||||||
| 511 | |||||||||||||||||||||||||||||
| 512 | 62 | ▶ | if (defined $file_fh) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 513 | 3 | close($file_fh); | |||||||||||||||||||||||||||
| 514 | } | ||||||||||||||||||||||||||||
| 515 | |||||||||||||||||||||||||||||
| 516 | 62 | $self->_file_fh(undef); | |||||||||||||||||||||||||||
| 517 | } | ||||||||||||||||||||||||||||
| 518 | |||||||||||||||||||||||||||||
| 519 | 62 | ▶ | sub DESTROY { | ||||||||||||||||||||||||||
|
Subroutine
DESTROY: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 520 | 62 | my $self = shift; | |||||||||||||||||||||||||||
| 521 | |||||||||||||||||||||||||||||
| 522 | 62 | $self->finish(); | |||||||||||||||||||||||||||
| 523 | } | ||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||
| 525 | 3 | 1; | |||||||||||||||||||||||||||
| 526 | |||||||||||||||||||||||||||||
| 527 | __END__ | ||||||||||||||||||||||||||||