blib/lib/Tie/FieldVals/Row.pm

filter: Statementstmt 71.5% 244 / 341 Branchbran 50.5% 91 / 180 Conditioncond 58.3% 21 / 36 MC/DCmcdc 19.0% 4 / 21 Subroutinesub 82.7% 24 / 29 Podpod 100% 13 / 13 totaltotal 64.0% 397 / 620 CC 107 SCAR 33.9
CC
107
Cov
64%
is_matched
95.2
set_fields_as_vars
50.5
match_any
30.0
CRAP 29.7
« blib/lib/Tie/FieldVals/Select.pm ↑ Summary scripts/fv2xml »
1 package Tie::FieldVals::Row;
2 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 =head1 NAME
6  
7 Tie::FieldVals::Row - a hash tie for rows (records) of Tie::FieldVals data
8  
9 =head1 VERSION
10  
11 This describes version B<0.6203> of Tie::FieldVals::Row.
12  
13 =cut
14  
15 12 our $VERSION = '0.6203';
16  
17 =head1 SYNOPSIS
18  
19 use Tie::FieldVals::Row;
20  
21 my %person;
22 my @keys = qw(Forename Surname DateOfBirth Gender);
23 my $row_obj = tie %person, 'Tie::FieldVals::Row', fields=>\@keys;
24  
25 # set the row
26 $row_obj->set_from_string($row_str,override_keys=>1);
27  
28 # compare the row
29 if ($row_obj->match(Forename=>'Mary'))
30 {
31 # do something
32 }
33  
34 =head1 DESCRIPTION
35  
36 This is a Tie object to map a row (record) of enhanced Field:Value data to
37 a hash. This sets fixed keys so that they match the columns of the data.
38 Values can go over more than one line. Fields can have multiple values.
39  
40 Field names cannot have spaces in them, indeed, they must consist of plain
41 alphanumeric characters or underscores. They are case-sensitive.
42  
43 =cut
44  
45 12 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
46 12 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
47 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 # to make taint happy
50 12 $ENV{PATH} = "/bin:/usr/bin:/usr/local/bin";
51 12 $ENV{CDPATH} = '';
52 12 $ENV{BASH_ENV} = '';
53  
54 # for debugging
55 12 my $DEBUG = 0;
56  
57 =head1 OBJECT METHODS
58  
59 =head2 set_from_hash
60  
61 Set the hash data from a simple untied hash.
62  
63 $row_obj->set_from_hash(\%hash);
64  
65 $row_obj->set_from_hash(\%hash
66 override_keys=>1,
67 append_keys=>0);
68  
69 Arguments:
70  
71 =over
72  
73 =item append_keys
74  
75 Append to the list of official fields with the Field: contents
76 of this string.
77 (default: false)
78  
79 =item override_keys
80  
81 If override_keys is true, then the official fields, the legal
82 keys to this hash, are reset from the Field: contents of this
83 string.
84 (default: false)
85  
86 =back
87  
88 =cut
89 2 ▶sub set_from_hash ($$;%) {
Subroutine set_from_hash: called CC=9 SCAR=22.8
Pod documented
90 2 my $self = shift;
91 2 my $hash_ref = shift;
92 2 my %args = (
93 override_keys=>0,
94 append_keys=>0,
95 @_
96 );
97  
98 # if we are overriding the keys, simply clear
99 # the whole self-hash
100 2 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'override_keys'}) { } 1 1 2
101 {
102 1 %{$self->{FIELDS}} = ();
103 1 $self->{OPTIONS}->{fields} = [];
104 }
105 else
106 {
107 # otherwise, just clear the existing data
108 1 $self->CLEAR();
109 }
110  
111 2 my @fields = ();
112 2 my $cur_field = '';
113 2 foreach my $field (sort keys %{$hash_ref})
114 {
115 20 my $val = $hash_ref->{$field};
116 20 ▶ if ($args{override_keys}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'override_keys'} or $args{'append_keys'} or exists $self->{'FIELDS'}{$field}) { } 20 0 20
Condition66%4/6
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
l!l&&r!l&&!r
10100
$args{"override_keys"} or $args{"append_keys"}
l!l&&r!l&&!r
10010
MC/DC0%0/3
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
$args{"override_keys"} $args{"append_keys"} exists $self->{"FIELDS"}{$field}
Truth table50%2/4
A or B or C
A= $args{"override_keys"}B= $args{"append_keys"}C= exists $self->{"FIELDS"}{$field}
ABCresult
1XX1
01X1
0011
0000
117 || $args{append_keys}
118 || exists $self->{FIELDS}->{$field})
119 {
120 20 $cur_field = $field;
121 20 ▶ if (!defined $self->{FIELDS}->{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"FIELDS"}{$field}) 20 0 20
122 {
123 20 $self->{FIELDS}->{$field} = [];
124 }
125 20 ▶ if (ref $val)
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 20 20
126 {
127 0 push @{$self->{FIELDS}->{$field}}, @{$val};
128 }
129 else
130 {
131 20 push @{$self->{FIELDS}->{$field}}, $val;
132 }
133 20 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"override_keys"}) 10 10 20
134 {
135 10 push @{$self->{OPTIONS}->{fields}}, $field;
136 }
137 }
138 else
139 {
140 0 carp "unknown field $field in hash";
141 }
142 }
143 } # set_from_hash
144  
145 =head2 set_from_string
146  
147 Set the hash data from an enhanced Field:Value data string.
148  
149 $row_obj->set_from_string($record_str);
150  
151 $row_obj->set_from_string($record_str,
152 override_keys=>1,
153 append_keys=>0);
154  
155 The format of the string is basically a multi-line string
156 in Field:Value format, with the addition that if a line does
157 not start with a known fieldname followed by a colon, that
158 the contents of that line is added to the value of the previous
159 field.
160  
161 If a particular FieldName is repeated, its value is added to
162 the existing value of that FieldName, and it becomes a
163 multi-value field.
164  
165 Arguments:
166  
167 =over
168  
169 =item append_keys
170  
171 Append to the list of official fields with the Field: contents
172 of this string.
173 (default: false)
174  
175 =item override_keys
176  
177 If override_keys is true, then the official fields, the legal
178 keys to this hash, are reset from the Field: contents of this
179 string.
180 (default: false)
181  
182 =back
183  
184 =cut
185 783 ▶sub set_from_string ($$;%) {
Subroutine set_from_string: called CC=11 SCAR=24.6
Pod documented
186 783 my $self = shift;
187 783 my $record_str = shift;
188 783 my %args = (
189 override_keys=>0,
190 append_keys=>0,
191 @_
192 );
193  
194 # if we are overriding the keys, simply clear
195 # the whole self-hash
196 783 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'override_keys'}) { } 12 771 783
197 {
198 12 %{$self->{FIELDS}} = ();
199 12 $self->{OPTIONS}->{fields} = [];
200 }
201 else
202 {
203 # otherwise, just clear the existing data
204 771 $self->CLEAR();
205 }
206  
207 # the lines contain either field:value pairs
208 # or continuations of the previous field's value
209 783 my @fields = ();
210 783 my @lines = split(/\n/, $record_str);
211 783 my $cur_field = '';
212 783 ▶ while (@lines)
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 9757 783 10540
213 {
214 9757 my $line = shift @lines;
215 9757 ▶ if ($line =~ /^([a-zA-Z][-_a-zA-Z0-9]*):(.*)$/)
Branch: 2 branches
BranchTrueFalseTotal
if ($line =~ /^([a-zA-Z][-_a-zA-Z0-9]*):(.*)$/) { } 8345 1412 9757
elsif ($cur_field) { } 1412 0 1412
216 {
217 8345 my $field = $1;
218 8345 my $val = $2;
219 8345 ▶ if ($args{override_keys}
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'override_keys'} or $args{'append_keys'} or exists $self->{'FIELDS'}{$field}) { } 8345 0 8345
elsif ($cur_field) { } 0 0 0
Condition66%4/6
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
l!l&&r!l&&!r
12282230
$args{"override_keys"} or $args{"append_keys"}
l!l&&r!l&&!r
12208223
MC/DC0%0/3
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
$args{"override_keys"} $args{"append_keys"} exists $self->{"FIELDS"}{$field}
Truth table50%2/4
A or B or C
A= $args{"override_keys"}B= $args{"append_keys"}C= exists $self->{"FIELDS"}{$field}
ABCresult
1XX1
01X1
0011
0000
220 || $args{append_keys}
221 || exists $self->{FIELDS}->{$field})
222 {
223 8345 $cur_field = $field;
224 8345 ▶ if (!defined $self->{FIELDS}->{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"FIELDS"}{$field}) 8343 2 8345
225 {
226 8343 $self->{FIELDS}->{$field} = [];
227 }
228 8345 push @{$self->{FIELDS}->{$field}}, $val;
229 8345 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"override_keys"}) 122 8223 8345
230 {
231 122 push @{$self->{OPTIONS}->{fields}}, $field;
232 }
233 }
234 elsif ($cur_field)
235 {
236 # not a field -- must be a value
237 # append the current line to the last field
238 0 my $count = @{$self->{FIELDS}->{$cur_field}};
239 0 ${$self->{FIELDS}->{$cur_field}}[$count - 1] .= "\n$line";
240 }
241 }
242 elsif ($cur_field)
243 {
244 # append the current line to the last field
245 1412 my $count = @{$self->{FIELDS}->{$cur_field}};
246 1412 ${$self->{FIELDS}->{$cur_field}}[$count - 1] .= "\n$line";
247 }
248 }
249 } # set_from_string
250  
251 =head2 set_from_xml_string
252  
253 Set the hash data from an XML string.
254  
255 $row_obj->set_from_xml_string($record_str);
256  
257 $row_obj->set_from_xml_string($record_str,
258 override_keys=>1,
259 clear=>1);
260  
261 The format of this XML string is as follows:
262  
263 <record>
264 <Field>Value</Field>
265 <AnotherField>AnotherValue</AnotherField>
266 ...
267 </record>
268  
269 If a particular FieldName is repeated, its value is added to
270 the existing value of that FieldName, and it becomes a
271 multi-value field.
272  
273 Arguments:
274  
275 =over
276  
277 =item append_keys
278  
279 Append to the list of official fields with the <Field> contents
280 of this string.
281 (default: false)
282  
283 =item override_keys
284  
285 If override_keys is true, then the official fields, the legal
286 keys to this hash, are reset from the <Field> contents of this
287 string.
288 (default: false)
289  
290 =back
291  
292 =cut
293 119 ▶sub set_from_xml_string ($$;%) {
Subroutine set_from_xml_string: called CC=10 SCAR=23.1
Pod documented
294 119 my $self = shift;
295 119 my $record_str = shift;
296 119 my %args = (
297 override_keys=>0,
298 append_keys=>0,
299 @_
300 );
301  
302 # if we are overriding the keys, simply clear
303 # the whole self-hash
304 119 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'override_keys'}) { } 1 118 119
305 {
306 1 %{$self->{FIELDS}} = ();
307 1 $self->{OPTIONS}->{fields} = [];
308 }
309 else
310 {
311 # otherwise, just clear the existing data
312 118 $self->CLEAR();
313 }
314  
315 # record_str should contain ...<record>...</record>
316 # or just the fields
317 119 ▶ if ($record_str =~ m#<record>(.*)</record>#s)
Branch: 1 branch
BranchTrueFalseTotal
if ($record_str =~ m[<record>(.*)</record>]s) 119 0 119
318 {
319 119 $record_str = $1;
320 }
321 # now record_str should just contain the fields
322 # eg <Author>...</Author>...<Review>..</Review>
323 119 my @all_fields = split(/(<[a-zA-Z][-_a-zA-Z0-9]*>|<\/[a-zA-Z][-_a-zA-Z0-9]*>)/, $record_str);
324 119 ▶ while (@all_fields)
Branch: 1 branch
BranchTrueFalseTotal
if (@all_fields) 4046 119 4165
325 {
326 4046 my $fld = shift @all_fields;
327 # is this a valid start-tag?
328 4046 ▶ if ($fld =~ m#<([a-zA-Z][-_a-zA-Z0-9]*)>#)
Branch: 1 branch
BranchTrueFalseTotal
if ($fld =~ /<([a-zA-Z][-_a-zA-Z0-9]*)>/) 1309 2737 4046
329 {
330 1309 my $field = $1;
331 # is this a legal key?
332 1309 ▶ if ($args{override_keys}
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}) 1309 0 1309
Condition66%4/6
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
l!l&&r!l&&!r
1112980
$args{"override_keys"} or $args{"append_keys"}
l!l&&r!l&&!r
1101298
MC/DC0%0/3
$args{"override_keys"} or $args{"append_keys"} or exists $self->{"FIELDS"}{$field}
$args{"override_keys"} $args{"append_keys"} exists $self->{"FIELDS"}{$field}
Truth table50%2/4
A or B or C
A= $args{"override_keys"}B= $args{"append_keys"}C= exists $self->{"FIELDS"}{$field}
ABCresult
1XX1
01X1
0011
0000
333 || $args{append_keys}
334 || exists $self->{FIELDS}->{$field})
335 {
336 1309 my $val = shift @all_fields;
337 # restore the special characters to their real meanings
338 1309 $val =~ s/&gt;/>/g;
339 1309 $val =~ s/&lt;/</g;
340 1309 $val =~ s/&quot;/"/g;
341 1309 $val =~ s/&apos;/'/g;
342 1309 $val =~ s/&amp;/&/g;
343 1309 ▶ if (!defined $self->{FIELDS}->{$field})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $self->{"FIELDS"}{$field}) 1309 0 1309
344 {
345 1309 $self->{FIELDS}->{$field} = [];
346 }
347 1309 push @{$self->{FIELDS}->{$field}}, $val;
348 }
349 1309 ▶ if ($args{override_keys})
Branch: 1 branch
BranchTrueFalseTotal
if ($args{"override_keys"}) 11 1298 1309
350 {
351 11 push @{$self->{OPTIONS}->{fields}}, $field;
352 }
353 }
354 }
355 } # set_from_xml_string
356  
357 =head2 get_as_string
358  
359 Returns the hash data as a string in the same format as
360 expected by L</set_from_string>.
361  
362 my $str = $row_obj->get_as_string();
363  
364 my $str = $row_obj->get_as_string(fields=>\@fields);
365  
366 If B<fields> is defined, then return a string which is made up
367 of only that subset of the fields given by the @fields array.
368  
369 =cut
370 132 ▶sub get_as_string ($;%) {
Subroutine get_as_string: called CC=5 SCAR=16.1
Pod documented
371 132 my $self = shift;
372 132 my %args = (
373 fields=>undef,
374 @_
375 );
376  
377 132 my $out = '';
378 my $fields_ref = (defined $args{fields}
379 132 ▶ ? $args{fields} : $self->{OPTIONS}->{fields});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'fields'} ? : 0 132 132
380 132 foreach my $field (@{$fields_ref})
381 {
382 1443 ▶ if ($self->EXISTS($field))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->EXISTS($field)) 1443 0 1443
383 {
384 1443 my $num_vals = $self->field_count($field);
385 1443 my $aref = $self->FETCH(\$field);
386 1443 ▶ for (my $i=0; $i < $num_vals; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_vals) 1443 1443 2886
387 {
388 1443 my $val = $aref->[$i];
389 1443 $out .= "${field}:";
390 1443 $out .= $val;
391 1443 $out .= "\n";
392 }
393 }
394 }
395 132 $out =~ s/\n$//;
396  
397 132 return $out;
398 } # get_as_string
399  
400 =head2 get_xml_string
401  
402 Returns the hash data as an XML string in the same
403 format as expected by L</set_from_xml_string>.
404  
405 my $str = $row_obj->get_xml_string();
406  
407 my $str = $row_obj->get_xml_string(fields=>\@fields);
408  
409 If B<fields> is defined, then return a string which is made up
410 of only that subset of the fields given by the @fields array.
411  
412 =cut
413 119 ▶sub get_xml_string ($;%) {
Subroutine get_xml_string: called CC=5 SCAR=16.1
Pod documented
414 119 my $self = shift;
415 119 my %args = (
416 fields=>undef,
417 @_
418 );
419  
420 119 my $out = '';
421 119 $out .= "<record>\n";
422 my $fields_ref = (defined $args{fields}
423 119 ▶ ? $args{fields} : $self->{OPTIONS}->{fields});
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'fields'} ? : 0 119 119
424 119 foreach my $field (@{$fields_ref})
425 {
426 1309 ▶ if ($self->EXISTS($field))
Branch: 1 branch
BranchTrueFalseTotal
if ($self->EXISTS($field)) 1309 0 1309
427 {
428 1309 my $num_vals = $self->field_count($field);
429 1309 my $aref = $self->FETCH(\$field);
430 1309 ▶ for (my $i=0; $i < $num_vals; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_vals) 1309 1309 2618
431 {
432 1309 my $val = $$aref[$i];
433 1309 $val =~ s/&/&amp;/g;
434 1309 $val =~ s/</&lt;/g;
435 1309 $val =~ s/>/&gt;/g;
436 1309 $out .= "<${field}>";
437 1309 $out .= $val;
438 1309 $out .= "</${field}>";
439 1309 $out .= "\n";
440 }
441 }
442 }
443 119 $out .= "</record>\n";
444  
445 119 return $out;
446 } # get_xml_string
447  
448 =head2 field_names
449  
450 my @field_names = @{$row_obj->field_names()};
451  
452 Return the names of the fields in the order they were defined,
453 rather than the random order that "keys" would give.
454 This will either be the array which was used when the hash
455 was tied, or the order that fields were read from a string
456 if set_from_string or set_from_xml_string is called with
457 override_fields true.
458  
459 =cut
460 16 ▶sub field_names ($) {
Subroutine field_names: called CC=1 SCAR=0.0
Pod documented
461 16 my $self = shift;
462  
463 16 return $self->{OPTIONS}->{fields};
464 } # field_names
465  
466 =head2 field_count
467  
468 my $cnt = $row_obj->field_count($field_name);
469  
470 Return the number of different field values for the
471 given field in the given Row. A multi-valued field
472 will give a count greater than 1.
473  
474 If there is no value defined for the given field, then returns zero.
475  
476 =cut
477 2752 ▶sub field_count ($$) {
Subroutine field_count: called CC=5 SCAR=20.4
Pod documented
478 2752 my $self = shift;
479 2752 my $field_name = shift;
480  
481 2752 my $count = 0;
482 2752 ▶ if (!exists $self->{FIELDS}->{$field_name}
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"FIELDS"}{$field_name} and defined $self->{"FIELDS"}{$field_name}) 0 2752 2752
Condition33%1/3
exists $self->{"FIELDS"}{$field_name} and defined $self->{"FIELDS"}{$field_name}
!ll&&!rl&&r
002752
MC/DC0%0/2
exists $self->{"FIELDS"}{$field_name} and defined $self->{"FIELDS"}{$field_name}
exists $self->{"FIELDS"}{$field_name} defined $self->{"FIELDS"}{$field_name}
Truth table33%1/3
A and B
A= exists $self->{"FIELDS"}{$field_name}B= defined $self->{"FIELDS"}{$field_name}
ABresult
0X0
100
111
483 || !defined $self->{FIELDS}->{$field_name})
484 {
485 0 return 0;
486 }
487  
488 2752 ▶ if (ref($self->{FIELDS}->{$field_name}) eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $self->{'FIELDS'}{$field_name} eq 'ARRAY') { } 2752 0 2752
elsif (not ref $self->{'FIELDS'}{$field_name}) { } 0 0 0
489 {
490 2752 $count = @{$self->{FIELDS}->{$field_name}};
491 }
492 elsif (!ref($self->{FIELDS}->{$field_name}))
493 {
494 0 $count = 1;
495 }
496 else
497 {
498 0 warn "record->${field_name} not array";
499 0 warn Dumper($self->{FIELDS});
500 }
501  
502 2752 return $count;
503 } # field_count
504  
505 =head2 set_fields_as_vars
506  
507 $row_obj->set_fields_as_vars($package_name);
508  
509 $row_obj->set_fields_as_vars($package_name,
510 field_ind=>$field_ind);
511  
512 Sets the data of the hash as variables with the same name as the
513 field name; multi-valued fields have arrays of the field name.
514  
515 These are set in the given package.
516  
517 Arguments:
518  
519 =over
520  
521 =item field_ind
522  
523 For multi-valued fields, the @I<Field> variable is set, but also the
524 $I<Field> variable will be set, to the value of the variable with
525 B<field_ind> index. (default: 0)
526  
527 =back
528  
529 =cut
530 0 ▶sub set_fields_as_vars ($;%) {
Subroutine set_fields_as_vars: not called CC=8 SCAR=39.2
Pod documented
531 0 my $self = shift;
532 0 my $pkg_name = shift;
533 0 my %args = (
534 field_ind=>0,
535 @_
536 );
537  
538 0 my $field_ind = $args{field_ind};
539  
540 0 ▶ while (my ($key, $value) = each %{$self->{FIELDS}})
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"FIELDS"};}) 0 0 0
541 {
542 0 $key =~ m#([a-zA-Z0-9][-_a-zA-Z0-9]*)#; # keep taint happy
543 0 my $field = $1;
544 0 my $varname = "${pkg_name}::${field}";
545 0 ▶ if (ref $value eq 'ARRAY')
Branch: 2 branches
BranchTrueFalseTotal
if (ref $value eq 'ARRAY') { } 0 0 0
elsif (not ref $value) { } 0 0 0
546 {
547 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
548 0 my $num_vals = @{$value};
549 0 ▶ for (my $i=0; $i < $num_vals; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $num_vals) 0 0 0
550 {
551 0 my $tval = ${$value}[$i];
552 0 $tval =~ m#([^`]*)#s;
553 0 my $val = $1;
554 0 ▶ if ($num_vals > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($num_vals > 0) { } 0 0 0
555 {
556 0 ▶ if ($i == 0)
Branch: 2 branches
BranchTrueFalseTotal
if ($i == 0) { } 0 0 0
elsif ($i == $field_ind) { } 0 0 0
557 {
558 0 $$varname = $val;
559 0 @$varname = ();
560 }
561 elsif ($i == $field_ind)
562 {
563 0 $$varname = $val;
564 }
565 0 $$varname[$i] = $val;
566 }
567 else
568 {
569 0 $$varname = $val;
570 }
571 }
572 }
573 elsif (!ref $value)
574 {
575 12 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
576 0 $value =~ m#([^`]*)#s;
577 0 my $val = $1;
578 0 $$varname = $val;
579 }
580 }
581 } # set_fields_as_vars
582  
583 =head2 match
584  
585 $row_obj->match(Author=>qr/Li.a/,
586 Universe=>'Buffy',
587 Year=>'> 2001')
588  
589 Checks if this row matches the hash.
590 The hash is in the form of Field => value pairs, where
591 the value can be a plain value,
592 a comparison (< > = eq ne ...)
593 or a regular expression.
594  
595 If the plain value or the comparison starts with '!'
596 then the sense of the comparison is reversed.
597  
598 Returns:
599 1 if matches all conditions, 0 if fails
600  
601 =cut
602 671 ▶sub match ($%) {
Subroutine match: called CC=5 SCAR=16.1
Pod documented
603 671 my $self = shift;
604 671 my %match = (@_);
605 671 my $fields = $self->{FIELDS};
606 671 my $retval = 0;
607  
608 671 my $found = 0;
609 671 ▶ while (my ($fn, $re) = each %match)
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %match) 552 671 1223
610 {
611 552 my $val = $self->FETCH($fn);
612 552 ▶ if (defined $val and is_matched($val, $re))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and is_matched($val, $re)) 164 388 552
Condition66%2/3
defined $val and is_matched($val, $re)
!ll&&!rl&&r
0388164
MC/DC50%1/2
defined $val and is_matched($val, $re)
defined $val is_matched($val, $re)
Truth table66%2/3
A and B
A= defined $valB= is_matched($val, $re)
ABresult
0X0
100
111
613 {
614 164 $found++;
615 }
616 }
617 671 ▶ $retval = 1 if $found == scalar keys %match;
Branch: 1 branch
BranchTrueFalseTotal
if $found == scalar keys %match 283 388 671
618  
619 671 return $retval;
620 } # match
621  
622 =head2 match_any
623  
624 $row_obj->match_any($match_str);
625  
626 Checks if any field in this row matches the string.
627  
628 Returns:
629 1 if any field matches the string, 0 if fails
630  
631 =cut
632 0 ▶sub match_any ($$) {
Subroutine match_any: not called CC=5 SCAR=34.0
Pod documented
633 0 my $self = shift;
634 0 my $match_str = shift;
635 0 my $fields = $self->{FIELDS};
636 0 my $retval = 0;
637  
638 0 my $found = 0;
639 0 ▶ while (my $fn = each %{$fields})
Branch: 1 branch
BranchTrueFalseTotal
if (defined(my $fn = each %{$fields;})) 0 0 0
640 {
641 0 my $val = $self->FETCH($fn);
642 0 ▶ if (defined $val and is_matched($val, $match_str))
Branch: 1 branch
BranchTrueFalseTotal
if (defined $val and is_matched($val, $match_str)) 0 0 0
Condition0%0/3
defined $val and is_matched($val, $match_str)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $val and is_matched($val, $match_str)
defined $val is_matched($val, $match_str)
Truth table0%0/3
A and B
A= defined $valB= is_matched($val, $match_str)
ABresult
0X0
100
111
643 {
644 0 $found++;
645 }
646 }
647 0 ▶ $retval = 1 if ($found > 0);
Branch: 1 branch
BranchTrueFalseTotal
if $found > 0 0 0 0
648  
649 0 return $retval;
650 } # match_any
651  
652 =head1 Tie-Hash METHODS
653  
654 =head2 TIEHASH
655  
656 Create a new instance of the object as tied to a hash.
657  
658 tie %person, 'Tie::FieldVals::Row', fields=>\@keys;
659  
660 The B<fields> argument defines the names of the legal fields.
661 Legal fields can also be set from a string when using the B<override_keys>
662 argument to L</set_from_string> or L</set_from_xml_string>.
663  
664 =cut
665 906 ▶sub TIEHASH {
Subroutine TIEHASH: called CC=3 SCAR=11.1
666 906 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 906 906
667 906 my $class = shift;
668 906 my %args = (
669 fields=>undef,
670 @_
671 );
672 906 ▶ if (!defined $args{fields})
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $args{"fields"}) 0 906 906
673 {
674 0 croak "Tie::FieldVals::Row -- no fields given";
675 }
676 906 my @keys = @{$args{fields}};
677  
678 906 my %hash;
679  
680 906 @hash{@keys} = (undef) x @keys;
681 906 my $self = {};
682 906 $self->{FIELDS} = \%hash;
683 906 $self->{OPTIONS} = \%args;
684  
685 906 bless $self, $class;
686 } # TIEHASH
687  
688 =head2 FETCH
689  
690 Get a key=>value from the hash.
691 Some values may be multi-values, and can either be gotten as an array
692 reference or joined together.
693 If a key is not an official key, undefined is returned.
694  
695 $val = $hash{$key}
696  
697 Gets the value, or if it is a multi-value, gets the values joined
698 by spaces.
699  
700 $val = $hash{\$key}
701  
702 Gets the whole key field as an array ref.
703  
704 $match = {$key=>'##'};
705 $val = $hash{$match};
706  
707 $match = [$key, '##'];
708 $val = $hash{$match};
709  
710 Gets the value, or if it is a multi-value, gets the values joined
711 by the given string (in this case, '##').
712  
713 See also L</field_count> to determine whether a field is a multi-valued
714 field.
715  
716 =cut
717 3868 ▶sub FETCH {
Subroutine FETCH: called CC=10 SCAR=29.8
718 3868 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 3868 3868
719 3868 my ($self, $match) = @_;
720 3868 my $key = '';
721 3868 my $separator = ' ';
722 3868 my $return_array = 0;
723  
724 3868 ▶ if (ref $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match) { } 3116 752 3868
725 3116 ▶ if (ref $match eq 'SCALAR') {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $match eq 'SCALAR') { } 3116 0 3116
elsif (ref $match eq 'HASH') { } 0 0 0
elsif (ref $match eq 'ARRAY') { } 0 0 0
726 3116 $key = $$match;
727 3116 $return_array = 1;
728 }
729 elsif (ref $match eq 'HASH') {
730 0 my @keys = keys %{$match};
731 0 $key = shift @keys;
732 0 $separator = $match->{$key};
733 }
734 elsif (ref $match eq 'ARRAY') {
735 0 $key = shift @{$match};
736 0 $separator = shift @{$match};
737 }
738 else {
739 0 carp "invalid match '", ref $match, "' to FETCH hash";
740 0 return undef;
741 }
742 }
743 else {
744 752 $key = $match; # just a plain key
745 }
746  
747 3868 ▶ unless (exists $self->{FIELDS}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"FIELDS"}{$key}) 0 3868 3868
748 0 return undef;
749 }
750  
751 3868 ▶ if (ref $self->{FIELDS}->{$key} eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $self->{'FIELDS'}{$key} eq 'ARRAY') { } 3868 0 3868
752 3868 my $count = @{$self->{FIELDS}->{$key}};
753  
754 3868 ▶ if ($return_array)
Branch: 2 branches
BranchTrueFalseTotal
if ($return_array) { } 3116 752 3868
elsif ($count == 1) { } 752 0 752
755 {
756 3116 return $self->{FIELDS}->{$key};
757 }
758 # if there's only one, return it
759 elsif ($count == 1) {
760 752 return @{$self->{FIELDS}->{$key}}[0];
761 }
762 else {
763 # otherwise, return the values joined together
764 0 return join($separator, @{$self->{FIELDS}->{$key}});
765 }
766 }
767 else {
768 0 return $self->{FIELDS}->{$key};
769 }
770  
771 } # FETCH
772  
773 =head2 STORE
774  
775 Add a key=>value to the hash.
776 Either add a single value, or an array reference to create a
777 multi-value.
778  
779 If a key is not an official key, nothing is set, and it
780 complains of error.
781  
782 $hash{$key} = $val;
783 $hash{$key} = [$v1,$v2,$v3];
784  
785 =cut
786 4 ▶sub STORE {
Subroutine STORE: called CC=6 SCAR=24.5
787 4 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 4 4
788 4 my ($self, $match, $val) = @_;
789 4 my $key = '';
790  
791 4 ▶ if (ref $match) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $match) { } 0 4 4
792 0 carp "invalid match '", ref $match, "' to STORE hash";
793 0 return undef;
794 }
795 else {
796 4 $key = $match; # just a plain key
797 }
798 4 ▶ unless (exists $self->{FIELDS}->{$key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (exists $self->{"FIELDS"}{$key}) 0 4 4
799  
800 0 carp "invalid key [$key] in hash\n";
801 0 return undef;
802 }
803  
804 4 ▶ if (ref $val) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val) { } 0 4 4
805 0 ▶ if (ref $val eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $val eq 'ARRAY') { } 0 0 0
806 0 $self->{FIELDS}->{$key} = $val;
807 }
808 else
809 {
810 0 carp "invalid value reference '", ref $val, "' to STORE hash";
811 0 return undef;
812 }
813 }
814 else {
815 4 $self->{FIELDS}->{$key} = [$val];
816 }
817  
818 } # STORE
819  
820 =head2 DELETE
821  
822 Remove a key=>value from the hash, only if it exists.
823  
824 =cut
825 0 ▶sub DELETE {
Subroutine DELETE: not called CC=3 SCAR=24.8
826 0 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 0 0
827 0 my ($self, $key) = @_;
828  
829 0 ▶ return unless exists $self->{FIELDS}->{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $self->{"FIELDS"}{$key} 0 0 0
830  
831 0 my $ret = $self->{FIELDS}->{$key};
832  
833 0 $self->{FIELDS}->{$key} = undef;
834  
835 0 return $ret;
836 } # DELETE
837  
838 =head2 CLEAR
839  
840 Remove all the data from the hash.
841  
842 =cut
843 890 ▶sub CLEAR {
Subroutine CLEAR: called CC=3 SCAR=11.1
844 890 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 890 890
845 890 my $self = shift;
846  
847 890 $self->{FIELDS}->{$_} = undef foreach keys %{$self->{FIELDS}};
848 } # CLEAR
849  
850 =head2 EXISTS
851  
852 Does this key exist?
853  
854 =cut
855 2862 ▶sub EXISTS {
Subroutine EXISTS: called CC=2 SCAR=7.0
856 2862 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 2862 2862
857 2862 my $self = shift;
858 2862 my $key = shift;
859  
860 2862 return exists $self->{FIELDS}->{$key};
861 } # EXISTS
862  
863 =head2 FIRSTKEY
864  
865 Get the first key of this hash.
866  
867 =cut
868 18 ▶sub FIRSTKEY {
Subroutine FIRSTKEY: called CC=2 SCAR=7.0
869 18 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 18 18
870 18 my $self = shift;
871  
872 18 my $a = keys %{$self->{FIELDS}}; # reset each() iterator
873 18 each %{$self->{FIELDS}};
874 } # FIRSTKEY
875  
876 =head2 NEXTKEY
877  
878 Get the next key of this hash.
879  
880 =cut
881 203 ▶sub NEXTKEY {
Subroutine NEXTKEY: called CC=2 SCAR=7.0
882 203 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 203 203
883 203 my $self = shift;
884 203 my $lastkey = shift; # previous key
885  
886 203 each %{$self->{FIELDS}};
887 } # NEXTKEY
888  
889 906 ▶sub DESTROY {
Subroutine DESTROY: called CC=2 SCAR=7.6
890 906 ▶ carp &whowasi if $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
if $DEBUG 0 906 906
891 }
892  
893 =head1 PRIVATE METHODS
894  
895 For developer reference only.
896  
897 =head2 debug
898  
899 Set debugging on.
900  
901 =cut
902 0 ▶sub debug { $DEBUG = @_ ? shift : 1 }
Subroutine debug: not called CC=2 SCAR=17.9
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@_ ? : 0 0 0
903  
904 =head2 whowasi
905  
906 For debugging: say who called this
907  
908 =cut
909 0 ▶sub whowasi { (caller(1))[3] . '()' }
Subroutine whowasi: not called CC=1 SCAR=6.9
Pod documented
910  
911 =head2 is_matched($str,$re)
912  
913 Check if the string matches the expression.
914  
915 =cut
916 552 ▶sub is_matched {
Subroutine is_matched: called CC=28 SCAR=45.6
Pod documented
917 552 my($str,$re)=@_;
918 552 ▶ if (ref $re eq 'Regexp') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $re eq "Regexp") 0 552 552
919 0 ▶ return $str =~ /$re/ ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 0 0 0
920 }
921 552 my $op;
922 my $val;
923 552 my $negate = 0;
924 552 my $retval = 0;
925  
926 # if it starts with a ! and isn't !=
927 # then negate the match
928 552 ▶ if ($re and $re =~ /^![^=]/)
Branch: 1 branch
BranchTrueFalseTotal
if ($re and $re =~ /^![^=]/) 0 552 552
Condition33%1/3
$re and $re =~ /^![^=]/
!ll&&!rl&&r
05520
MC/DC0%0/2
$re and $re =~ /^![^=]/
$re $re =~ /^![^=]/
Truth table33%1/3
A and B
A= $reB= $re =~ /^![^=]/
ABresult
0X0
100
111
929 {
930 0 $negate = 1;
931 0 $re =~ s/^!//;
932 }
933 552 ▶ if ( $re and $re =~/^(\S*)\s+(.*)/ ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($re and $re =~ /^(\S*)\s+(.*)/) { } 314 238 552
elsif ($re) { } 238 0 238
Condition66%2/3
$re and $re =~ /^(\S*)\s+(.*)/
!ll&&!rl&&r
0238314
MC/DC50%1/2
$re and $re =~ /^(\S*)\s+(.*)/
$re $re =~ /^(\S*)\s+(.*)/
Truth table66%2/3
A and B
A= $reB= $re =~ /^(\S*)\s+(.*)/
ABresult
0X0
100
111
934 314 $op = $1;
935 314 $val = $2;
936  
937 314 my $numop = '< > == != <= >=';
938 314 my $chrop = 'lt gt eq ne le ge';
939 314 ▶ if (!($numop =~ /$op/) and !($chrop =~ /$op/)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not $numop =~ /$op/ || $chrop =~ /$op/) { } 25 289 314
elsif ($numop =~ /$op/) { } 119 170 289
Condition100%3/3
$numop =~ /$op/ || $chrop =~ /$op/
l!l&&r!l&&!r
11917025
MC/DC100%2/2
$numop =~ /$op/ || $chrop =~ /$op/
$numop =~ /$op/ $chrop =~ /$op/
Truth table100%3/3
A || B
A= $numop =~ /$op/B= $chrop =~ /$op/
ABresult
1X1
011
000
940 25 ▶ $retval = ($str =~ /$re/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 4 21 25
941 }
942 elsif ($numop =~ /$op/) {
943 119 ▶ my $num = ($str ? $str : 0);
Branch: 1 branch
BranchTrueFalseTotal
$str ? : 108 11 119
944 0 ▶ if ($op eq '<' ) { $retval = ($num < $val); }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq '<') { } 0 119 119
elsif ($op eq '>') { } 0 119 119
elsif ($op eq '==') { } 0 119 119
elsif ($op eq '!=') { } 0 119 119
elsif ($op eq '<=') { } 0 119 119
elsif ($op eq '>=') { } 119 0 119
945 0 elsif ($op eq '>' ) { $retval = ($num > $val); }
946 0 elsif ($op eq '==') { $retval = ($num == $val); }
947 0 elsif ($op eq '!=') { $retval = ($num != $val); }
948 0 elsif ($op eq '<=') { $retval = ($num <= $val); }
949 119 elsif ($op eq '>=') { $retval = ($num >= $val); }
950 } else {
951 0 ▶ if ($op eq 'lt') { $retval = ($str lt $val); }
Branch: 6 branches
BranchTrueFalseTotal
if ($op eq 'lt') { } 0 170 170
elsif ($op eq 'gt') { } 0 170 170
elsif ($op eq 'eq') { } 170 0 170
elsif ($op eq 'ne') { } 0 0 0
elsif ($op eq 'le') { } 0 0 0
elsif ($op eq 'ge') { } 0 0 0
952 0 elsif ($op eq 'gt') { $retval = ($str gt $val); }
953 170 elsif ($op eq 'eq') { $retval = ($str eq $val); }
954 0 elsif ($op eq 'ne') { $retval = ($str ne $val); }
955 0 elsif ($op eq 'le') { $retval = ($str le $val); }
956 0 elsif ($op eq 'ge') { $retval = ($str ge $val); }
957 }
958  
959 }
960 elsif ($re) {
961 238 ▶ $retval = ($str =~ /$re/ ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /$re/ ? : 5 233 238
962 }
963 else {
964 0 ▶ $retval = ($str eq '' ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
$str eq '' ? : 0 0 0
965 }
966  
967 552 ▶ if ($negate)
Branch: 1 branch
BranchTrueFalseTotal
if ($negate) 0 552 552
968 {
969 0 return (!$retval);
970 }
971 552 return $retval;
972 }
973  
974 =head1 REQUIRES
975  
976 Test::More
977 Carp
978  
979 =head1 SEE ALSO
980  
981 perl(1).
982 L<Tie::FieldVals>
983  
984 =head1 BUGS
985  
986 Please report any bugs or feature requests to the author.
987  
988 =head1 AUTHOR
989  
990 Kathryn Andersen (RUBYKAT)
991 perlkat AT katspace dot com
992 http://www.katspace.com
993  
994 =head1 COPYRIGHT AND LICENCE
995  
996 Copyright (c) 2004 by Kathryn Andersen
997  
998 This program is free software; you can redistribute it and/or modify it
999 under the same terms as Perl itself.
1000  
1001  
1002 =cut
1003  
1004 12 1; # End of Tie::FieldVals::Row
1005 __END__
« blib/lib/Tie/FieldVals/Select.pm ↑ Summary scripts/fv2xml »