| 1 | package Text::xSV::Slurp; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||||||
| 3 | 10 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 10 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | |||||||||||||||||||||||||||||||||||||||||||
| 6 | 10 | ▶ | use Carp 'confess', 'cluck'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 10 | ▶ | use Text::CSV; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 10 | ▶ | use IO::String; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 10 | ▶ | use constant HOH_HANDLER_KEY => 0; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 10 | ▶ | use constant HOH_HANDLER_KEY_VALUE_PATH => 1; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 10 | ▶ | use constant HOH_HANDLER_OLD_VALUE => 2; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | 10 | ▶ | use constant HOH_HANDLER_NEW_VALUE => 3; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 10 | ▶ | use constant HOH_HANDLER_LINE_HASH => 4; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 15 | 10 | ▶ | use constant HOH_HANDLER_HOH => 5; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 16 | 10 | ▶ | use constant HOH_HANDLER_SCRATCH_PAD => 6; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 17 | |||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ▶ | use base 'Exporter'; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||||||||||||||||
| 20 | 10 | our @EXPORT = qw/ xsv_slurp /; | |||||||||||||||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||||||||||||||||
| 22 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||||||||||||||||
| 24 | Text::xSV::Slurp - Convert xSV data to common data shapes. | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | =head1 VERSION | ||||||||||||||||||||||||||||||||||||||||||
| 27 | |||||||||||||||||||||||||||||||||||||||||||
| 28 | Version 0.23 | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||||||||||||||||
| 32 | 10 | our $VERSION = '0.23'; | |||||||||||||||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||||||||||||||||
| 34 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | C<Text::xSV::Slurp> converts xSV (typically CSV) data to nested data structures | ||||||||||||||||||||||||||||||||||||||||||
| 37 | of various shapes. It allows both column and row filtering using user defined | ||||||||||||||||||||||||||||||||||||||||||
| 38 | functions. | ||||||||||||||||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||||||||||||||||
| 40 | This brief example creates an array of hashes from a file, where each array | ||||||||||||||||||||||||||||||||||||||||||
| 41 | record corresponds to a line of the file, and each line is represented as a hash | ||||||||||||||||||||||||||||||||||||||||||
| 42 | of header-to-value pairs. | ||||||||||||||||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||||||||||||||||
| 44 | use Text::xSV::Slurp 'xsv_slurp'; | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | my $aoh = xsv_slurp( 'foo.csv' ); | ||||||||||||||||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||||||||||||||||
| 48 | ## if foo.csv contains: | ||||||||||||||||||||||||||||||||||||||||||
| 49 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 50 | ## uid,name | ||||||||||||||||||||||||||||||||||||||||||
| 51 | ## 342,tim | ||||||||||||||||||||||||||||||||||||||||||
| 52 | ## 939,danboo | ||||||||||||||||||||||||||||||||||||||||||
| 53 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 54 | ## then $aoh contains: | ||||||||||||||||||||||||||||||||||||||||||
| 55 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 56 | ## [ | ||||||||||||||||||||||||||||||||||||||||||
| 57 | ## { uid => '342', name => 'tim' }, | ||||||||||||||||||||||||||||||||||||||||||
| 58 | ## { uid => '939', name => 'danboo' }, | ||||||||||||||||||||||||||||||||||||||||||
| 59 | ## ] | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | =head1 FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||||||
| 63 | =head2 C<xsv_slurp()> | ||||||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||||||
| 65 | C<xsv_slurp()> converts xSV (typically CSV) data to nested data structures of | ||||||||||||||||||||||||||||||||||||||||||
| 66 | various shapes. It allows both column and row filtering using user defined | ||||||||||||||||||||||||||||||||||||||||||
| 67 | functions. | ||||||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||||||
| 69 | Option summary: | ||||||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||||||
| 71 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||||||
| 73 | =item * C<file> - file name to be opened | ||||||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||||||
| 75 | =item * C<handle> - file handle to be iterated | ||||||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||||||
| 77 | =item * C<string> - string to be parsed | ||||||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||||||
| 79 | =item * C<shape> - target data structure (C<aoa>, C<aoh>, C<hoa> or C<hoh>) | ||||||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||||||
| 81 | =item * C<col_grep> - skip a subset of columns based on user callback | ||||||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||||||
| 83 | =item * C<row_grep> - skip a subset of rows based on user callback | ||||||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||||||
| 85 | =item * C<key> - xSV string or ARRAY used to build the keys of the C<hoh> shape | ||||||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||||||
| 87 | =item * C<on_store> - redefine how the C<hoh> shape should store values | ||||||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||||||
| 89 | =item * C<on_collide> - redefine how the C<hoh> shape should handle key collisions | ||||||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||||||
| 91 | =item * C<text_csv> - option hash for L<Text::CSV>/L<Text::CSV_XS> constructor | ||||||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||||||
| 93 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 94 | |||||||||||||||||||||||||||||||||||||||||||
| 95 | The C<file>, C<handle> and C<string> options are mutually exclusive. Only one | ||||||||||||||||||||||||||||||||||||||||||
| 96 | source parameter may be passed in each call to C<xsv_slurp()>, otherwise a fatal | ||||||||||||||||||||||||||||||||||||||||||
| 97 | exception will be raised. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | The source can also be provided implicitly, without the associated key, and the | ||||||||||||||||||||||||||||||||||||||||||
| 100 | source type will be guessed by examining the first item in the option list. If | ||||||||||||||||||||||||||||||||||||||||||
| 101 | the item is a reference type, it is treated as a C<handle> source. If the item | ||||||||||||||||||||||||||||||||||||||||||
| 102 | contains a newline or carriage return, it is treated as a C<string> source. If | ||||||||||||||||||||||||||||||||||||||||||
| 103 | the item passes none of the prior tests, it is treated as a C<file> source. | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | ## implicit C<handle> source | ||||||||||||||||||||||||||||||||||||||||||
| 106 | my $aoa = xsv_slurp( \*STDIN, shape => 'aoa' ); | ||||||||||||||||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||||||||||||||||
| 108 | ## implicit C<string> source | ||||||||||||||||||||||||||||||||||||||||||
| 109 | my $aoh = xsv_slurp( "h1,h2\n" . "d1,d2\n" ); | ||||||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||||||
| 111 | ## implicit C<file> source | ||||||||||||||||||||||||||||||||||||||||||
| 112 | my $aoh = xsv_slurp( 'foo.csv' ); | ||||||||||||||||||||||||||||||||||||||||||
| 113 | |||||||||||||||||||||||||||||||||||||||||||
| 114 | The C<shape> parameter supports values of C<aoa>, C<aoh>, C<hoa> or C<hoh>. The | ||||||||||||||||||||||||||||||||||||||||||
| 115 | default shape is C<aoh>. Each shape affects certain parameters differently (see | ||||||||||||||||||||||||||||||||||||||||||
| 116 | below). | ||||||||||||||||||||||||||||||||||||||||||
| 117 | |||||||||||||||||||||||||||||||||||||||||||
| 118 | The C<text_csv> option can be used to control L<Text::CSV>/L<Text::CSV_XS> | ||||||||||||||||||||||||||||||||||||||||||
| 119 | parsing. The given HASH reference is passed to the L<Text::CSV> constructor. If | ||||||||||||||||||||||||||||||||||||||||||
| 120 | the C<text_csv> option is undefined, the default L<Text::CSV> constructor is | ||||||||||||||||||||||||||||||||||||||||||
| 121 | called. For example, to change the separator to a colon, you could do the | ||||||||||||||||||||||||||||||||||||||||||
| 122 | following: | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | my $aoh = xsv_slurp( file => 'foo.csv', | ||||||||||||||||||||||||||||||||||||||||||
| 125 | text_csv => { sep_char => ':' } ); | ||||||||||||||||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||||||||||||||||
| 127 | =head3 aoa | ||||||||||||||||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||||||||||||||||
| 129 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 130 | |||||||||||||||||||||||||||||||||||||||||||
| 131 | example input: | ||||||||||||||||||||||||||||||||||||||||||
| 132 | |||||||||||||||||||||||||||||||||||||||||||
| 133 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 134 | l,m,n | ||||||||||||||||||||||||||||||||||||||||||
| 135 | p,q,r | ||||||||||||||||||||||||||||||||||||||||||
| 136 | |||||||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||||||
| 138 | example data structure: | ||||||||||||||||||||||||||||||||||||||||||
| 139 | |||||||||||||||||||||||||||||||||||||||||||
| 140 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 141 | [ qw/ h1 h2 h3 / ], | ||||||||||||||||||||||||||||||||||||||||||
| 142 | [ qw/ l m n / ], | ||||||||||||||||||||||||||||||||||||||||||
| 143 | [ qw/ p q r / ], | ||||||||||||||||||||||||||||||||||||||||||
| 144 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 145 | |||||||||||||||||||||||||||||||||||||||||||
| 146 | shape specifics: | ||||||||||||||||||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||||||||||||||||
| 148 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 149 | |||||||||||||||||||||||||||||||||||||||||||
| 150 | =item * C<col_grep> - passed an ARRAY reference of indexes, should return a | ||||||||||||||||||||||||||||||||||||||||||
| 151 | list of indexes to be included | ||||||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||||||
| 153 | =item * C<row_grep> - passed an ARRAY reference of values, should return true or | ||||||||||||||||||||||||||||||||||||||||||
| 154 | false whether the row should be included or not | ||||||||||||||||||||||||||||||||||||||||||
| 155 | |||||||||||||||||||||||||||||||||||||||||||
| 156 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 157 | |||||||||||||||||||||||||||||||||||||||||||
| 158 | full example: | ||||||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||||||
| 160 | ## - convert xSV example to an array of arrays | ||||||||||||||||||||||||||||||||||||||||||
| 161 | ## - include only rows containing values matching /[nr]/ | ||||||||||||||||||||||||||||||||||||||||||
| 162 | ## - include only the first and last columns | ||||||||||||||||||||||||||||||||||||||||||
| 163 | |||||||||||||||||||||||||||||||||||||||||||
| 164 | my $aoa = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 165 | shape => 'aoa', | ||||||||||||||||||||||||||||||||||||||||||
| 166 | col_grep => sub { return @( shift() }[0,-1] }, | ||||||||||||||||||||||||||||||||||||||||||
| 167 | row_grep => sub { return grep /[nr]/, @{ $_[0] } }, | ||||||||||||||||||||||||||||||||||||||||||
| 168 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||||||||||||||||
| 170 | ## $aoa contains: | ||||||||||||||||||||||||||||||||||||||||||
| 171 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 172 | ## [ | ||||||||||||||||||||||||||||||||||||||||||
| 173 | ## [ 'l', 'n' ], | ||||||||||||||||||||||||||||||||||||||||||
| 174 | ## [ 'p', 'r' ], | ||||||||||||||||||||||||||||||||||||||||||
| 175 | ## ] | ||||||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||||||
| 177 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 178 | |||||||||||||||||||||||||||||||||||||||||||
| 179 | =head3 aoh | ||||||||||||||||||||||||||||||||||||||||||
| 180 | |||||||||||||||||||||||||||||||||||||||||||
| 181 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 182 | |||||||||||||||||||||||||||||||||||||||||||
| 183 | example input: | ||||||||||||||||||||||||||||||||||||||||||
| 184 | |||||||||||||||||||||||||||||||||||||||||||
| 185 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 186 | l,m,n | ||||||||||||||||||||||||||||||||||||||||||
| 187 | p,q,r | ||||||||||||||||||||||||||||||||||||||||||
| 188 | |||||||||||||||||||||||||||||||||||||||||||
| 189 | example data structure: | ||||||||||||||||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||||||||||||||||
| 191 | [ | ||||||||||||||||||||||||||||||||||||||||||
| 192 | { h1 => 'l', h2 => 'm', h3 => 'n' }, | ||||||||||||||||||||||||||||||||||||||||||
| 193 | { h1 => 'p', h2 => 'q', h3 => 'r' }, | ||||||||||||||||||||||||||||||||||||||||||
| 194 | ] | ||||||||||||||||||||||||||||||||||||||||||
| 195 | |||||||||||||||||||||||||||||||||||||||||||
| 196 | shape specifics: | ||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 199 | |||||||||||||||||||||||||||||||||||||||||||
| 200 | =item * C<col_grep> - passed an ARRAY reference of column names, should return a | ||||||||||||||||||||||||||||||||||||||||||
| 201 | list of column names to be included | ||||||||||||||||||||||||||||||||||||||||||
| 202 | |||||||||||||||||||||||||||||||||||||||||||
| 203 | =item * C<row_grep> - passed a HASH reference of column name / value pairs, | ||||||||||||||||||||||||||||||||||||||||||
| 204 | should return true or false whether the row should be | ||||||||||||||||||||||||||||||||||||||||||
| 205 | included or not | ||||||||||||||||||||||||||||||||||||||||||
| 206 | |||||||||||||||||||||||||||||||||||||||||||
| 207 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 208 | |||||||||||||||||||||||||||||||||||||||||||
| 209 | full example: | ||||||||||||||||||||||||||||||||||||||||||
| 210 | |||||||||||||||||||||||||||||||||||||||||||
| 211 | ## - convert xSV example to an array of hashes | ||||||||||||||||||||||||||||||||||||||||||
| 212 | ## - include only rows containing values matching /n/ | ||||||||||||||||||||||||||||||||||||||||||
| 213 | ## - include only the h3 column | ||||||||||||||||||||||||||||||||||||||||||
| 214 | |||||||||||||||||||||||||||||||||||||||||||
| 215 | my $aoh = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 216 | shape => 'aoh', | ||||||||||||||||||||||||||||||||||||||||||
| 217 | col_grep => sub { return 'h3' }, | ||||||||||||||||||||||||||||||||||||||||||
| 218 | row_grep => sub { return grep /n/, values %{ $_[0] } }, | ||||||||||||||||||||||||||||||||||||||||||
| 219 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 220 | |||||||||||||||||||||||||||||||||||||||||||
| 221 | ## $aoh contains: | ||||||||||||||||||||||||||||||||||||||||||
| 222 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 223 | ## [ | ||||||||||||||||||||||||||||||||||||||||||
| 224 | ## { h3 => 'n' }, | ||||||||||||||||||||||||||||||||||||||||||
| 225 | ## ] | ||||||||||||||||||||||||||||||||||||||||||
| 226 | |||||||||||||||||||||||||||||||||||||||||||
| 227 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 228 | |||||||||||||||||||||||||||||||||||||||||||
| 229 | =head3 hoa | ||||||||||||||||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||||||||||||||||
| 231 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 232 | |||||||||||||||||||||||||||||||||||||||||||
| 233 | example input: | ||||||||||||||||||||||||||||||||||||||||||
| 234 | |||||||||||||||||||||||||||||||||||||||||||
| 235 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 236 | l,m,n | ||||||||||||||||||||||||||||||||||||||||||
| 237 | p,q,r | ||||||||||||||||||||||||||||||||||||||||||
| 238 | |||||||||||||||||||||||||||||||||||||||||||
| 239 | example data structure: | ||||||||||||||||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||||||||||||||||
| 241 | { | ||||||||||||||||||||||||||||||||||||||||||
| 242 | h1 => [ qw/ l p / ], | ||||||||||||||||||||||||||||||||||||||||||
| 243 | h2 => [ qw/ m q / ], | ||||||||||||||||||||||||||||||||||||||||||
| 244 | h3 => [ qw/ n r / ], | ||||||||||||||||||||||||||||||||||||||||||
| 245 | } | ||||||||||||||||||||||||||||||||||||||||||
| 246 | |||||||||||||||||||||||||||||||||||||||||||
| 247 | shape specifics: | ||||||||||||||||||||||||||||||||||||||||||
| 248 | |||||||||||||||||||||||||||||||||||||||||||
| 249 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 250 | |||||||||||||||||||||||||||||||||||||||||||
| 251 | =item * C<col_grep> - passed an ARRAY reference of column names, should return a | ||||||||||||||||||||||||||||||||||||||||||
| 252 | list of column names to be included | ||||||||||||||||||||||||||||||||||||||||||
| 253 | |||||||||||||||||||||||||||||||||||||||||||
| 254 | =item * C<row_grep> - passed a HASH reference of column name / value pairs, | ||||||||||||||||||||||||||||||||||||||||||
| 255 | should return true or false whether the row should be | ||||||||||||||||||||||||||||||||||||||||||
| 256 | included or not | ||||||||||||||||||||||||||||||||||||||||||
| 257 | |||||||||||||||||||||||||||||||||||||||||||
| 258 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 259 | |||||||||||||||||||||||||||||||||||||||||||
| 260 | full example: | ||||||||||||||||||||||||||||||||||||||||||
| 261 | |||||||||||||||||||||||||||||||||||||||||||
| 262 | ## - convert xSV example to a hash of arrays | ||||||||||||||||||||||||||||||||||||||||||
| 263 | ## - include only rows containing values matching /n/ | ||||||||||||||||||||||||||||||||||||||||||
| 264 | ## - include only the h3 column | ||||||||||||||||||||||||||||||||||||||||||
| 265 | |||||||||||||||||||||||||||||||||||||||||||
| 266 | my $hoa = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 267 | shape => 'hoa', | ||||||||||||||||||||||||||||||||||||||||||
| 268 | col_grep => sub { return 'h3' }, | ||||||||||||||||||||||||||||||||||||||||||
| 269 | row_grep => sub { return grep /n/, values %{ $_[0] } }, | ||||||||||||||||||||||||||||||||||||||||||
| 270 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 271 | |||||||||||||||||||||||||||||||||||||||||||
| 272 | ## $hoa contains: | ||||||||||||||||||||||||||||||||||||||||||
| 273 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 274 | ## { | ||||||||||||||||||||||||||||||||||||||||||
| 275 | ## h3 => [ qw/ n r / ], | ||||||||||||||||||||||||||||||||||||||||||
| 276 | ## } | ||||||||||||||||||||||||||||||||||||||||||
| 277 | |||||||||||||||||||||||||||||||||||||||||||
| 278 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 279 | |||||||||||||||||||||||||||||||||||||||||||
| 280 | =head3 hoh | ||||||||||||||||||||||||||||||||||||||||||
| 281 | |||||||||||||||||||||||||||||||||||||||||||
| 282 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 283 | |||||||||||||||||||||||||||||||||||||||||||
| 284 | example input: | ||||||||||||||||||||||||||||||||||||||||||
| 285 | |||||||||||||||||||||||||||||||||||||||||||
| 286 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 287 | l,m,n | ||||||||||||||||||||||||||||||||||||||||||
| 288 | p,q,r | ||||||||||||||||||||||||||||||||||||||||||
| 289 | |||||||||||||||||||||||||||||||||||||||||||
| 290 | example data structure (assuming a C<key> of C<'h2,h3'>): | ||||||||||||||||||||||||||||||||||||||||||
| 291 | |||||||||||||||||||||||||||||||||||||||||||
| 292 | { | ||||||||||||||||||||||||||||||||||||||||||
| 293 | m => { n => { h1 => 'l' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 294 | q => { r => { h1 => 'p' } }, | ||||||||||||||||||||||||||||||||||||||||||
| 295 | } | ||||||||||||||||||||||||||||||||||||||||||
| 296 | |||||||||||||||||||||||||||||||||||||||||||
| 297 | shape specifics: | ||||||||||||||||||||||||||||||||||||||||||
| 298 | |||||||||||||||||||||||||||||||||||||||||||
| 299 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 300 | |||||||||||||||||||||||||||||||||||||||||||
| 301 | =item * C<key> - an xSV string or ARRAY specifying the indexing column names | ||||||||||||||||||||||||||||||||||||||||||
| 302 | |||||||||||||||||||||||||||||||||||||||||||
| 303 | =item * C<col_grep> - passed an ARRAY reference of column names, should return a | ||||||||||||||||||||||||||||||||||||||||||
| 304 | list of column names to be included | ||||||||||||||||||||||||||||||||||||||||||
| 305 | |||||||||||||||||||||||||||||||||||||||||||
| 306 | =item * C<row_grep> - passed a HASH reference of column name / value pairs, | ||||||||||||||||||||||||||||||||||||||||||
| 307 | should return true or false whether the row should be | ||||||||||||||||||||||||||||||||||||||||||
| 308 | included or not | ||||||||||||||||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||||||||||||||||
| 310 | =item * C<on_collide> - specify how key collisions should be handled (see | ||||||||||||||||||||||||||||||||||||||||||
| 311 | L</HoH collision handlers>) | ||||||||||||||||||||||||||||||||||||||||||
| 312 | |||||||||||||||||||||||||||||||||||||||||||
| 313 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 314 | |||||||||||||||||||||||||||||||||||||||||||
| 315 | full example: | ||||||||||||||||||||||||||||||||||||||||||
| 316 | |||||||||||||||||||||||||||||||||||||||||||
| 317 | ## - convert xSV example to a hash of hashes | ||||||||||||||||||||||||||||||||||||||||||
| 318 | ## - index using h1 values | ||||||||||||||||||||||||||||||||||||||||||
| 319 | ## - include only rows containing values matching /n/ | ||||||||||||||||||||||||||||||||||||||||||
| 320 | ## - include only the h3 column | ||||||||||||||||||||||||||||||||||||||||||
| 321 | |||||||||||||||||||||||||||||||||||||||||||
| 322 | my $hoh = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 323 | shape => 'hoh', | ||||||||||||||||||||||||||||||||||||||||||
| 324 | key => 'h1', | ||||||||||||||||||||||||||||||||||||||||||
| 325 | col_grep => sub { return 'h3' }, | ||||||||||||||||||||||||||||||||||||||||||
| 326 | row_grep => sub { return grep /n/, values %{ $_[0] } }, | ||||||||||||||||||||||||||||||||||||||||||
| 327 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 328 | |||||||||||||||||||||||||||||||||||||||||||
| 329 | ## $hoh contains: | ||||||||||||||||||||||||||||||||||||||||||
| 330 | ## | ||||||||||||||||||||||||||||||||||||||||||
| 331 | ## { | ||||||||||||||||||||||||||||||||||||||||||
| 332 | ## l => { h3 => 'n' }, | ||||||||||||||||||||||||||||||||||||||||||
| 333 | ## p => { h3 => 'r' }, | ||||||||||||||||||||||||||||||||||||||||||
| 334 | ## } | ||||||||||||||||||||||||||||||||||||||||||
| 335 | |||||||||||||||||||||||||||||||||||||||||||
| 336 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 337 | |||||||||||||||||||||||||||||||||||||||||||
| 338 | =head1 HoH storage handlers | ||||||||||||||||||||||||||||||||||||||||||
| 339 | |||||||||||||||||||||||||||||||||||||||||||
| 340 | Using the C<hoh> shape can result in non-unique C<key> combinations. The default | ||||||||||||||||||||||||||||||||||||||||||
| 341 | action is to simply assign the values to the given slot as they are encountered, | ||||||||||||||||||||||||||||||||||||||||||
| 342 | resulting in any prior values being lost. | ||||||||||||||||||||||||||||||||||||||||||
| 343 | |||||||||||||||||||||||||||||||||||||||||||
| 344 | For example, using C<h1,h2> as the indexing key with the default collision | ||||||||||||||||||||||||||||||||||||||||||
| 345 | handler: | ||||||||||||||||||||||||||||||||||||||||||
| 346 | |||||||||||||||||||||||||||||||||||||||||||
| 347 | $xsv_data = <<EOXSV; | ||||||||||||||||||||||||||||||||||||||||||
| 348 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 349 | 1,2,3 | ||||||||||||||||||||||||||||||||||||||||||
| 350 | 1,2,5 | ||||||||||||||||||||||||||||||||||||||||||
| 351 | EOXSV | ||||||||||||||||||||||||||||||||||||||||||
| 352 | |||||||||||||||||||||||||||||||||||||||||||
| 353 | $hoh = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 354 | shape => 'hoh', | ||||||||||||||||||||||||||||||||||||||||||
| 355 | key => 'h1,h2' | ||||||||||||||||||||||||||||||||||||||||||
| 356 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 357 | |||||||||||||||||||||||||||||||||||||||||||
| 358 | would result in the initial value in the C<h3> column being lost. The resulting | ||||||||||||||||||||||||||||||||||||||||||
| 359 | data structure would only record the C<5> value: | ||||||||||||||||||||||||||||||||||||||||||
| 360 | |||||||||||||||||||||||||||||||||||||||||||
| 361 | { | ||||||||||||||||||||||||||||||||||||||||||
| 362 | 1 => { 2 => { h3 => 5 } }, ## 3 sir! | ||||||||||||||||||||||||||||||||||||||||||
| 363 | } | ||||||||||||||||||||||||||||||||||||||||||
| 364 | |||||||||||||||||||||||||||||||||||||||||||
| 365 | Typically this is not very useful. The user probably wanted to aggregate the | ||||||||||||||||||||||||||||||||||||||||||
| 366 | values in some way. This is where the C<on_store> and C<on_collide> handlers | ||||||||||||||||||||||||||||||||||||||||||
| 367 | come in, allowing the caller to specify how these assignments should be | ||||||||||||||||||||||||||||||||||||||||||
| 368 | handled. | ||||||||||||||||||||||||||||||||||||||||||
| 369 | |||||||||||||||||||||||||||||||||||||||||||
| 370 | The C<on_store> handler is called for each assignment action, while the | ||||||||||||||||||||||||||||||||||||||||||
| 371 | C<on_collide> handler is only called when an actual collision occurs (i.e., | ||||||||||||||||||||||||||||||||||||||||||
| 372 | the nested value path for the current line is the same as a prior line). | ||||||||||||||||||||||||||||||||||||||||||
| 373 | |||||||||||||||||||||||||||||||||||||||||||
| 374 | If instead we wanted to push the values onto an array, we could use the built-in | ||||||||||||||||||||||||||||||||||||||||||
| 375 | C<push> handler for the C<on_store> event as follows: | ||||||||||||||||||||||||||||||||||||||||||
| 376 | |||||||||||||||||||||||||||||||||||||||||||
| 377 | $hoh = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 378 | shape => 'hoh', | ||||||||||||||||||||||||||||||||||||||||||
| 379 | key => 'h1,h2', | ||||||||||||||||||||||||||||||||||||||||||
| 380 | on_store => 'push', | ||||||||||||||||||||||||||||||||||||||||||
| 381 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 382 | |||||||||||||||||||||||||||||||||||||||||||
| 383 | the resulting C<HoH>, using the same data as above, would instead look like: | ||||||||||||||||||||||||||||||||||||||||||
| 384 | |||||||||||||||||||||||||||||||||||||||||||
| 385 | { | ||||||||||||||||||||||||||||||||||||||||||
| 386 | 1 => { 2 => { h3 => [3,5] } }, ## 3 sir! | ||||||||||||||||||||||||||||||||||||||||||
| 387 | } | ||||||||||||||||||||||||||||||||||||||||||
| 388 | |||||||||||||||||||||||||||||||||||||||||||
| 389 | Or if we wanted to sum the values we could us the C<sum> handler for the | ||||||||||||||||||||||||||||||||||||||||||
| 390 | C<on_collide> event: | ||||||||||||||||||||||||||||||||||||||||||
| 391 | |||||||||||||||||||||||||||||||||||||||||||
| 392 | $hoh = xsv_slurp( string => $xsv_data, | ||||||||||||||||||||||||||||||||||||||||||
| 393 | shape => 'hoh', | ||||||||||||||||||||||||||||||||||||||||||
| 394 | key => 'h1,h2', | ||||||||||||||||||||||||||||||||||||||||||
| 395 | on_collide => 'sum', | ||||||||||||||||||||||||||||||||||||||||||
| 396 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 397 | |||||||||||||||||||||||||||||||||||||||||||
| 398 | resulting in the summation of the values: | ||||||||||||||||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||||||||||||||||
| 400 | { | ||||||||||||||||||||||||||||||||||||||||||
| 401 | 1 => { 2 => { h3 => 8 } }, | ||||||||||||||||||||||||||||||||||||||||||
| 402 | } | ||||||||||||||||||||||||||||||||||||||||||
| 403 | |||||||||||||||||||||||||||||||||||||||||||
| 404 | =head2 builtin C<on_store> handlers | ||||||||||||||||||||||||||||||||||||||||||
| 405 | |||||||||||||||||||||||||||||||||||||||||||
| 406 | A number of builtin C<on_store> handlers are provided and can be specified | ||||||||||||||||||||||||||||||||||||||||||
| 407 | by name. | ||||||||||||||||||||||||||||||||||||||||||
| 408 | |||||||||||||||||||||||||||||||||||||||||||
| 409 | The example data structures below use the following data. | ||||||||||||||||||||||||||||||||||||||||||
| 410 | |||||||||||||||||||||||||||||||||||||||||||
| 411 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 412 | 1,2,3 | ||||||||||||||||||||||||||||||||||||||||||
| 413 | 1,2,5 | ||||||||||||||||||||||||||||||||||||||||||
| 414 | |||||||||||||||||||||||||||||||||||||||||||
| 415 | =head3 count | ||||||||||||||||||||||||||||||||||||||||||
| 416 | |||||||||||||||||||||||||||||||||||||||||||
| 417 | Count the times a key occurs. | ||||||||||||||||||||||||||||||||||||||||||
| 418 | |||||||||||||||||||||||||||||||||||||||||||
| 419 | { 1 => { 2 => { h3 => 2 } } } | ||||||||||||||||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||||||||||||||||
| 421 | =head3 frequency | ||||||||||||||||||||||||||||||||||||||||||
| 422 | |||||||||||||||||||||||||||||||||||||||||||
| 423 | Create a frequency count of values. | ||||||||||||||||||||||||||||||||||||||||||
| 424 | |||||||||||||||||||||||||||||||||||||||||||
| 425 | { 1 => { 2 => { h3 => { 3 => 1, 5 => 1 } } } } | ||||||||||||||||||||||||||||||||||||||||||
| 426 | |||||||||||||||||||||||||||||||||||||||||||
| 427 | =head3 push | ||||||||||||||||||||||||||||||||||||||||||
| 428 | |||||||||||||||||||||||||||||||||||||||||||
| 429 | C<push> values onto an array *always*. | ||||||||||||||||||||||||||||||||||||||||||
| 430 | |||||||||||||||||||||||||||||||||||||||||||
| 431 | { 1 => { 2 => { h3 => [ 3, 5 ] } } } | ||||||||||||||||||||||||||||||||||||||||||
| 432 | |||||||||||||||||||||||||||||||||||||||||||
| 433 | =head3 unshift | ||||||||||||||||||||||||||||||||||||||||||
| 434 | |||||||||||||||||||||||||||||||||||||||||||
| 435 | C<unshift> values onto an array *always*. | ||||||||||||||||||||||||||||||||||||||||||
| 436 | |||||||||||||||||||||||||||||||||||||||||||
| 437 | { 1 => { 2 => { h3 => [ 5, 3 ] } } } | ||||||||||||||||||||||||||||||||||||||||||
| 438 | |||||||||||||||||||||||||||||||||||||||||||
| 439 | =head2 builtin C<on_collide> handlers | ||||||||||||||||||||||||||||||||||||||||||
| 440 | |||||||||||||||||||||||||||||||||||||||||||
| 441 | A number of builtin C<on_collide> handlers are provided and can be specified | ||||||||||||||||||||||||||||||||||||||||||
| 442 | by name. | ||||||||||||||||||||||||||||||||||||||||||
| 443 | |||||||||||||||||||||||||||||||||||||||||||
| 444 | The example data structures below use the following data. | ||||||||||||||||||||||||||||||||||||||||||
| 445 | |||||||||||||||||||||||||||||||||||||||||||
| 446 | h1,h2,h3 | ||||||||||||||||||||||||||||||||||||||||||
| 447 | 1,2,3 | ||||||||||||||||||||||||||||||||||||||||||
| 448 | 1,2,5 | ||||||||||||||||||||||||||||||||||||||||||
| 449 | |||||||||||||||||||||||||||||||||||||||||||
| 450 | =head3 sum | ||||||||||||||||||||||||||||||||||||||||||
| 451 | |||||||||||||||||||||||||||||||||||||||||||
| 452 | Sum the values. | ||||||||||||||||||||||||||||||||||||||||||
| 453 | |||||||||||||||||||||||||||||||||||||||||||
| 454 | { 1 => { 2 => { h3 => 8 } } } | ||||||||||||||||||||||||||||||||||||||||||
| 455 | |||||||||||||||||||||||||||||||||||||||||||
| 456 | =head3 average | ||||||||||||||||||||||||||||||||||||||||||
| 457 | |||||||||||||||||||||||||||||||||||||||||||
| 458 | Average the values. | ||||||||||||||||||||||||||||||||||||||||||
| 459 | |||||||||||||||||||||||||||||||||||||||||||
| 460 | { 1 => { 2 => { h3 => 4 } } } | ||||||||||||||||||||||||||||||||||||||||||
| 461 | |||||||||||||||||||||||||||||||||||||||||||
| 462 | =head3 push | ||||||||||||||||||||||||||||||||||||||||||
| 463 | |||||||||||||||||||||||||||||||||||||||||||
| 464 | C<push> values onto an array *only on colliding*. | ||||||||||||||||||||||||||||||||||||||||||
| 465 | |||||||||||||||||||||||||||||||||||||||||||
| 466 | { 1 => { 2 => { h3 => [ 3, 5 ] } } } | ||||||||||||||||||||||||||||||||||||||||||
| 467 | |||||||||||||||||||||||||||||||||||||||||||
| 468 | =head3 unshift | ||||||||||||||||||||||||||||||||||||||||||
| 469 | |||||||||||||||||||||||||||||||||||||||||||
| 470 | C<unshift> values onto an array *only on colliding*. | ||||||||||||||||||||||||||||||||||||||||||
| 471 | |||||||||||||||||||||||||||||||||||||||||||
| 472 | { 1 => { 2 => { h3 => [ 5, 3 ] } } } | ||||||||||||||||||||||||||||||||||||||||||
| 473 | |||||||||||||||||||||||||||||||||||||||||||
| 474 | =head3 die | ||||||||||||||||||||||||||||||||||||||||||
| 475 | |||||||||||||||||||||||||||||||||||||||||||
| 476 | Carp::confess if a collision occurs. | ||||||||||||||||||||||||||||||||||||||||||
| 477 | |||||||||||||||||||||||||||||||||||||||||||
| 478 | Error: key collision in HoH construction (key-value path was: { 'h1' => '1' }, { 'h2' => '2' }) | ||||||||||||||||||||||||||||||||||||||||||
| 479 | |||||||||||||||||||||||||||||||||||||||||||
| 480 | =head3 warn | ||||||||||||||||||||||||||||||||||||||||||
| 481 | |||||||||||||||||||||||||||||||||||||||||||
| 482 | Carp::cluck if a collision occurs. | ||||||||||||||||||||||||||||||||||||||||||
| 483 | |||||||||||||||||||||||||||||||||||||||||||
| 484 | Warning: key collision in HoH construction (key-value path was: { 'h1' => '1' }, { 'h2' => '2' }) | ||||||||||||||||||||||||||||||||||||||||||
| 485 | |||||||||||||||||||||||||||||||||||||||||||
| 486 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 487 | |||||||||||||||||||||||||||||||||||||||||||
| 488 | 10 | my %shape_map = | |||||||||||||||||||||||||||||||||||||||||
| 489 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 490 | 'aoa' => \&_as_aoa, | ||||||||||||||||||||||||||||||||||||||||||
| 491 | 'aoh' => \&_as_aoh, | ||||||||||||||||||||||||||||||||||||||||||
| 492 | 'hoa' => \&_as_hoa, | ||||||||||||||||||||||||||||||||||||||||||
| 493 | 'hoh' => \&_as_hoh, | ||||||||||||||||||||||||||||||||||||||||||
| 494 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 495 | |||||||||||||||||||||||||||||||||||||||||||
| 496 | sub xsv_slurp | ||||||||||||||||||||||||||||||||||||||||||
| 497 | 53 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
xsv_slurp: called CC=13 SCAR=25.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 498 | 53 | my @o = @_; | |||||||||||||||||||||||||||||||||||||||||
| 499 | |||||||||||||||||||||||||||||||||||||||||||
| 500 | ## guess the source if there is an odd number of args | ||||||||||||||||||||||||||||||||||||||||||
| 501 | 53 | ▶ | if ( @o % 2 ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 502 | { | ||||||||||||||||||||||||||||||||||||||||||
| 503 | 1 | my $src = shift @o; | |||||||||||||||||||||||||||||||||||||||||
| 504 | 1 | ▶ | if ( ref $src ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 505 | { | ||||||||||||||||||||||||||||||||||||||||||
| 506 | 0 | @o = ( handle => $src, @o ); | |||||||||||||||||||||||||||||||||||||||||
| 507 | } | ||||||||||||||||||||||||||||||||||||||||||
| 508 | elsif ( $src =~ /[\r\n]/ ) | ||||||||||||||||||||||||||||||||||||||||||
| 509 | { | ||||||||||||||||||||||||||||||||||||||||||
| 510 | 1 | @o = ( string => $src, @o ); | |||||||||||||||||||||||||||||||||||||||||
| 511 | } | ||||||||||||||||||||||||||||||||||||||||||
| 512 | else | ||||||||||||||||||||||||||||||||||||||||||
| 513 | { | ||||||||||||||||||||||||||||||||||||||||||
| 514 | 0 | @o = ( file => $src, @o ); | |||||||||||||||||||||||||||||||||||||||||
| 515 | } | ||||||||||||||||||||||||||||||||||||||||||
| 516 | } | ||||||||||||||||||||||||||||||||||||||||||
| 517 | |||||||||||||||||||||||||||||||||||||||||||
| 518 | ## convert argument list to option hash | ||||||||||||||||||||||||||||||||||||||||||
| 519 | 53 | my %o = @o; | |||||||||||||||||||||||||||||||||||||||||
| 520 | |||||||||||||||||||||||||||||||||||||||||||
| 521 | ## validate the source type | ||||||||||||||||||||||||||||||||||||||||||
| 522 | 53 | my @all_srcs = qw/ file handle string /; | |||||||||||||||||||||||||||||||||||||||||
| 523 | 53 | my @given_srcs = grep { defined $o{$_} } @all_srcs; | |||||||||||||||||||||||||||||||||||||||||
| 524 | |||||||||||||||||||||||||||||||||||||||||||
| 525 | 53 | ▶ | if ( ! @given_srcs ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 526 | { | ||||||||||||||||||||||||||||||||||||||||||
| 527 | 2 | confess "Error: no source given, specify one of: @all_srcs."; | |||||||||||||||||||||||||||||||||||||||||
| 528 | } | ||||||||||||||||||||||||||||||||||||||||||
| 529 | elsif ( @given_srcs > 1 ) | ||||||||||||||||||||||||||||||||||||||||||
| 530 | { | ||||||||||||||||||||||||||||||||||||||||||
| 531 | 0 | confess "Error: too many sources given (@given_srcs), specify only one."; | |||||||||||||||||||||||||||||||||||||||||
| 532 | } | ||||||||||||||||||||||||||||||||||||||||||
| 533 | |||||||||||||||||||||||||||||||||||||||||||
| 534 | ## validate the shape | ||||||||||||||||||||||||||||||||||||||||||
| 535 | 51 | ▶ | my $shape = defined $o{'shape'} ? lc $o{'shape'} : 'aoh'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 536 | 51 | my $shaper = $shape_map{ $shape }; | |||||||||||||||||||||||||||||||||||||||||
| 537 | |||||||||||||||||||||||||||||||||||||||||||
| 538 | 51 | ▶ | if ( ! $shaper ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 539 | { | ||||||||||||||||||||||||||||||||||||||||||
| 540 | 1 | my @all_shapes = keys %shape_map; | |||||||||||||||||||||||||||||||||||||||||
| 541 | 1 | confess "Error: unrecognized shape given ($shape). Must be one of: @all_shapes" | |||||||||||||||||||||||||||||||||||||||||
| 542 | } | ||||||||||||||||||||||||||||||||||||||||||
| 543 | |||||||||||||||||||||||||||||||||||||||||||
| 544 | ## check various global options for expectations | ||||||||||||||||||||||||||||||||||||||||||
| 545 | 50 | ▶ | if ( defined $o{'col_grep'} && ref $o{'col_grep'} ne 'CODE' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $o{"col_grep"} and ref $o{"col_grep"} ne "CODE"
MC/DC100%2/2
defined $o{"col_grep"} and ref $o{"col_grep"} ne "CODE"
defined $o{"col_grep"}
ref $o{"col_grep"} ne "CODE"
Truth table100%3/3
A and B
A= defined $o{"col_grep"}B= ref $o{"col_grep"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 546 | { | ||||||||||||||||||||||||||||||||||||||||||
| 547 | 1 | confess 'Error: col_grep must be a CODE ref'; | |||||||||||||||||||||||||||||||||||||||||
| 548 | } | ||||||||||||||||||||||||||||||||||||||||||
| 549 | |||||||||||||||||||||||||||||||||||||||||||
| 550 | 49 | ▶ | if ( defined $o{'row_grep'} && ref $o{'row_grep'} ne 'CODE' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
defined $o{"row_grep"} and ref $o{"row_grep"} ne "CODE"
MC/DC100%2/2
defined $o{"row_grep"} and ref $o{"row_grep"} ne "CODE"
defined $o{"row_grep"}
ref $o{"row_grep"} ne "CODE"
Truth table100%3/3
A and B
A= defined $o{"row_grep"}B= ref $o{"row_grep"} ne "CODE"
| |||||||||||||||||||||||||||||||||||||||||||
| 551 | { | ||||||||||||||||||||||||||||||||||||||||||
| 552 | 1 | confess 'Error: row_grep must be a CODE ref'; | |||||||||||||||||||||||||||||||||||||||||
| 553 | } | ||||||||||||||||||||||||||||||||||||||||||
| 554 | |||||||||||||||||||||||||||||||||||||||||||
| 555 | ## isolate the source | ||||||||||||||||||||||||||||||||||||||||||
| 556 | 48 | my $src = $given_srcs[0]; | |||||||||||||||||||||||||||||||||||||||||
| 557 | |||||||||||||||||||||||||||||||||||||||||||
| 558 | ## convert the source to a handle | ||||||||||||||||||||||||||||||||||||||||||
| 559 | 48 | my $handle = _get_handle( $src => $o{$src} ); | |||||||||||||||||||||||||||||||||||||||||
| 560 | |||||||||||||||||||||||||||||||||||||||||||
| 561 | ## create the CSV parser | ||||||||||||||||||||||||||||||||||||||||||
| 562 | 47 | ▶ | my $csv = Text::CSV->new( $o{'text_csv'} || () ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$o{'text_csv'} || ()
MC/DC0%0/2
$o{'text_csv'} || ()
$o{'text_csv'}
()
Truth table66%2/3
A || B
A= $o{'text_csv'}B= ()
| |||||||||||||||||||||||||||||||||||||||||||
| 563 | |||||||||||||||||||||||||||||||||||||||||||
| 564 | ## run the data conversion | ||||||||||||||||||||||||||||||||||||||||||
| 565 | 47 | my $data = $shaper->( $handle, $csv, \%o ); | |||||||||||||||||||||||||||||||||||||||||
| 566 | |||||||||||||||||||||||||||||||||||||||||||
| 567 | ## cleanup | ||||||||||||||||||||||||||||||||||||||||||
| 568 | 42 | close $handle; | |||||||||||||||||||||||||||||||||||||||||
| 569 | |||||||||||||||||||||||||||||||||||||||||||
| 570 | 42 | return $data; | |||||||||||||||||||||||||||||||||||||||||
| 571 | } | ||||||||||||||||||||||||||||||||||||||||||
| 572 | |||||||||||||||||||||||||||||||||||||||||||
| 573 | ## arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 574 | ## $handle - file handle | ||||||||||||||||||||||||||||||||||||||||||
| 575 | ## $csv - the Text::CSV parser object | ||||||||||||||||||||||||||||||||||||||||||
| 576 | ## $o - the user options passed to xsv_slurp | ||||||||||||||||||||||||||||||||||||||||||
| 577 | sub _as_aoa | ||||||||||||||||||||||||||||||||||||||||||
| 578 | 7 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_aoa: called CC=7 SCAR=19.5
| |||||||||||||||||||||||||||||||||||||||||||
| 579 | 7 | my ( $handle, $csv, $o ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 580 | |||||||||||||||||||||||||||||||||||||||||||
| 581 | 7 | my @aoa; | |||||||||||||||||||||||||||||||||||||||||
| 582 | |||||||||||||||||||||||||||||||||||||||||||
| 583 | my @cols; | ||||||||||||||||||||||||||||||||||||||||||
| 584 | 7 | my $col_grep; | |||||||||||||||||||||||||||||||||||||||||
| 585 | |||||||||||||||||||||||||||||||||||||||||||
| 586 | 7 | ▶ | while ( my $line = $csv->getline($handle) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 587 | { | ||||||||||||||||||||||||||||||||||||||||||
| 588 | |||||||||||||||||||||||||||||||||||||||||||
| 589 | ## skip unwanted rows | ||||||||||||||||||||||||||||||||||||||||||
| 590 | 14 | ▶ | if ( defined $o->{'row_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 591 | { | ||||||||||||||||||||||||||||||||||||||||||
| 592 | 6 | ▶ | next if ! $o->{'row_grep'}->( $line ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 593 | } | ||||||||||||||||||||||||||||||||||||||||||
| 594 | |||||||||||||||||||||||||||||||||||||||||||
| 595 | ## remove unwanted cols | ||||||||||||||||||||||||||||||||||||||||||
| 596 | 10 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 597 | { | ||||||||||||||||||||||||||||||||||||||||||
| 598 | 3 | ▶ | if ( ! $col_grep ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 599 | { | ||||||||||||||||||||||||||||||||||||||||||
| 600 | 1 | $col_grep++; | |||||||||||||||||||||||||||||||||||||||||
| 601 | 1 | @cols = $o->{'col_grep'}->( 0 .. $#{ $line } ); | |||||||||||||||||||||||||||||||||||||||||
| 602 | } | ||||||||||||||||||||||||||||||||||||||||||
| 603 | 3 | @{ $line } = @{ $line }[@cols]; | |||||||||||||||||||||||||||||||||||||||||
| 604 | } | ||||||||||||||||||||||||||||||||||||||||||
| 605 | |||||||||||||||||||||||||||||||||||||||||||
| 606 | 10 | push @aoa, $line; | |||||||||||||||||||||||||||||||||||||||||
| 607 | |||||||||||||||||||||||||||||||||||||||||||
| 608 | } | ||||||||||||||||||||||||||||||||||||||||||
| 609 | |||||||||||||||||||||||||||||||||||||||||||
| 610 | 7 | ▶ | if ( ! $csv->eof ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 611 | { | ||||||||||||||||||||||||||||||||||||||||||
| 612 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 613 | } | ||||||||||||||||||||||||||||||||||||||||||
| 614 | |||||||||||||||||||||||||||||||||||||||||||
| 615 | 7 | return \@aoa; | |||||||||||||||||||||||||||||||||||||||||
| 616 | } | ||||||||||||||||||||||||||||||||||||||||||
| 617 | |||||||||||||||||||||||||||||||||||||||||||
| 618 | ## arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 619 | ## $handle - file handle | ||||||||||||||||||||||||||||||||||||||||||
| 620 | ## $csv - the Text::CSV parser object | ||||||||||||||||||||||||||||||||||||||||||
| 621 | ## $o - the user options passed to xsv_slurp | ||||||||||||||||||||||||||||||||||||||||||
| 622 | sub _as_aoh | ||||||||||||||||||||||||||||||||||||||||||
| 623 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_aoh: called CC=9 SCAR=22.1
| |||||||||||||||||||||||||||||||||||||||||||
| 624 | 9 | my ( $handle, $csv, $o ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 625 | |||||||||||||||||||||||||||||||||||||||||||
| 626 | 9 | my @aoh; | |||||||||||||||||||||||||||||||||||||||||
| 627 | |||||||||||||||||||||||||||||||||||||||||||
| 628 | 9 | my $headers = $csv->getline($handle); | |||||||||||||||||||||||||||||||||||||||||
| 629 | |||||||||||||||||||||||||||||||||||||||||||
| 630 | 9 | ▶ | return \@aoh if $csv->eof; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 631 | |||||||||||||||||||||||||||||||||||||||||||
| 632 | 7 | ▶ | if ( ! defined $headers ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 633 | { | ||||||||||||||||||||||||||||||||||||||||||
| 634 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 635 | } | ||||||||||||||||||||||||||||||||||||||||||
| 636 | |||||||||||||||||||||||||||||||||||||||||||
| 637 | 7 | my @headers = @{ $headers }; | |||||||||||||||||||||||||||||||||||||||||
| 638 | |||||||||||||||||||||||||||||||||||||||||||
| 639 | 7 | my @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 640 | |||||||||||||||||||||||||||||||||||||||||||
| 641 | 7 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 642 | { | ||||||||||||||||||||||||||||||||||||||||||
| 643 | 1 | @grep_headers = $o->{'col_grep'}->( @headers ); | |||||||||||||||||||||||||||||||||||||||||
| 644 | } | ||||||||||||||||||||||||||||||||||||||||||
| 645 | |||||||||||||||||||||||||||||||||||||||||||
| 646 | 7 | ▶ | while ( my $line = $csv->getline($handle) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 647 | { | ||||||||||||||||||||||||||||||||||||||||||
| 648 | |||||||||||||||||||||||||||||||||||||||||||
| 649 | 7 | my %line; | |||||||||||||||||||||||||||||||||||||||||
| 650 | |||||||||||||||||||||||||||||||||||||||||||
| 651 | 7 | @line{ @headers } = @{ $line }; | |||||||||||||||||||||||||||||||||||||||||
| 652 | |||||||||||||||||||||||||||||||||||||||||||
| 653 | ## skip unwanted rows | ||||||||||||||||||||||||||||||||||||||||||
| 654 | 7 | ▶ | if ( defined $o->{'row_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 655 | { | ||||||||||||||||||||||||||||||||||||||||||
| 656 | 2 | ▶ | next if ! $o->{'row_grep'}->( \%line ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 657 | } | ||||||||||||||||||||||||||||||||||||||||||
| 658 | |||||||||||||||||||||||||||||||||||||||||||
| 659 | ## remove unwanted cols | ||||||||||||||||||||||||||||||||||||||||||
| 660 | 6 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 661 | { | ||||||||||||||||||||||||||||||||||||||||||
| 662 | 2 | %line = map { $_ => $line{$_} } @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 663 | } | ||||||||||||||||||||||||||||||||||||||||||
| 664 | |||||||||||||||||||||||||||||||||||||||||||
| 665 | 6 | push @aoh, \%line; | |||||||||||||||||||||||||||||||||||||||||
| 666 | |||||||||||||||||||||||||||||||||||||||||||
| 667 | } | ||||||||||||||||||||||||||||||||||||||||||
| 668 | |||||||||||||||||||||||||||||||||||||||||||
| 669 | 7 | ▶ | if ( ! $csv->eof ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 670 | { | ||||||||||||||||||||||||||||||||||||||||||
| 671 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 672 | } | ||||||||||||||||||||||||||||||||||||||||||
| 673 | |||||||||||||||||||||||||||||||||||||||||||
| 674 | 7 | return \@aoh; | |||||||||||||||||||||||||||||||||||||||||
| 675 | } | ||||||||||||||||||||||||||||||||||||||||||
| 676 | |||||||||||||||||||||||||||||||||||||||||||
| 677 | ## arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 678 | ## $handle - file handle | ||||||||||||||||||||||||||||||||||||||||||
| 679 | ## $csv - the Text::CSV parser object | ||||||||||||||||||||||||||||||||||||||||||
| 680 | ## $o - the user options passed to xsv_slurp | ||||||||||||||||||||||||||||||||||||||||||
| 681 | sub _as_hoa | ||||||||||||||||||||||||||||||||||||||||||
| 682 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_hoa: called CC=10 SCAR=23.1
| |||||||||||||||||||||||||||||||||||||||||||
| 683 | 6 | my ( $handle, $csv, $o ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 684 | |||||||||||||||||||||||||||||||||||||||||||
| 685 | 6 | my %hoa; | |||||||||||||||||||||||||||||||||||||||||
| 686 | |||||||||||||||||||||||||||||||||||||||||||
| 687 | 6 | my $headers = $csv->getline($handle); | |||||||||||||||||||||||||||||||||||||||||
| 688 | |||||||||||||||||||||||||||||||||||||||||||
| 689 | 6 | ▶ | return \%hoa if $csv->eof; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 690 | |||||||||||||||||||||||||||||||||||||||||||
| 691 | 5 | ▶ | if ( ! defined $headers ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 692 | { | ||||||||||||||||||||||||||||||||||||||||||
| 693 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 694 | } | ||||||||||||||||||||||||||||||||||||||||||
| 695 | |||||||||||||||||||||||||||||||||||||||||||
| 696 | 5 | my @headers = @{ $headers }; | |||||||||||||||||||||||||||||||||||||||||
| 697 | |||||||||||||||||||||||||||||||||||||||||||
| 698 | 5 | my @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 699 | |||||||||||||||||||||||||||||||||||||||||||
| 700 | 5 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 701 | { | ||||||||||||||||||||||||||||||||||||||||||
| 702 | 1 | @grep_headers = $o->{'col_grep'}->( @headers ); | |||||||||||||||||||||||||||||||||||||||||
| 703 | 1 | @hoa{ @grep_headers } = map { [] } @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 704 | } | ||||||||||||||||||||||||||||||||||||||||||
| 705 | else | ||||||||||||||||||||||||||||||||||||||||||
| 706 | { | ||||||||||||||||||||||||||||||||||||||||||
| 707 | 4 | @hoa{ @headers } = map { [] } @headers; | |||||||||||||||||||||||||||||||||||||||||
| 708 | } | ||||||||||||||||||||||||||||||||||||||||||
| 709 | |||||||||||||||||||||||||||||||||||||||||||
| 710 | 5 | ▶ | while ( my $line = $csv->getline($handle) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 711 | { | ||||||||||||||||||||||||||||||||||||||||||
| 712 | 6 | my %line; | |||||||||||||||||||||||||||||||||||||||||
| 713 | |||||||||||||||||||||||||||||||||||||||||||
| 714 | 6 | @line{ @headers } = @{ $line }; | |||||||||||||||||||||||||||||||||||||||||
| 715 | |||||||||||||||||||||||||||||||||||||||||||
| 716 | ## skip unwanted rows | ||||||||||||||||||||||||||||||||||||||||||
| 717 | 6 | ▶ | if ( defined $o->{'row_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 718 | { | ||||||||||||||||||||||||||||||||||||||||||
| 719 | 2 | ▶ | next if ! $o->{'row_grep'}->( \%line ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 720 | } | ||||||||||||||||||||||||||||||||||||||||||
| 721 | |||||||||||||||||||||||||||||||||||||||||||
| 722 | ## remove unwanted cols | ||||||||||||||||||||||||||||||||||||||||||
| 723 | 5 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 724 | { | ||||||||||||||||||||||||||||||||||||||||||
| 725 | 2 | %line = map { $_ => $line{$_} } @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 726 | } | ||||||||||||||||||||||||||||||||||||||||||
| 727 | |||||||||||||||||||||||||||||||||||||||||||
| 728 | 5 | for my $k ( keys %line ) | |||||||||||||||||||||||||||||||||||||||||
| 729 | { | ||||||||||||||||||||||||||||||||||||||||||
| 730 | 12 | push @{ $hoa{$k} }, $line{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 731 | } | ||||||||||||||||||||||||||||||||||||||||||
| 732 | |||||||||||||||||||||||||||||||||||||||||||
| 733 | } | ||||||||||||||||||||||||||||||||||||||||||
| 734 | |||||||||||||||||||||||||||||||||||||||||||
| 735 | 5 | ▶ | if ( ! $csv->eof ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 736 | { | ||||||||||||||||||||||||||||||||||||||||||
| 737 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 738 | } | ||||||||||||||||||||||||||||||||||||||||||
| 739 | |||||||||||||||||||||||||||||||||||||||||||
| 740 | 5 | return \%hoa; | |||||||||||||||||||||||||||||||||||||||||
| 741 | } | ||||||||||||||||||||||||||||||||||||||||||
| 742 | |||||||||||||||||||||||||||||||||||||||||||
| 743 | my %named_handlers = | ||||||||||||||||||||||||||||||||||||||||||
| 744 | ( | ||||||||||||||||||||||||||||||||||||||||||
| 745 | |||||||||||||||||||||||||||||||||||||||||||
| 746 | ## predefined methods for handling hoh storage | ||||||||||||||||||||||||||||||||||||||||||
| 747 | on_store => | ||||||||||||||||||||||||||||||||||||||||||
| 748 | { | ||||||||||||||||||||||||||||||||||||||||||
| 749 | |||||||||||||||||||||||||||||||||||||||||||
| 750 | ## count | ||||||||||||||||||||||||||||||||||||||||||
| 751 | 'count' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 752 | 9 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 753 | 9 | ▶ | return ( $_[HOH_HANDLER_OLD_VALUE] || 0 ) + 1; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || 0
MC/DC100%1/1
$_[2] || 0
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 754 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 755 | |||||||||||||||||||||||||||||||||||||||||||
| 756 | ## value histogram (count occurences of each value) | ||||||||||||||||||||||||||||||||||||||||||
| 757 | 'frequency' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 758 | 8 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 759 | 8 | ▶ | my $ref = $_[HOH_HANDLER_OLD_VALUE] || {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || {}
MC/DC100%1/1
$_[2] || {}
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 760 | 8 | $ref->{ $_[HOH_HANDLER_NEW_VALUE] } ++; | |||||||||||||||||||||||||||||||||||||||||
| 761 | 8 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 762 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 763 | |||||||||||||||||||||||||||||||||||||||||||
| 764 | ## push to array | ||||||||||||||||||||||||||||||||||||||||||
| 765 | 'push' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 766 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 767 | 6 | ▶ | my $ref = $_[HOH_HANDLER_OLD_VALUE] || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || []
MC/DC100%1/1
$_[2] || []
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 768 | 6 | push @{ $ref }, $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 769 | 6 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 770 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 771 | |||||||||||||||||||||||||||||||||||||||||||
| 772 | ## unshift to array | ||||||||||||||||||||||||||||||||||||||||||
| 773 | 'unshift' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 774 | 6 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 775 | 6 | ▶ | my $ref = $_[HOH_HANDLER_OLD_VALUE] || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$_[2] || []
MC/DC100%1/1
$_[2] || []
$_[2]
Truth table100%2/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||||||
| 776 | 6 | unshift @{ $ref }, $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 777 | 6 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 778 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 779 | |||||||||||||||||||||||||||||||||||||||||||
| 780 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 781 | |||||||||||||||||||||||||||||||||||||||||||
| 782 | ## predefined methods for handling hoh collisions | ||||||||||||||||||||||||||||||||||||||||||
| 783 | on_collide => | ||||||||||||||||||||||||||||||||||||||||||
| 784 | { | ||||||||||||||||||||||||||||||||||||||||||
| 785 | |||||||||||||||||||||||||||||||||||||||||||
| 786 | ## sum | ||||||||||||||||||||||||||||||||||||||||||
| 787 | 'sum' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 788 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=12.7
| |||||||||||||||||||||||||||||||||||||||||||
| 789 | 2 | ▶ | return ( $_[HOH_HANDLER_OLD_VALUE] || 0 ) + ( $_[HOH_HANDLER_NEW_VALUE] || 0 ); | ||||||||||||||||||||||||||||||||||||||||
|
Condition50%2/4
$_[2] || 0
$_[3] || 0
MC/DC0%0/1
$_[2] || 0
$_[2]
MC/DC0%0/1
$_[3] || 0
$_[3]
Truth table50%1/2
A
A= $_[2]
Truth table50%1/2
A
A= $_[3]
| |||||||||||||||||||||||||||||||||||||||||||
| 790 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 791 | |||||||||||||||||||||||||||||||||||||||||||
| 792 | ## average | ||||||||||||||||||||||||||||||||||||||||||
| 793 | 'average' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 794 | 5 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||||||
| 795 | 5 | ▶ | if ( ! exists $_[HOH_HANDLER_SCRATCH_PAD]{'count'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 796 | { | ||||||||||||||||||||||||||||||||||||||||||
| 797 | 2 | $_[HOH_HANDLER_SCRATCH_PAD]{'count'} = 1; | |||||||||||||||||||||||||||||||||||||||||
| 798 | 2 | $_[HOH_HANDLER_SCRATCH_PAD]{'sum'} = $_[HOH_HANDLER_OLD_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 799 | } | ||||||||||||||||||||||||||||||||||||||||||
| 800 | 5 | $_[HOH_HANDLER_SCRATCH_PAD]{'count'}++; | |||||||||||||||||||||||||||||||||||||||||
| 801 | 5 | $_[HOH_HANDLER_SCRATCH_PAD]{'sum'} += $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 802 | 5 | return $_[HOH_HANDLER_SCRATCH_PAD]{'sum'} / $_[HOH_HANDLER_SCRATCH_PAD]{'count'}; | |||||||||||||||||||||||||||||||||||||||||
| 803 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 804 | |||||||||||||||||||||||||||||||||||||||||||
| 805 | ## die | ||||||||||||||||||||||||||||||||||||||||||
| 806 | 'die' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 807 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 808 | 1 | ▶ | if ( defined $_[HOH_HANDLER_OLD_VALUE] ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 809 | { | ||||||||||||||||||||||||||||||||||||||||||
| 810 | 1 | my @kv_pairs = @{ $_[HOH_HANDLER_KEY_VALUE_PATH] }; | |||||||||||||||||||||||||||||||||||||||||
| 811 | 1 | my @kv_strings = map { "{ '$_->[0]' => '$_->[1]' }" } @kv_pairs; | |||||||||||||||||||||||||||||||||||||||||
| 812 | 1 | my $kv_path = join ', ', @kv_strings; | |||||||||||||||||||||||||||||||||||||||||
| 813 | 1 | confess "Error: key collision in HoH construction (key-value path was: $kv_path)"; | |||||||||||||||||||||||||||||||||||||||||
| 814 | } | ||||||||||||||||||||||||||||||||||||||||||
| 815 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 816 | |||||||||||||||||||||||||||||||||||||||||||
| 817 | ## warn | ||||||||||||||||||||||||||||||||||||||||||
| 818 | 'warn' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 819 | 1 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 820 | 1 | ▶ | if ( defined $_[HOH_HANDLER_OLD_VALUE] ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 821 | { | ||||||||||||||||||||||||||||||||||||||||||
| 822 | 1 | my @kv_pairs = @{ $_[HOH_HANDLER_KEY_VALUE_PATH] }; | |||||||||||||||||||||||||||||||||||||||||
| 823 | 1 | my @kv_strings = map { "{ '$_->[0]' => '$_->[1]' }" } @kv_pairs; | |||||||||||||||||||||||||||||||||||||||||
| 824 | 1 | my $kv_path = join ', ', @kv_strings; | |||||||||||||||||||||||||||||||||||||||||
| 825 | 1 | cluck "Warning: key collision in HoH construction (key-value path was: $kv_path)"; | |||||||||||||||||||||||||||||||||||||||||
| 826 | } | ||||||||||||||||||||||||||||||||||||||||||
| 827 | 1 | return $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 828 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 829 | |||||||||||||||||||||||||||||||||||||||||||
| 830 | ## push to array | ||||||||||||||||||||||||||||||||||||||||||
| 831 | 'push' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 832 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 833 | 2 | ▶ | my $ref = ref $_[HOH_HANDLER_OLD_VALUE] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 834 | ? $_[HOH_HANDLER_OLD_VALUE] | ||||||||||||||||||||||||||||||||||||||||||
| 835 | : [ $_[HOH_HANDLER_OLD_VALUE] ]; | ||||||||||||||||||||||||||||||||||||||||||
| 836 | 2 | push @{ $ref }, $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 837 | 2 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 838 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 839 | |||||||||||||||||||||||||||||||||||||||||||
| 840 | ## unshift to array | ||||||||||||||||||||||||||||||||||||||||||
| 841 | 'unshift' => sub | ||||||||||||||||||||||||||||||||||||||||||
| 842 | 2 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=7.0
| |||||||||||||||||||||||||||||||||||||||||||
| 843 | 2 | ▶ | my $ref = ref $_[HOH_HANDLER_OLD_VALUE] | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 844 | ? $_[HOH_HANDLER_OLD_VALUE] | ||||||||||||||||||||||||||||||||||||||||||
| 845 | : [ $_[HOH_HANDLER_OLD_VALUE] ]; | ||||||||||||||||||||||||||||||||||||||||||
| 846 | 2 | unshift @{ $ref }, $_[HOH_HANDLER_NEW_VALUE]; | |||||||||||||||||||||||||||||||||||||||||
| 847 | 2 | return $ref; | |||||||||||||||||||||||||||||||||||||||||
| 848 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 849 | |||||||||||||||||||||||||||||||||||||||||||
| 850 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 851 | |||||||||||||||||||||||||||||||||||||||||||
| 852 | 10 | ); | |||||||||||||||||||||||||||||||||||||||||
| 853 | |||||||||||||||||||||||||||||||||||||||||||
| 854 | ## arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 855 | ## $handle - file handle | ||||||||||||||||||||||||||||||||||||||||||
| 856 | ## $csv - the Text::CSV parser object | ||||||||||||||||||||||||||||||||||||||||||
| 857 | ## $o - the user options passed to xsv_slurp | ||||||||||||||||||||||||||||||||||||||||||
| 858 | sub _as_hoh | ||||||||||||||||||||||||||||||||||||||||||
| 859 | 25 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_as_hoh: called CC=27 SCAR=33.1
| |||||||||||||||||||||||||||||||||||||||||||
| 860 | 25 | my ( $handle, $csv, $o ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 861 | |||||||||||||||||||||||||||||||||||||||||||
| 862 | 25 | my %hoh; | |||||||||||||||||||||||||||||||||||||||||
| 863 | |||||||||||||||||||||||||||||||||||||||||||
| 864 | 25 | my $headers = $csv->getline($handle); | |||||||||||||||||||||||||||||||||||||||||
| 865 | |||||||||||||||||||||||||||||||||||||||||||
| 866 | 25 | ▶ | return \%hoh if $csv->eof; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 867 | |||||||||||||||||||||||||||||||||||||||||||
| 868 | 24 | ▶ | if ( ! defined $headers ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 869 | { | ||||||||||||||||||||||||||||||||||||||||||
| 870 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 871 | } | ||||||||||||||||||||||||||||||||||||||||||
| 872 | |||||||||||||||||||||||||||||||||||||||||||
| 873 | 24 | my @headers = @{ $headers }; | |||||||||||||||||||||||||||||||||||||||||
| 874 | |||||||||||||||||||||||||||||||||||||||||||
| 875 | 24 | my @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 876 | |||||||||||||||||||||||||||||||||||||||||||
| 877 | 24 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 878 | { | ||||||||||||||||||||||||||||||||||||||||||
| 879 | 1 | @grep_headers = $o->{'col_grep'}->( @headers ); | |||||||||||||||||||||||||||||||||||||||||
| 880 | } | ||||||||||||||||||||||||||||||||||||||||||
| 881 | |||||||||||||||||||||||||||||||||||||||||||
| 882 | 24 | my @key; | |||||||||||||||||||||||||||||||||||||||||
| 883 | |||||||||||||||||||||||||||||||||||||||||||
| 884 | 24 | ▶ | if ( ref $o->{'key'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 885 | { | ||||||||||||||||||||||||||||||||||||||||||
| 886 | |||||||||||||||||||||||||||||||||||||||||||
| 887 | 0 | @key = @{ $o->{'key'} }; | |||||||||||||||||||||||||||||||||||||||||
| 888 | |||||||||||||||||||||||||||||||||||||||||||
| 889 | } | ||||||||||||||||||||||||||||||||||||||||||
| 890 | elsif ( defined $o->{'key'} ) | ||||||||||||||||||||||||||||||||||||||||||
| 891 | { | ||||||||||||||||||||||||||||||||||||||||||
| 892 | |||||||||||||||||||||||||||||||||||||||||||
| 893 | 23 | ▶ | if ( ! $csv->parse( $o->{'key'} ) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 894 | { | ||||||||||||||||||||||||||||||||||||||||||
| 895 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 896 | } | ||||||||||||||||||||||||||||||||||||||||||
| 897 | |||||||||||||||||||||||||||||||||||||||||||
| 898 | 23 | @key = $csv->fields; | |||||||||||||||||||||||||||||||||||||||||
| 899 | |||||||||||||||||||||||||||||||||||||||||||
| 900 | } | ||||||||||||||||||||||||||||||||||||||||||
| 901 | else | ||||||||||||||||||||||||||||||||||||||||||
| 902 | { | ||||||||||||||||||||||||||||||||||||||||||
| 903 | 1 | confess 'Error: no key given for hoh shape'; | |||||||||||||||||||||||||||||||||||||||||
| 904 | } | ||||||||||||||||||||||||||||||||||||||||||
| 905 | |||||||||||||||||||||||||||||||||||||||||||
| 906 | ## set the on_collide handler at the default level and by header | ||||||||||||||||||||||||||||||||||||||||||
| 907 | 23 | my %storage_handlers; | |||||||||||||||||||||||||||||||||||||||||
| 908 | |||||||||||||||||||||||||||||||||||||||||||
| 909 | 23 | for my $header ( @headers ) | |||||||||||||||||||||||||||||||||||||||||
| 910 | { | ||||||||||||||||||||||||||||||||||||||||||
| 911 | |||||||||||||||||||||||||||||||||||||||||||
| 912 | 66 | for my $type ( qw/ on_store on_collide / ) | |||||||||||||||||||||||||||||||||||||||||
| 913 | { | ||||||||||||||||||||||||||||||||||||||||||
| 914 | |||||||||||||||||||||||||||||||||||||||||||
| 915 | 131 | my $handler = $o->{$type}; | |||||||||||||||||||||||||||||||||||||||||
| 916 | |||||||||||||||||||||||||||||||||||||||||||
| 917 | 131 | ▶ | next if ! $handler; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 918 | |||||||||||||||||||||||||||||||||||||||||||
| 919 | 51 | ▶ | if ( ref $handler eq 'HASH' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 920 | { | ||||||||||||||||||||||||||||||||||||||||||
| 921 | 14 | $handler = $handler->{$header}; | |||||||||||||||||||||||||||||||||||||||||
| 922 | } | ||||||||||||||||||||||||||||||||||||||||||
| 923 | |||||||||||||||||||||||||||||||||||||||||||
| 924 | 51 | ▶ | next if ! $handler; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 925 | |||||||||||||||||||||||||||||||||||||||||||
| 926 | 47 | ▶ | if ( ! ref $handler ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 927 | { | ||||||||||||||||||||||||||||||||||||||||||
| 928 | |||||||||||||||||||||||||||||||||||||||||||
| 929 | 44 | ▶ | if ( ! exists $named_handlers{$type}{$handler} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 930 | { | ||||||||||||||||||||||||||||||||||||||||||
| 931 | 2 | my $all_names = join ', ', sort keys %{ $named_handlers{$type} }; | |||||||||||||||||||||||||||||||||||||||||
| 932 | 2 | confess "Error: invalid '$type' handler given ($handler). Must be one of: $all_names." | |||||||||||||||||||||||||||||||||||||||||
| 933 | } | ||||||||||||||||||||||||||||||||||||||||||
| 934 | |||||||||||||||||||||||||||||||||||||||||||
| 935 | 42 | $handler = $named_handlers{$type}{$handler}; | |||||||||||||||||||||||||||||||||||||||||
| 936 | } | ||||||||||||||||||||||||||||||||||||||||||
| 937 | |||||||||||||||||||||||||||||||||||||||||||
| 938 | confess "Error: cannot set multiple storage handlers for '$header'" | ||||||||||||||||||||||||||||||||||||||||||
| 939 | 45 | ▶ | if $storage_handlers{$header}; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 940 | |||||||||||||||||||||||||||||||||||||||||||
| 941 | 44 | $storage_handlers{$header}{$type} = $handler; | |||||||||||||||||||||||||||||||||||||||||
| 942 | |||||||||||||||||||||||||||||||||||||||||||
| 943 | } | ||||||||||||||||||||||||||||||||||||||||||
| 944 | |||||||||||||||||||||||||||||||||||||||||||
| 945 | } | ||||||||||||||||||||||||||||||||||||||||||
| 946 | |||||||||||||||||||||||||||||||||||||||||||
| 947 | ## per-header scratch-pads used in collision functions | ||||||||||||||||||||||||||||||||||||||||||
| 948 | 20 | my %scratch_pads = map { $_ => {} } @headers; | |||||||||||||||||||||||||||||||||||||||||
| 949 | |||||||||||||||||||||||||||||||||||||||||||
| 950 | 20 | ▶ | while ( my $line = $csv->getline($handle) ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 951 | { | ||||||||||||||||||||||||||||||||||||||||||
| 952 | |||||||||||||||||||||||||||||||||||||||||||
| 953 | 55 | my %line; | |||||||||||||||||||||||||||||||||||||||||
| 954 | |||||||||||||||||||||||||||||||||||||||||||
| 955 | 55 | @line{ @headers } = @{ $line }; | |||||||||||||||||||||||||||||||||||||||||
| 956 | |||||||||||||||||||||||||||||||||||||||||||
| 957 | ## skip unwanted rows | ||||||||||||||||||||||||||||||||||||||||||
| 958 | 55 | ▶ | if ( defined $o->{'row_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 959 | { | ||||||||||||||||||||||||||||||||||||||||||
| 960 | 2 | ▶ | next if ! $o->{'row_grep'}->( \%line ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 961 | } | ||||||||||||||||||||||||||||||||||||||||||
| 962 | |||||||||||||||||||||||||||||||||||||||||||
| 963 | ## step through the nested keys | ||||||||||||||||||||||||||||||||||||||||||
| 964 | 54 | my $leaf = \%hoh; | |||||||||||||||||||||||||||||||||||||||||
| 965 | |||||||||||||||||||||||||||||||||||||||||||
| 966 | 54 | my @val; | |||||||||||||||||||||||||||||||||||||||||
| 967 | |||||||||||||||||||||||||||||||||||||||||||
| 968 | 54 | for my $k ( @key ) | |||||||||||||||||||||||||||||||||||||||||
| 969 | { | ||||||||||||||||||||||||||||||||||||||||||
| 970 | |||||||||||||||||||||||||||||||||||||||||||
| 971 | 101 | my $v = $line{$k}; | |||||||||||||||||||||||||||||||||||||||||
| 972 | 101 | ▶ | $leaf->{$v} ||= {}; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$leaf->{$v} ||= {}
MC/DC100%1/1
$leaf->{$v} ||= {}
$leaf->{$v}
Truth table100%2/2
A
A= $leaf->{$v}
| |||||||||||||||||||||||||||||||||||||||||||
| 973 | 101 | $leaf = $leaf->{$v}; | |||||||||||||||||||||||||||||||||||||||||
| 974 | |||||||||||||||||||||||||||||||||||||||||||
| 975 | 101 | push @val, $v; | |||||||||||||||||||||||||||||||||||||||||
| 976 | |||||||||||||||||||||||||||||||||||||||||||
| 977 | } | ||||||||||||||||||||||||||||||||||||||||||
| 978 | |||||||||||||||||||||||||||||||||||||||||||
| 979 | ## remove key headers from the line | ||||||||||||||||||||||||||||||||||||||||||
| 980 | 54 | delete @line{ @key }; | |||||||||||||||||||||||||||||||||||||||||
| 981 | |||||||||||||||||||||||||||||||||||||||||||
| 982 | ## remove unwanted cols | ||||||||||||||||||||||||||||||||||||||||||
| 983 | 54 | ▶ | if ( defined $o->{'col_grep'} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 984 | { | ||||||||||||||||||||||||||||||||||||||||||
| 985 | 2 | %line = map { $_ => $line{$_} } @grep_headers; | |||||||||||||||||||||||||||||||||||||||||
| 986 | } | ||||||||||||||||||||||||||||||||||||||||||
| 987 | |||||||||||||||||||||||||||||||||||||||||||
| 988 | ## perform the aggregation if applicable | ||||||||||||||||||||||||||||||||||||||||||
| 989 | 54 | for my $key ( keys %line ) | |||||||||||||||||||||||||||||||||||||||||
| 990 | { | ||||||||||||||||||||||||||||||||||||||||||
| 991 | |||||||||||||||||||||||||||||||||||||||||||
| 992 | 73 | my $new_value = $line{$key}; | |||||||||||||||||||||||||||||||||||||||||
| 993 | |||||||||||||||||||||||||||||||||||||||||||
| 994 | 73 | my $on_collide = $storage_handlers{$key}{'on_collide'}; | |||||||||||||||||||||||||||||||||||||||||
| 995 | 73 | my $on_store = $storage_handlers{$key}{'on_store'}; | |||||||||||||||||||||||||||||||||||||||||
| 996 | |||||||||||||||||||||||||||||||||||||||||||
| 997 | 73 | ▶ | if ( $on_store || $on_collide && exists $leaf->{$key} ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$on_store or $on_collide and exists $leaf->{$key}
$on_collide and exists $leaf->{$key}
MC/DC100%3/3
$on_store or $on_collide and exists $leaf->{$key}
$on_store
$on_collide
exists $leaf->{$key}
Truth table100%4/4
A or B and C
A= $on_storeB= $on_collideC= exists $leaf->{$key}
| |||||||||||||||||||||||||||||||||||||||||||
| 998 | { | ||||||||||||||||||||||||||||||||||||||||||
| 999 | |||||||||||||||||||||||||||||||||||||||||||
| 1000 | 47 | ▶ | my $handler = $on_collide || $on_store; | ||||||||||||||||||||||||||||||||||||||||
|
Condition66%2/3
$on_collide || $on_store
MC/DC0%0/2
$on_collide || $on_store
$on_collide
$on_store
Truth table66%2/3
A || B
A= $on_collideB= $on_store
| |||||||||||||||||||||||||||||||||||||||||||
| 1001 | |||||||||||||||||||||||||||||||||||||||||||
| 1002 | $new_value = $handler->( | ||||||||||||||||||||||||||||||||||||||||||
| 1003 | $key, ## HOH_HANDLER_KEY | ||||||||||||||||||||||||||||||||||||||||||
| 1004 | [ map [ $key[$_] => $val[$_] ], 0 .. $#key ], ## HOH_HANDLER_KEY_VALUE_PATH | ||||||||||||||||||||||||||||||||||||||||||
| 1005 | $leaf->{$key}, ## HOH_HANDLER_OLD_VALUE | ||||||||||||||||||||||||||||||||||||||||||
| 1006 | $new_value, ## HOH_HANDLER_NEW_VALUE | ||||||||||||||||||||||||||||||||||||||||||
| 1007 | \%line, ## HOH_HANDLER_LINE_HASH | ||||||||||||||||||||||||||||||||||||||||||
| 1008 | \%hoh, ## HOH_HANDLER_HOH | ||||||||||||||||||||||||||||||||||||||||||
| 1009 | 47 | $scratch_pads{$key}, ## HOH_HANDLER_SCRATCH_PAD | |||||||||||||||||||||||||||||||||||||||||
| 1010 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 1011 | |||||||||||||||||||||||||||||||||||||||||||
| 1012 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1013 | |||||||||||||||||||||||||||||||||||||||||||
| 1014 | 72 | $leaf->{$key} = $new_value; | |||||||||||||||||||||||||||||||||||||||||
| 1015 | |||||||||||||||||||||||||||||||||||||||||||
| 1016 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1017 | |||||||||||||||||||||||||||||||||||||||||||
| 1018 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1019 | |||||||||||||||||||||||||||||||||||||||||||
| 1020 | 19 | ▶ | if ( ! $csv->eof ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1021 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1022 | 0 | confess 'Error: ' . $csv->error_diag; | |||||||||||||||||||||||||||||||||||||||||
| 1023 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1024 | |||||||||||||||||||||||||||||||||||||||||||
| 1025 | 19 | return \%hoh; | |||||||||||||||||||||||||||||||||||||||||
| 1026 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1027 | |||||||||||||||||||||||||||||||||||||||||||
| 1028 | ## arguments: | ||||||||||||||||||||||||||||||||||||||||||
| 1029 | ## $src_type - type of data source, handle, string or file | ||||||||||||||||||||||||||||||||||||||||||
| 1030 | ## $src_value - the file name, file handle or xSV string | ||||||||||||||||||||||||||||||||||||||||||
| 1031 | sub _get_handle | ||||||||||||||||||||||||||||||||||||||||||
| 1032 | 48 | ▶ | { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_get_handle: called CC=5 SCAR=17.8
| |||||||||||||||||||||||||||||||||||||||||||
| 1033 | 48 | my ( $src_type, $src_value ) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 1034 | |||||||||||||||||||||||||||||||||||||||||||
| 1035 | 48 | ▶ | if ( $src_type eq 'handle' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1036 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1037 | 0 | return $src_value; | |||||||||||||||||||||||||||||||||||||||||
| 1038 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1039 | |||||||||||||||||||||||||||||||||||||||||||
| 1040 | 48 | ▶ | if ( $src_type eq 'string' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1041 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1042 | 47 | my $handle = IO::String->new( $src_value ); | |||||||||||||||||||||||||||||||||||||||||
| 1043 | 47 | return $handle; | |||||||||||||||||||||||||||||||||||||||||
| 1044 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1045 | |||||||||||||||||||||||||||||||||||||||||||
| 1046 | 1 | ▶ | if ( $src_type eq 'file' ) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1047 | { | ||||||||||||||||||||||||||||||||||||||||||
| 1048 | 1 | ▶ | open( my $handle, '<', $src_value ) || confess "Error: could not open '$src_value': $!"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 1049 | 0 | return $handle; | |||||||||||||||||||||||||||||||||||||||||
| 1050 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1051 | |||||||||||||||||||||||||||||||||||||||||||
| 1052 | 0 | confess "Error: could not determine source type"; | |||||||||||||||||||||||||||||||||||||||||
| 1053 | } | ||||||||||||||||||||||||||||||||||||||||||
| 1054 | |||||||||||||||||||||||||||||||||||||||||||
| 1055 | =head1 AUTHOR | ||||||||||||||||||||||||||||||||||||||||||
| 1056 | |||||||||||||||||||||||||||||||||||||||||||
| 1057 | Dan Boorstein, C<< <dan at boorstein.net> >> | ||||||||||||||||||||||||||||||||||||||||||
| 1058 | |||||||||||||||||||||||||||||||||||||||||||
| 1059 | =head1 TODO | ||||||||||||||||||||||||||||||||||||||||||
| 1060 | |||||||||||||||||||||||||||||||||||||||||||
| 1061 | =over | ||||||||||||||||||||||||||||||||||||||||||
| 1062 | |||||||||||||||||||||||||||||||||||||||||||
| 1063 | =item * add xsv_eruct() to dump shapes to xsv data | ||||||||||||||||||||||||||||||||||||||||||
| 1064 | |||||||||||||||||||||||||||||||||||||||||||
| 1065 | =item * add weighted-average collide keys and tests | ||||||||||||||||||||||||||||||||||||||||||
| 1066 | |||||||||||||||||||||||||||||||||||||||||||
| 1067 | =item * document hoh 'on_store/on_collide' custom keys | ||||||||||||||||||||||||||||||||||||||||||
| 1068 | |||||||||||||||||||||||||||||||||||||||||||
| 1069 | =item * add a recipes/examples section to cover grep and on_collide examples | ||||||||||||||||||||||||||||||||||||||||||
| 1070 | |||||||||||||||||||||||||||||||||||||||||||
| 1071 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1072 | |||||||||||||||||||||||||||||||||||||||||||
| 1073 | =head1 BUGS | ||||||||||||||||||||||||||||||||||||||||||
| 1074 | |||||||||||||||||||||||||||||||||||||||||||
| 1075 | Please report any bugs or feature requests to C<bug-text-xsv-slurp at rt.cpan.org>, or through | ||||||||||||||||||||||||||||||||||||||||||
| 1076 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Text-xSV-Slurp>. I will be notified, and then you'll | ||||||||||||||||||||||||||||||||||||||||||
| 1077 | automatically be notified of progress on your bug as I make changes. | ||||||||||||||||||||||||||||||||||||||||||
| 1078 | |||||||||||||||||||||||||||||||||||||||||||
| 1079 | |||||||||||||||||||||||||||||||||||||||||||
| 1080 | =head1 SUPPORT | ||||||||||||||||||||||||||||||||||||||||||
| 1081 | |||||||||||||||||||||||||||||||||||||||||||
| 1082 | You can find documentation for this module with the perldoc command. | ||||||||||||||||||||||||||||||||||||||||||
| 1083 | |||||||||||||||||||||||||||||||||||||||||||
| 1084 | perldoc Text::xSV::Slurp | ||||||||||||||||||||||||||||||||||||||||||
| 1085 | |||||||||||||||||||||||||||||||||||||||||||
| 1086 | |||||||||||||||||||||||||||||||||||||||||||
| 1087 | You can also look for information at: | ||||||||||||||||||||||||||||||||||||||||||
| 1088 | |||||||||||||||||||||||||||||||||||||||||||
| 1089 | =over 4 | ||||||||||||||||||||||||||||||||||||||||||
| 1090 | |||||||||||||||||||||||||||||||||||||||||||
| 1091 | =item * RT: CPAN's request tracker | ||||||||||||||||||||||||||||||||||||||||||
| 1092 | |||||||||||||||||||||||||||||||||||||||||||
| 1093 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Text-xSV-Slurp> | ||||||||||||||||||||||||||||||||||||||||||
| 1094 | |||||||||||||||||||||||||||||||||||||||||||
| 1095 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||||||||||||||||||||||||||||||||||
| 1096 | |||||||||||||||||||||||||||||||||||||||||||
| 1097 | L<http://annocpan.org/dist/Text-xSV-Slurp> | ||||||||||||||||||||||||||||||||||||||||||
| 1098 | |||||||||||||||||||||||||||||||||||||||||||
| 1099 | =item * CPAN Ratings | ||||||||||||||||||||||||||||||||||||||||||
| 1100 | |||||||||||||||||||||||||||||||||||||||||||
| 1101 | L<http://cpanratings.perl.org/d/Text-xSV-Slurp> | ||||||||||||||||||||||||||||||||||||||||||
| 1102 | |||||||||||||||||||||||||||||||||||||||||||
| 1103 | =item * Search CPAN | ||||||||||||||||||||||||||||||||||||||||||
| 1104 | |||||||||||||||||||||||||||||||||||||||||||
| 1105 | L<http://search.cpan.org/dist/Text-xSV-Slurp/> | ||||||||||||||||||||||||||||||||||||||||||
| 1106 | |||||||||||||||||||||||||||||||||||||||||||
| 1107 | =back | ||||||||||||||||||||||||||||||||||||||||||
| 1108 | |||||||||||||||||||||||||||||||||||||||||||
| 1109 | |||||||||||||||||||||||||||||||||||||||||||
| 1110 | =head1 ACKNOWLEDGEMENTS | ||||||||||||||||||||||||||||||||||||||||||
| 1111 | |||||||||||||||||||||||||||||||||||||||||||
| 1112 | |||||||||||||||||||||||||||||||||||||||||||
| 1113 | =head1 COPYRIGHT & LICENSE | ||||||||||||||||||||||||||||||||||||||||||
| 1114 | |||||||||||||||||||||||||||||||||||||||||||
| 1115 | Copyright 2009 Dan Boorstein. | ||||||||||||||||||||||||||||||||||||||||||
| 1116 | |||||||||||||||||||||||||||||||||||||||||||
| 1117 | This program is free software; you can redistribute it and/or modify it | ||||||||||||||||||||||||||||||||||||||||||
| 1118 | under the terms of either: the GNU General Public License as published | ||||||||||||||||||||||||||||||||||||||||||
| 1119 | by the Free Software Foundation; or the Artistic License. | ||||||||||||||||||||||||||||||||||||||||||
| 1120 | |||||||||||||||||||||||||||||||||||||||||||
| 1121 | See http://dev.perl.org/licenses/ for more information. | ||||||||||||||||||||||||||||||||||||||||||
| 1122 | |||||||||||||||||||||||||||||||||||||||||||
| 1123 | |||||||||||||||||||||||||||||||||||||||||||
| 1124 | =cut | ||||||||||||||||||||||||||||||||||||||||||
| 1125 | |||||||||||||||||||||||||||||||||||||||||||
| 1126 | 10 | 1; # End of Text::xSV::Slurp | |||||||||||||||||||||||||||||||||||||||||