| 1 | package Text::ECSV; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.02'; | |||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 1 | ▶ | use base 'Text::CSV_XS', 'Class::Accessor::Fast'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 1 | __PACKAGE__->mk_accessors(qw{ | |||||||||||||||||||||||||||
| 11 | fields_hash | ||||||||||||||||||||||||||||
| 12 | dup_keys_strategy | ||||||||||||||||||||||||||||
| 13 | } | ||||||||||||||||||||||||||||
| 14 | ); | ||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 7 | ▶ | sub field_named { | ||||||||||||||||||||||||||
|
Subroutine
field_named: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||||||||
| 17 | 7 | my $self = shift; | |||||||||||||||||||||||||||
| 18 | 7 | my $name = shift; | |||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 7 | return $self->fields_hash->{$name}; | |||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | 4 | ▶ | sub parse { | ||||||||||||||||||||||||||
|
Subroutine
parse: called CC=6 SCAR=18.5
Pod
documented
| |||||||||||||||||||||||||||||
| 24 | 4 | my $self = shift; | |||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | # reset fields hash | ||||||||||||||||||||||||||||
| 27 | 4 | $self->fields_hash({}); | |||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||
| 29 | # run Text::CSV_XS parse | ||||||||||||||||||||||||||||
| 30 | 4 | my $status = $self->SUPER::parse(@_); | |||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | # if the CSV parsing failed then just return | ||||||||||||||||||||||||||||
| 33 | 4 | ▶ | return $status | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 34 | if not $status; | ||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||
| 36 | # decode the fileds | ||||||||||||||||||||||||||||
| 37 | 4 | foreach my $field ($self->fields) { | |||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | # if we have key value pair | ||||||||||||||||||||||||||||
| 40 | 14 | ▶ | if ($field =~ m/^([^=]+)=(.*)$/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 41 | 14 | my $name = $1; | |||||||||||||||||||||||||||
| 42 | 14 | my $value = $2; | |||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | # if it the second occurence of the same key and we have a strategy use it | ||||||||||||||||||||||||||||
| 45 | # to construct the new value | ||||||||||||||||||||||||||||
| 46 | 14 | ▶ | if ( exists $self->{'fields_hash'}->{$name} | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
exists $self->{"fields_hash"}{$name} and exists $self->{"dup_keys_strategy"}
MC/DC50%1/2
exists $self->{"fields_hash"}{$name} and exists $self->{"dup_keys_strategy"}
exists $self->{"fields_hash"}{$name}
exists $self->{"dup_keys_strategy"}
Truth table66%2/3
A and B
A= exists $self->{"fields_hash"}{$name}B= exists $self->{"dup_keys_strategy"}
| |||||||||||||||||||||||||||||
| 47 | and exists $self->{'dup_keys_strategy'}) { | ||||||||||||||||||||||||||||
| 48 | $value = $self->{'dup_keys_strategy'} | ||||||||||||||||||||||||||||
| 49 | 3 | ->($name, $self->{'fields_hash'}->{$name}, $value); | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | # store value | ||||||||||||||||||||||||||||
| 53 | 14 | $self->{'fields_hash'}->{$name} = $value; | |||||||||||||||||||||||||||
| 54 | } | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | # else fail | ||||||||||||||||||||||||||||
| 57 | else { | ||||||||||||||||||||||||||||
| 58 | 0 | $status = 0; | |||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||
| 60 | # TODO fill error messages | ||||||||||||||||||||||||||||
| 61 | |||||||||||||||||||||||||||||
| 62 | 0 | last; | |||||||||||||||||||||||||||
| 63 | } | ||||||||||||||||||||||||||||
| 64 | } | ||||||||||||||||||||||||||||
| 65 | |||||||||||||||||||||||||||||
| 66 | 4 | return $status; | |||||||||||||||||||||||||||
| 67 | } | ||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||
| 69 | 1 | ▶ | sub combine { | ||||||||||||||||||||||||||
|
Subroutine
combine: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 70 | 1 | my $self = shift; | |||||||||||||||||||||||||||
| 71 | 1 | my @key_values = @_; | |||||||||||||||||||||||||||
| 72 | 1 | my @fields; | |||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||
| 74 | 1 | ▶ | while (@key_values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 3 | push @fields, (shift(@key_values) . '=' . shift(@key_values)); | |||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | 1 | return $self->SUPER::combine(@fields); | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | 1 | 1; | |||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | __END__ | ||||||||||||||||||||||||||||