| 1 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||
| 2 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 3 | # Data::Tools::CSV perl module | ||||||||||||||||||||||||||||||||||||||||||||||
| 4 | # Copyright (c) 2013-2024 Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||||||||||||||||||||
| 5 | # <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 6 | # http://cade.noxrun.com/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 7 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 8 | # GPL | ||||||||||||||||||||||||||||||||||||||||||||||
| 9 | # | ||||||||||||||||||||||||||||||||||||||||||||||
| 10 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||
| 11 | package Data::Tools::CSV; | ||||||||||||||||||||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 13 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||||||||||
| 15 | 1 | our $VERSION = '1.60'; | |||||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||||||
| 17 | 1 | our @ISA = qw( Exporter ); | |||||||||||||||||||||||||||||||||||||||||||||
| 18 | 1 | our @EXPORT = qw( | |||||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||||||
| 20 | parse_csv | ||||||||||||||||||||||||||||||||||||||||||||||
| 21 | parse_csv_line | ||||||||||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||||||||||
| 23 | parse_csv_to_hash_array | ||||||||||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||||||||||
| 25 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||||||||||
| 27 | 1 | our %EXPORT_TAGS = ( | |||||||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||||||
| 29 | 'all' => \@EXPORT, | ||||||||||||||||||||||||||||||||||||||||||||||
| 30 | 'none' => [], | ||||||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||||||
| 32 | ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||||||
| 34 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||||||||||
| 37 | sub parse_csv | ||||||||||||||||||||||||||||||||||||||||||||||
| 38 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_csv: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 39 | 5 | my $csv_data = shift(); | |||||||||||||||||||||||||||||||||||||||||||||
| 40 | 5 | my $delim = shift(); | |||||||||||||||||||||||||||||||||||||||||||||
| 41 | 5 | my $strip = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||||||||||
| 43 | 5 | my @csv_data = grep /\S/, split /[\r\n]+/, $csv_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||||||||||
| 45 | # 123,is,testing,"""The"" book, is now",qwerty | ||||||||||||||||||||||||||||||||||||||||||||||
| 46 | 5 | my @res; | |||||||||||||||||||||||||||||||||||||||||||||
| 47 | 5 | for my $line ( @csv_data ) | |||||||||||||||||||||||||||||||||||||||||||||
| 48 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 49 | 10 | push @res, parse_csv_line( $line, $delim, $strip ); | |||||||||||||||||||||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||||||||||
| 52 | 5 | return \@res; | |||||||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||||||
| 55 | sub parse_csv_line | ||||||||||||||||||||||||||||||||||||||||||||||
| 56 | 25 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_csv_line: called CC=10 SCAR=23.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 57 | 25 | my @line = split //, shift(); | |||||||||||||||||||||||||||||||||||||||||||||
| 58 | 25 | ▶ | my $delim = shift() || ','; | ||||||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
shift() || ','
MC/DC100%1/1
shift() || ','
shift()
Truth table100%2/2
A
A= shift()
| |||||||||||||||||||||||||||||||||||||||||||||||
| 59 | 25 | my $strip = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 60 | 25 | my @out; | |||||||||||||||||||||||||||||||||||||||||||||
| 61 | my $fld; | ||||||||||||||||||||||||||||||||||||||||||||||
| 62 | 25 | my $q; | |||||||||||||||||||||||||||||||||||||||||||||
| 63 | 25 | for( @line, undef ) | |||||||||||||||||||||||||||||||||||||||||||||
| 64 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 65 | 261 | ▶ | if( ( $_ eq $delim and $q % 2 == 0 ) or ! defined ) | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$_ eq $delim and $q % 2 == 0 or not defined $_
$_ eq $delim and $q % 2 == 0
MC/DC100%3/3
$_ eq $delim and $q % 2 == 0 or not defined $_
$_ eq $delim
$q % 2 == 0
not defined $_
Truth table80%4/5
A and B or not(C)
A= $_ eq $delimB= $q % 2 == 0C= not defined $_
| |||||||||||||||||||||||||||||||||||||||||||||||
| 66 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 67 | 56 | ▶ | $fld =~ s/^\s*// if $strip; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 68 | 56 | ▶ | $fld =~ s/\s*$// if $strip; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 69 | 56 | ▶ | $fld =~ s/^"(.*?)"$/$1/ and $fld =~ s/""/"/g; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 70 | 56 | push @out, $fld; | |||||||||||||||||||||||||||||||||||||||||||||
| 71 | 56 | $fld = undef; | |||||||||||||||||||||||||||||||||||||||||||||
| 72 | 56 | next; | |||||||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 74 | 205 | ▶ | $q++ if /"/; | ||||||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||||||
| 75 | 205 | $fld .= $_; | |||||||||||||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||||||
| 78 | 25 | return \@out; | |||||||||||||||||||||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||||||
| 81 | #----------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||||||
| 83 | sub parse_csv_to_hash_array | ||||||||||||||||||||||||||||||||||||||||||||||
| 84 | 3 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||||||
|
Subroutine
parse_csv_to_hash_array: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||||||
| 85 | 3 | my $csv_data = shift(); | |||||||||||||||||||||||||||||||||||||||||||||
| 86 | 3 | my $delim = shift(); | |||||||||||||||||||||||||||||||||||||||||||||
| 87 | 3 | my $strip = shift; | |||||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||||||
| 89 | 3 | my @csv_data = grep /\S/, split /[\r\n]+/, $csv_data; | |||||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||||||
| 91 | 3 | my $head = parse_csv_line( shift @csv_data, $delim, $strip ); | |||||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||||||
| 93 | 3 | my @res; | |||||||||||||||||||||||||||||||||||||||||||||
| 94 | 3 | for my $line ( @csv_data ) | |||||||||||||||||||||||||||||||||||||||||||||
| 95 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 96 | 3 | my $line_array = parse_csv_line( $line, $delim, $strip ); | |||||||||||||||||||||||||||||||||||||||||||||
| 97 | 3 | push @res, { map { $_ => shift @$line_array } @$head }; | |||||||||||||||||||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 99 | |||||||||||||||||||||||||||||||||||||||||||||||
| 100 | 3 | return \@res; | |||||||||||||||||||||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||||||
| 103 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||||||
| 105 | =pod | ||||||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||||||
| 108 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||||||
| 110 | Data::Tools::CSV -- compact, pure-perl CSV parsing | ||||||||||||||||||||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||||||||||||||||||||
| 112 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||||||
| 114 | use Data::Tools::CSV qw( :all ); # import all functions | ||||||||||||||||||||||||||||||||||||||||||||||
| 115 | use Data::Tools::CSV; # the same as :all :) | ||||||||||||||||||||||||||||||||||||||||||||||
| 116 | use Data::Tools::CSV qw( :none ); # do not import anything | ||||||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||||||
| 118 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||||||
| 120 | my $array_of_arrays = parse_csv( $csv_data_string ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 121 | my @single_line = parse_csv_line( $single_csv_line ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 122 | |||||||||||||||||||||||||||||||||||||||||||||||
| 123 | while( <$fh> ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 124 | { | ||||||||||||||||||||||||||||||||||||||||||||||
| 125 | parse_csv_line( $_ ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 126 | ... | ||||||||||||||||||||||||||||||||||||||||||||||
| 127 | } | ||||||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||||||
| 129 | # hash keys names are mapped from the first line of $csv_data (head) | ||||||||||||||||||||||||||||||||||||||||||||||
| 130 | my @array_of_hashes = parse_csv_to_hash_array( $csv_data ); | ||||||||||||||||||||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||||||||||||||||||||
| 132 | # -------------------------------------------------------------------------- | ||||||||||||||||||||||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||||||||||||||||||||
| 134 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||||||||||||||||||||
| 136 | In all functions the '$delim' argument is optional and sets the delimiter to | ||||||||||||||||||||||||||||||||||||||||||||||
| 137 | be used. Default one is comma ',' (accordingly to RFC4180, see below). | ||||||||||||||||||||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||||||||||||||||||||
| 139 | In all functions the '$strip' argument should be true ("1" or non-zero string) | ||||||||||||||||||||||||||||||||||||||||||||||
| 140 | to strip data from leading and trailing whitespace. If leading or trailing | ||||||||||||||||||||||||||||||||||||||||||||||
| 141 | whitespace is required but stripping is needed to pad visually the data then | ||||||||||||||||||||||||||||||||||||||||||||||
| 142 | actual data must be quoted: | ||||||||||||||||||||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||||||||||||||||||||
| 144 | NAME, TEL | ||||||||||||||||||||||||||||||||||||||||||||||
| 145 | jim, 123 | ||||||||||||||||||||||||||||||||||||||||||||||
| 146 | boo, " 413 " | ||||||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||||||
| 148 | second field will be [TEL] and data will be [123] and [ 413 ]. | ||||||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||||||
| 150 | Unfortunately, for keeping API simple, using stripping will require $delim. | ||||||||||||||||||||||||||||||||||||||||||||||
| 151 | However $delim may be undef to use default delimiter. | ||||||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||||||
| 153 | =head2 parse_csv( $csv_data_string, $delim, $strip ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 154 | |||||||||||||||||||||||||||||||||||||||||||||||
| 155 | Parses multi-line CSV text and returnsh hashref to array of arrays. | ||||||||||||||||||||||||||||||||||||||||||||||
| 156 | |||||||||||||||||||||||||||||||||||||||||||||||
| 157 | =head2 parse_csv_line( $single_csv_line, $delim, $strip ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||||||||||
| 159 | Parses single line CSV data and returns list of parsed fields' data. | ||||||||||||||||||||||||||||||||||||||||||||||
| 160 | This function will NOT strip trailing CR/LFs. However, parse_csv() and | ||||||||||||||||||||||||||||||||||||||||||||||
| 161 | parse_csv_to_hash_array() will strip CR/LFs. | ||||||||||||||||||||||||||||||||||||||||||||||
| 162 | |||||||||||||||||||||||||||||||||||||||||||||||
| 163 | =head2 parse_csv_to_hash_array( $csv_data, $delim, $strip ) | ||||||||||||||||||||||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||||||||||||||||||||
| 165 | This function uses first line as hash key names to produce array of hashes | ||||||||||||||||||||||||||||||||||||||||||||||
| 166 | for the rest of the data. | ||||||||||||||||||||||||||||||||||||||||||||||
| 167 | |||||||||||||||||||||||||||||||||||||||||||||||
| 168 | NOTE: Lines with more data fields than header will discard extra data fields. | ||||||||||||||||||||||||||||||||||||||||||||||
| 169 | NOTE: Lines with less data fields than header will produce keys with undef values. | ||||||||||||||||||||||||||||||||||||||||||||||
| 170 | |||||||||||||||||||||||||||||||||||||||||||||||
| 171 | =head1 IMPLEMENTATION DETAILS | ||||||||||||||||||||||||||||||||||||||||||||||
| 172 | |||||||||||||||||||||||||||||||||||||||||||||||
| 173 | Data::Tools::CSV is compact, pure-perl implementation of a CSV parser of | ||||||||||||||||||||||||||||||||||||||||||||||
| 174 | RFC4180 style CSV files: | ||||||||||||||||||||||||||||||||||||||||||||||
| 175 | |||||||||||||||||||||||||||||||||||||||||||||||
| 176 | https://www.ietf.org/rfc/rfc4180.txt | ||||||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||||||
| 178 | RFC4180 says: | ||||||||||||||||||||||||||||||||||||||||||||||
| 179 | |||||||||||||||||||||||||||||||||||||||||||||||
| 180 | * lines are CRLF delimited, however CR or LF-only are accepted as well. | ||||||||||||||||||||||||||||||||||||||||||||||
| 181 | * whitespace is data, will not be stripped (2.4). | ||||||||||||||||||||||||||||||||||||||||||||||
| 182 | * whitespace and delimiters can be quoted with double quotes ("). | ||||||||||||||||||||||||||||||||||||||||||||||
| 183 | * quotes in quoted text should be doubled ("") as escaping. | ||||||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||||||
| 185 | Any empty data (i.e. two delimiters without data between) will produce undef | ||||||||||||||||||||||||||||||||||||||||||||||
| 186 | value. However there are quotes with nothing inside the result will be empty | ||||||||||||||||||||||||||||||||||||||||||||||
| 187 | string (not undef!). | ||||||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||||||
| 189 | =head1 KNOWN ISSUES | ||||||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||||||
| 191 | This implementation does not support multiline fields (lines split), | ||||||||||||||||||||||||||||||||||||||||||||||
| 192 | as described in RFC4180, (2.6). | ||||||||||||||||||||||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||||||||||||||||||||
| 194 | Delimiter of "0" is silently converted to ",". | ||||||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||||||
| 196 | There is no much error handling. However the code makes reasonable effort | ||||||||||||||||||||||||||||||||||||||||||||||
| 197 | to handle properly all the data provided. This may seem vague but the CSV | ||||||||||||||||||||||||||||||||||||||||||||||
| 198 | format itself is vague :) | ||||||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||||||
| 200 | =head1 FEEDBACK | ||||||||||||||||||||||||||||||||||||||||||||||
| 201 | |||||||||||||||||||||||||||||||||||||||||||||||
| 202 | Please, report any bugs or missing features as long as they follow RFC4180. | ||||||||||||||||||||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||||||||||||||||||||
| 204 | =head1 GITHUB REPOSITORY | ||||||||||||||||||||||||||||||||||||||||||||||
| 205 | |||||||||||||||||||||||||||||||||||||||||||||||
| 206 | git@github.com:cade-vs/perl-data-tools.git | ||||||||||||||||||||||||||||||||||||||||||||||
| 207 | |||||||||||||||||||||||||||||||||||||||||||||||
| 208 | git clone git://github.com/cade-vs/perl-data-tools.git | ||||||||||||||||||||||||||||||||||||||||||||||
| 209 | |||||||||||||||||||||||||||||||||||||||||||||||
| 210 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||||||
| 211 | |||||||||||||||||||||||||||||||||||||||||||||||
| 212 | Vladi Belperchinov-Shabanski "Cade" | ||||||||||||||||||||||||||||||||||||||||||||||
| 213 | <cade@noxrun.com> <cade@bis.bg> <cade@cpan.org> | ||||||||||||||||||||||||||||||||||||||||||||||
| 214 | http://cade.noxrun.com/ | ||||||||||||||||||||||||||||||||||||||||||||||
| 215 | |||||||||||||||||||||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||||||||||||||||||||
| 217 | =cut | ||||||||||||||||||||||||||||||||||||||||||||||
| 218 | |||||||||||||||||||||||||||||||||||||||||||||||
| 219 | ############################################################################## | ||||||||||||||||||||||||||||||||||||||||||||||
| 220 | 1 | 1; | |||||||||||||||||||||||||||||||||||||||||||||