WARC/Fields.pm

filter: Statementstmt 100% 735 / 735 Branchbran 100% 268 / 270 Conditioncond 100% 32 / 32 MC/DCmcdc 100% 19 / 19 Subroutinesub 98.0% 99 / 101 Podpod 100% 7 / 7 totaltotal 99.8% 1160 / 1164 CC 161 SCAR 15.5
CC
161
Cov
100%
_set_multiple_value
11.0
field
10.0
SPLICE
10.0
CRAP 4.7
« WARC/Index/File/CDX.pm ↑ Summary WARC/Record/Block.pm »
1 package WARC::Fields; # -*- CPerl -*-
2  
3 28 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 28 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 28 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 28 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 28 ▶use Scalar::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 28 our @ISA = qw();
11  
12 28 ▶use WARC; *WARC::Fields::VERSION = \$WARC::VERSION;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 =head1 NAME
15  
16 WARC::Fields - WARC record headers and application/warc-fields
17  
18 =head1 SYNOPSIS
19  
20 require WARC::Fields;
21  
22 $f = new WARC::Fields;
23 $f = $record->fields; # get WARC record headers
24 $g = $f->clone; # make writable copy
25  
26 $g->set_readonly; # make read-only
27  
28 $f->field('WARC-Type' => 'metadata'); # set
29 $value = $f->field('WARC-Type'); # get
30  
31 $fields_text = $f->as_string; # get WARC header lines for display
32 $fields_block = $f->as_block; # format for WARC file
33  
34 tie @field_names, ref $f, $f; # bind ordered list of field names
35  
36 tie %fields, ref $f, $f; # bind hash of field names => values
37  
38 $entry = $f->[$num]; # tie an anonymous array and access it
39 $value = $f->{$name}; # likewise with an anonymous tied hash
40  
41 $name = "$entry"; # tied array returns objects
42 $value = $entry->value; # one specific value
43 $offset = $entry->offset; # N of M with same name
44  
45 foreach (keys %{$f}) { ... } # iterate over names, in order
46  
47 =cut
48  
49 28 ▶use overload '@{}' => \&_as_tied_array, '%{}' => \&_as_tied_hash;
Subroutine BEGIN: called CC=1 SCAR=0.0
50 28 ▶use overload fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
51  
52 # This implementation uses column-oriented storage, with an array as the
53 # underlying object and constants to select array offsets.
54 #
55 # The NAMES and VALUES columns are always valid, but the MVOFF and INDEX
56 # positions may be undefined and are lazily rebuilt when needed.
57  
58 28 use constant { NAMES => 0, VALUES => 1, MVOFF => 2,
59 28 ▶ INDEX => 3, IS_RO => 4, C_TA => 5, C_TH => 6 };
Subroutine BEGIN: called CC=1 SCAR=0.0
60 28 ▶use constant OBJECT_INDEX => qw/NAMES VALUES MVOFF INDEX IS_RO/;
Subroutine BEGIN: called CC=1 SCAR=0.0
61 28 ▶use constant OBJECT_INIT => undef, undef, undef, undef, 0, undef, undef;
Subroutine BEGIN: called CC=1 SCAR=0.0
62  
63 744 ▶sub DESTROY { my $ob = shift;
Subroutine DESTROY: called CC=3 SCAR=11.0
64 744 ▶ untie @{$$ob->[C_TA]} if defined $$ob->[C_TA];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ob->[5] 3 741 744
65 744 ▶ untie %{$$ob->[C_TH]} if defined $$ob->[C_TH];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$ob->[6] 63 681 744
66 744 our $_total_destroyed; $_total_destroyed++ }
67  
68 # NAMES: array of field names, exactly as written
69 # VALUES: array of field values
70 # MVOFF: array of offsets for multiple-valued fields
71 # INDEX: hash of case-folded field names to array of row numbers
72 # IS_RO: boolean: TRUE if this object is read-only
73  
74 # C_TA: cache: tied array for array dereference
75 # C_TH: cache: tide hash for hash dereference
76  
77 sub _rebuild_INDEX {
78 30 ▶ my $self = shift;
Subroutine _rebuild_INDEX: called CC=2 SCAR=6.9
79 30 my %idx = ();
80  
81 30 ▶ for (my $i = 0; $i < @{$$self->[NAMES]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$$self->[0];}) 83 30 113
82 83 push @{$idx{lc $$self->[NAMES][$i]}}, $i;
83 }
84  
85 30 $$self->[INDEX] = \%idx;
86 }
87  
88 sub _update_INDEX {
89 75 ▶ my $self = shift; # INDEX slot must be valid
Subroutine _update_INDEX: called CC=5 SCAR=16.1
90 75 my $base = shift; # row number where an insertion or removal was made
91 75 my $count = shift; # how many rows were inserted (+) or removed (-)
92  
93 75 my %done = ();
94  
95 75 ▶ for (my $i = ($base < 0) ? 0 : $base; $i < @{$$self->[NAMES]}; $i++) {
Branch: 2 branches
BranchTrueFalseTotal
$base < 0 ? : 2 73 75
if ($i < @{$$self->[0];}) 114 75 189
96 114 my $key = lc $$self->[NAMES][$i];
97 114 ▶ next if $done{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $done{$key} 42 72 114
98 72 @{$$self->[INDEX]{$key}} =
99 72 ▶ map { $_ + ($_ > $base ? $count : 0) } @{$$self->[INDEX]{$key}};
Branch: 1 branch
BranchTrueFalseTotal
$_ > $base ? : 87 31 118
100 72 $done{$key}++;
101 }
102 }
103  
104 sub _rebuild_MVOFF {
105 6 ▶ my $self = shift;
Subroutine _rebuild_MVOFF: called CC=4 SCAR=13.9
106  
107 6 my @mvoff = ();#(undef) x scalar @{$$self->[NAMES]};
108 6 my %cidx = (); # counted index; references to unique entries
109  
110 6 foreach my $name (@{$$self->[NAMES]}) {
111 10028 my $key = lc $name;
112 10028 ▶ if (not defined $cidx{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $cidx{$key}) { } 26 10002 10028
elsif (ref $cidx{$key} eq 'SCALAR') { } 4 9998 10002
113 # first time this key is seen
114 26 push @mvoff, undef;
115 26 $cidx{$key} = \$mvoff[$#mvoff];
116 } elsif (ref $cidx{$key} eq 'SCALAR') {
117 # second time this key is seen
118 4 push @mvoff, 1;
119 4 ${$cidx{$key}} = 0; # replace undefined value
120 4 $cidx{$key} = 2; # prepare counter
121 } else {
122 # third or later time this key is seen
123 9998 push @mvoff, $cidx{$key}++;
124 }
125 }
126  
127 6 $$self->[MVOFF] = \@mvoff;
128 }
129  
130 sub _dbg_dump {
131 51 ▶ my $self = shift;
Subroutine _dbg_dump: called CC=9 SCAR=22.0
132  
133 51 my @mvoff = (' ') x @{$$self->[NAMES]};
134 20016 ▶ @mvoff = map { defined $_ ? $_ : 'U' }
Branch: 1 branch
BranchTrueFalseTotal
defined $_ ? : 20002 14 20016
135 51 ▶ @{$$self->[MVOFF]}[0 .. $#{$$self->[NAMES]}]
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[2] 5 46 51
136 if defined $$self->[MVOFF];
137 51 my @widths = map {length} qw/ROW MVO NAME/;
138  
139 51 foreach my $row (0 .. $#{$$self->[NAMES]}) {
140 20233 ▶ $widths[0] = length $row if length $row > $widths[0];
Branch: 1 branch
BranchTrueFalseTotal
if length $row > $widths[0] 2 20231 20233
141 20233 ▶ $widths[1] = length $mvoff[$row] if length $mvoff[$row] > $widths[1];
Branch: 1 branch
BranchTrueFalseTotal
if length $mvoff[$row] > $widths[1] 2 20231 20233
142 20233 ▶ $widths[2] = length $$self->[NAMES][$row]
Branch: 1 branch
BranchTrueFalseTotal
if length $$self->[0][$row] > $widths[2] 73 20160 20233
143 if length $$self->[NAMES][$row] > $widths[2];
144 }
145  
146 51 my $out = sprintf ' %4$*1$s %5$*2$s %6$*3$s %7$s', @widths,
147 qw/ ROW MVO NAME VALUE /;
148 51 $out .= "\n".('=' x length $out)."\n";
149 $out .= join "\n", map
150 20233 ▶ { sprintf ' %4$*1$d %5$*2$s %6$*3$s%7$1s %8$s', @widths,
Branch: 2 branches
BranchTrueFalseTotal
defined $$self->[1][$_] ? : 20221 12 20233
defined $$self->[1][$_] ? : 20221 12 20233
151 ($_, $mvoff[$_], $$self->[NAMES][$_],
152 (defined $$self->[VALUES][$_] ? ':' : ' '),
153 (defined $$self->[VALUES][$_] ? $$self->[VALUES][$_] : '*deleted*')) }
154 51 0 .. $#{$$self->[NAMES]};
155  
156 51 return $out;
157 }
158  
159 # From RFC2616:
160 # CTL = <any US-ASCII control character
161 # (octets 0 - 31) and DEL (127)>
162 # LWS = [CRLF] 1*( SP | HT )
163 # separators = "(" | ")" | "<" | ">" | "@"
164 # | "," | ";" | ":" | "\" | <">
165 # | "/" | "[" | "]" | "?" | "="
166 # | "{" | "}" | SP | HT
167 28 my $PARSE_RE__LWS = qr/(?:\015\012)?[ \t]+/;
168 28 my $PARSE_RE__separator = qr[[][)(><}{@,;:/"\\?=[:space:]]];
169 28 my $PARSE_RE__not_separator = qr[[^][)(><}{@,;:/"\\?=[:space:]]];
170  
171 # From WARC specification:
172 # field-name = token
173 # token = 1*<any US-ASCII character
174 # except CTLs or separators>
175 28 my $PARSE_RE__token = qr/[!#$%'*+-.0-9A-Z^_`a-z|~]+/;
176  
177 =head1 DESCRIPTION
178  
179 The C<WARC::Fields> class encapsulates information in the
180 "application/warc-fields" format used for WARC record headers. This is a
181 simple key-value format closely analogous to HTTP headers, however
182 differences are significant enough that the C<HTTP::Headers> class cannot
183 be reliably reused for WARC fields.
184  
185 Instances of this class are usually created as member variables of the
186 C<WARC::Record> class, but can also be returned as the content of WARC
187 records with Content-Type "application/warc-fields".
188  
189 Instances of C<WARC::Fields> retrieved from WARC files are read-only and
190 will croak() if any attempt is made to change their contents.
191  
192 This class strives to faithfully represent the contents of a WARC file,
193 while providing a simple interface to answer simple questions.
194  
195 =head2 Multiple Values
196  
197 Most WARC headers may only appear once and with a single value in valid
198 WARC records, with the notable exception of the WARC-Concurrent-To header.
199 C<WARC::Fields> neither attempts to enforce nor relies upon this
200 constraint. Headers that appear multiple times are considered to have
201 multiple values. When iterating a tied hash, all values of a recurring
202 header are collected and returned with the B<first> occurrence of its key.
203  
204 Multiple values are returned from the C<field> method and tied hash
205 interface as array references, and are set by passing in an array
206 reference. Existing rows are reused where possible when updating a field
207 with multiple values. If the new array reference contains fewer items
208 (including the special case of replacing multiple values with a single
209 value) excess rows are deleted. If the new array reference requires
210 additional rows to be inserted, they are inserted immediately after the
211 last existing row for a field, with the same name case as that row.
212  
213 Precise control of the layout is available using the tied array interface,
214 but the ordering of the header rows is not constrained in the WARC
215 specification.
216  
217 =head2 Field Name Mangling
218  
219 As with C<HTTP::Headers>, the '_' character is converted to '-' in field
220 names unless the first character of the name is ':', which cannot itself
221 appear in a field name. Unlike C<HTTP::Headers>, the leading ':' is
222 stripped off immediately and the name stored otherwise exactly as given.
223 The C<field> method and tied hash interface allow this convenience feature.
224 The field names exposed via the tied array interface are reported
225 B<exactly> as they appear in the WARC file.
226  
227 Strictly, "X-Crazy-Header" and "X_Crazy_Header" are two B<different>
228 headers that the above convenience mechanism conflates. The solution is
229 simple: if (and only if) a header field B<already exists> with the B<exact>
230 name given, it is used, otherwise C<s/_/-/g> occurs and the name is
231 rechecked for another exact match. If no match is found, case is folded
232 and a third check performed. If a match is found, the existing header is
233 updated, otherwise a new header is created with character case as given.
234  
235 The WARC specification specifically states that field names are
236 case-insensitive, accordingly, "X-Crazy-Header" and "X-CRAZY-HeAdEr" are
237 considered the same header for the C<field> method and tied hash interface.
238 They will appear exactly as given in the tied array interface, however.
239  
240 =cut
241  
242 # This function handles two different canonicalizations:
243 # (1) case folding as required by the WARC specification
244 # (2) convenience translation s/_/-/g,
245 # (2a) suppressed if m/^:/, which is removed
246 # (2b) overridden by an exact match
247 # To make this work:
248 # --- all keys in INDEX are case-folded
249 # --- all keys in NAMES preserve case
250 # --- existing keys are case-folded by this function
251 # --- new keys translate s/_/-/g but preserve case
252 sub _find_key {
253 8608 ▶ my $self = shift; # INDEX slot must be valid
Subroutine _find_key: called CC=5 SCAR=16.1
254 8608 my $k = shift;
255 8608 my $key; ($key = $k) =~ s/^://;
256 8608 my $pad = $key;
257 8608 my $is_quoted = ($k =~ m/^:/);
258  
259 # exact case-folded match?
260 8608 ▶ return lc $key if defined $$self->[INDEX]{lc $key};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[3]{lc $key} 4783 3825 8608
261  
262 # case-folded match after s/_/-/g?
263 3825 $pad =~ s/_/-/g;
264 3825 ▶ return lc $pad if defined $$self->[INDEX]{lc $pad} && !$is_quoted;
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[3]{lc $pad} and not $is_quoted 989 2836 3825
Condition100%3/3
defined $$self->[3]{lc $pad} and not $is_quoted
!ll&&!rl&&r
28342989
MC/DC100%2/2
defined $$self->[3]{lc $pad} and not $is_quoted
defined $$self->[3]{lc $pad} not $is_quoted
Truth table100%3/3
A and not(B)
A= defined $$self->[3]{lc $pad}B= not $is_quoted
ABresult
0X0
100
111
265  
266 # not found ==> a new key will be made
267 2836 ▶ return $is_quoted ? $key : $pad;
Branch: 1 branch
BranchTrueFalseTotal
$is_quoted ? : 7 2829 2836
268 }
269  
270 # called only if there is no or one current value
271 sub _set_single_value {
272 22 ▶ my $self = shift; # INDEX slot must be valid
Subroutine _set_single_value: called CC=3 SCAR=11.0
273 22 my $key = shift; # as returned from _find_key
274 22 my $value = shift;
275  
276 22 ▶ croak "attempt to modify read-only object" if $$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$self->[4] 1 21 22
277  
278 21 ▶ unless (defined $$self->[INDEX]{lc $key}) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $$self->[3]{lc $key}) 12 9 21
279 # insert new key
280 12 push @{$$self->[NAMES]}, $key; # preserve original key
281 12 $key = lc $key; # fold key case
282 12 push @{$$self->[INDEX]{$key}}, $#{$$self->[NAMES]};
283 }
284  
285 21 $$self->[VALUES][$$self->[INDEX]{$key}[0]] = $value;
286 }
287  
288 sub _key_multiple_value_p {
289 4723 ▶ my $self = shift; # INDEX slot must be valid
Subroutine _key_multiple_value_p: called CC=2 SCAR=6.9
290 4723 my $key = shift; # as returned from _find_key
291  
292 # For this to be true, the key must already exist, which means that
293 # _find_key has case-folded it already.
294 return (defined $$self->[INDEX]{$key}
295 4723 ▶ && 1 < scalar @{$$self->[INDEX]{$key}})
Condition100%3/3
defined $$self->[3]{$key} && 1 < scalar @{$$self->[3]{$key};}
!ll&&!rl&&r
12468427
MC/DC100%2/2
defined $$self->[3]{$key} && 1 < scalar @{$$self->[3]{$key};}
defined $$self->[3]{$key} 1 < scalar @{$$self->[3]{$key};}
Truth table100%3/3
A && B
A= defined $$self->[3]{$key}B= 1 < scalar @{$$self->[3]{$key};}
ABresult
0X0
100
111
296 }
297  
298 # called in all cases where multiple values are involved
299 sub _set_multiple_value {
300 59 ▶ my $self = shift; # INDEX slot must be valid
Subroutine _set_multiple_value: called CC=11 SCAR=24.0
301 59 my $key = shift; # as returned from _find_key
302 59 my $value_aref = shift;
303  
304 59 ▶ croak "attempt to modify read-only object" if $$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$self->[4] 2 57 59
305  
306 my $cur_count = (defined $$self->[INDEX]{$key}
307 57 ▶ && scalar @{$$self->[INDEX]{$key}});
Condition100%2/2
defined $$self->[3]{$key} && scalar @{$$self->[3]{$key};}
!ll
1641
MC/DC100%1/1
defined $$self->[3]{$key} && scalar @{$$self->[3]{$key};}
defined $$self->[3]{$key}
Truth table100%2/2
A
A= defined $$self->[3]{$key}
Aresult
00
11
308 57 my $new_count = scalar @$value_aref;
309  
310 57 ▶ unless ($cur_count) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($cur_count) 16 41 57
311 # insert new key
312 16 push @{$$self->[NAMES]}, $key; # preserve original key
313 16 push @{$$self->[VALUES]}, undef; # prepare slot
314 16 $key = lc $key; # fold key case
315 16 push @{$$self->[INDEX]{$key}}, $#{$$self->[NAMES]};
316 16 $cur_count = 1; # account for the added slot
317 }
318 # $key is always case-folded at this point
319  
320 # adjust table to accommodate new number of values
321 57 ▶ if ($cur_count > $new_count) {
Branch: 2 branches
BranchTrueFalseTotal
if ($cur_count > $new_count) { } 30 27 57
elsif ($cur_count < $new_count) { } 15 12 27
322 # remove extra rows
323 30 foreach my $extra_row (reverse sort
324 30 splice @{$$self->[INDEX]{$key}}, $new_count) {
325 43 splice @{$$self->[NAMES]}, $extra_row, 1;
326 43 splice @{$$self->[VALUES]}, $extra_row, 1;
327 43 _update_INDEX($self, $extra_row, -1);
328 }
329 # special case: removing a field entirely
330 30 ▶ if ($new_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($new_count == 0) 22 8 30
331 # This is here to catch a hypothetical bug before data is corrupted.
332 die "stray INDEX entries left after removing field"
333 # uncoverable branch true
334 22 ▶ unless scalar @{$$self->[INDEX]{$key}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
unless scalar @{$$self->[3]{$key};} == 0 0 22 22
335 22 delete $$self->[INDEX]{$key};
336 }
337 } elsif ($cur_count < $new_count) {
338 # add more rows
339 15 my $last_row = $$self->[INDEX]{$key}[-1];
340 15 my $new_rows = $new_count - $cur_count;
341 15 _update_INDEX($self, $last_row, $new_rows);
342 15 splice @{$$self->[NAMES]}, 1+$last_row, 0,
343 (($$self->[NAMES][$last_row]) x $new_rows);
344 15 splice @{$$self->[VALUES]}, 1+$last_row, 0, ((undef) x $new_rows);
345 15 push @{$$self->[INDEX]{$key}}, 1+$last_row .. $last_row+$new_rows;
346 } # otherwise, $cur_count == $new_count
347 57 ▶ $$self->[MVOFF] = undef unless $cur_count == $new_count;
Branch: 1 branch
BranchTrueFalseTotal
unless $cur_count == $new_count 45 12 57
348 # there are always $new_count rows with $key at this point
349  
350 57 ▶ for (my $i = 0; $i < $new_count; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $new_count) 9968 57 10025
351 9968 { $$self->[VALUES][$$self->[INDEX]{$key}[$i]] = $value_aref->[$i] }
352 }
353  
354 =head2 Methods
355  
356 =over
357  
358 =item $f = WARC::Fields-E<gt>new
359  
360 Construct a new C<WARC::Fields> object. Initial contents can be passed as
361 key-value pairs to this constructor and will be added in the given order.
362  
363 Repeating a key or supplying an array reference as a value assigns multiple
364 values to a key. To reduce the risk of confusion, only quoting with a
365 leading ':' overrides the convenience feature of applying C<s/_/-/g> when
366 constructing a C<WARC::Fields> object. The exact match rules used when
367 setting values on an existing object do not apply here.
368  
369 Field names given when constructing a WARC::Fields object are otherwise
370 stored exactly as given, with case preserved, even when other names that
371 fold to the same string have been given earlier in the argument list.
372  
373 =cut
374  
375 sub new {
376 224 ▶ my $class = shift;
Subroutine new: called CC=8 SCAR=20.8
Pod documented
377 224 my $ob = [OBJECT_INIT];
378 224 my $k; my $v;
379  
380 # explicitly initialize NAMES and VALUES to allow as_string and as_block
381 # methods to be called on empty objects
382 224 $ob->[NAMES] = [];
383 224 $ob->[VALUES] = [];
384  
385 224 ▶ while (($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 797 219 1016
386 797 ▶ croak "key without value" unless defined $v;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $v 1 796 797
387  
388 796 ▶ if ($k =~ m/^:/) { $k =~ s/^:// } else { $k =~ s/_/-/g }
Branch: 1 branch
BranchTrueFalseTotal
if ($k =~ /^:/) { } 7 789 796
389  
390 796 ▶ croak "reference to field with no name" unless $k =~ m/./;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /./ 2 794 796
391 794 ▶ croak "reference to invalid field name" if $k !~ m/^$PARSE_RE__token$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^$PARSE_RE__token$/o 2 792 794
392  
393 792 ▶ if (ref $v eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v eq 'ARRAY') { } 3 789 792
394 3 foreach my $value (@$v) {
395 8 push @{$ob->[NAMES]}, $k;
396 8 push @{$ob->[VALUES]}, $value;
397 8 push @{$ob->[INDEX]{lc $k}}, $#{$ob->[NAMES]};
398 }
399 } else {
400 789 push @{$ob->[NAMES]}, $k;
401 789 push @{$ob->[VALUES]}, $v;
402 789 push @{$ob->[INDEX]{lc $k}}, $#{$ob->[NAMES]};
403 }
404 }
405  
406 219 {our $_total_newly_constructed; $_total_newly_constructed++}
407 219 bless \ $ob, $class;
408 }
409  
410 =item $f-E<gt>clone
411  
412 Copy a C<WARC::Fields> object. A copy of a read-only object is writable.
413  
414 =cut
415  
416 sub clone {
417 8 ▶ my $self = shift;
Subroutine clone: called CC=3 SCAR=11.0
Pod documented
418 8 my $new = [OBJECT_INIT];
419  
420 8 $new->[NAMES] = [@{$$self->[NAMES]}];
421 8 $new->[VALUES] = [@{$$self->[VALUES]}];
422 8 ▶ $new->[MVOFF] = [@{$$self->[MVOFF]}] if defined $$self->[MVOFF];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[2] 1 7 8
423 30 $new->[INDEX] = {map {$_ => [@{$$self->[INDEX]{$_}}]}
424 8 ▶ keys %{$$self->[INDEX]}} if defined $$self->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[3] 7 1 8
425  
426 8 {our $_total_newly_cloned; $_total_newly_cloned++}
427 8 bless \ $new, ref $self;
428 }
429  
430 =item $f-E<gt>field( $name )
431  
432 =item $f-E<gt>field( $name =E<gt> $value )
433  
434 =item $f-E<gt>field( $n1 =E<gt> $v1, $n2 =E<gt> $v2, ... )
435  
436 Get or set the value of one or more fields. The field name is not case
437 sensitive, but C<WARC::Fields> will preserve its case if a new entry is
438 created.
439  
440 Setting a field to C<undef> effectively deletes that field, although it
441 remains visible in the tied array interface and will retain its position if
442 a new value is assigned. Setting a field to an empty array reference
443 removes that field entirely.
444  
445 =cut
446  
447 sub field {
448 7565 ▶ my $self = shift;
Subroutine field: called CC=10 SCAR=23.0
Pod documented
449  
450 7565 ▶ _rebuild_INDEX($self) unless defined $$self->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self->[3] 7 7558 7565
451  
452 7565 my $k; my $v; my $have_value_arg = scalar @_ > 1;
453 7565 ▶ while (($k, $v) = splice @_, 0, 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = splice(@_, 0, 2)) 7565 72 7637
454 7565 my $key = $self->_find_key($k);
455  
456 7565 ▶ croak "reference to field with no name" unless $key =~ m/./;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /./ 2 7563 7565
457 7563 ▶ croak "reference to invalid field name" if $key !~ m/^$PARSE_RE__token$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^$PARSE_RE__token$/o 3 7560 7563
458  
459 7560 ▶ if (not $have_value_arg) {
Branch: 3 branches
BranchTrueFalseTotal
if (not $have_value_arg) { } 7485 75 7560
elsif (UNIVERSAL::isa($v, 'ARRAY')) { } 51 24 75
elsif ($self->_key_multiple_value_p($key)) { } 2 22 24
460 # get a value
461 7485 ▶ return undef unless defined $$self->[INDEX]{$key};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self->[3]{$key} 2786 4699 7485
462 4699 ▶ return $$self->[VALUES][$$self->[INDEX]{$key}[0]]
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_key_multiple_value_p($key) 4674 25 4699
463 unless $self->_key_multiple_value_p($key);
464 78 return [grep {defined $_}
465 25 map {$$self->[VALUES][$_]} @{$$self->[INDEX]{$key}}];
466 } # otherwise set a value
467 elsif (UNIVERSAL::isa($v, 'ARRAY'))
468 51 { $self->_set_multiple_value($key, $v) }
469 elsif ($self->_key_multiple_value_p($key))
470 # has multiple values, but now only setting a single value
471 2 { $self->_set_multiple_value($key, [$v]) }
472 else
473 22 { $self->_set_single_value($key, $v) }
474 72 $have_value_arg = scalar @_ > 1;
475 }
476 72 return (); # return nothing
477 # Note that setting one or more fields and then getting a field is
478 # possible as a side-effect of this organization, but is explicitly NOT
479 # supported. That trick is NOT part of the stable API.
480 }
481  
482 =item $f = WARC::Fields-E<gt>parse( $text )
483  
484 =item $f = WARC::Fields-E<gt>parse( from =E<gt> $fh )
485  
486 =item $f = parse WARC::Fields from =E<gt> $fh
487  
488 Construct a new C<WARC::Fields> object, reading initial contents from the
489 provided text string or filehandle.
490  
491 The C<parse> method throws an exception if it encounters input that it does
492 not understand.
493  
494 If the C<parse> method encounters a field name with a leading ':', which
495 implies an empty name and is not allowed, the leading ':' is silently
496 dropped from the line and parsing retried. If the line is not valid after
497 this change, the C<parse> method throws an exception. This feature is in
498 keeping with the general principle of "be liberal in what you accept" and
499 is a preemptive workaround for a predicted bug in other implementations.
500  
501 =cut
502  
503 sub parse {
504 642 ▶ my $class = shift;
Subroutine parse: called CC=8 SCAR=20.8
Pod documented
505 642 my $text = shift;
506 642 my $rd;
507  
508 642 ▶ if ($text eq 'from') {
Branch: 1 branch
BranchTrueFalseTotal
if ($text eq 'from') { } 637 5 642
509 637 $rd = shift;
510 } else {
511 # This fails iff perl was built without PerlIO, which is non-default.
512 # uncoverable branch true
513 5 ▶ open $rd, '<', \$text or die "failure opening stream on variable: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $rd, "<", \$text or die "failure opening stream on variable: $!" 0 5 5
514 }
515  
516 642 my @names = ();
517 642 my @values = ();
518 642 my %idx = ();
519 642 my $at_end = 0;
520  
521 642 local *_;
522 642 ▶ while (<$rd>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $rd)) 4848 1 4849
523 4848 s/[\015\012]+$//;
524 4848 ▶ if (m/^:?($PARSE_RE__token):\s+(.*)$/o) # $1 -- name $2 -- value
Branch: 3 branches
BranchTrueFalseTotal
if (/^:?($PARSE_RE__token):\s+(.*)$/o) { } 4206 642 4848
elsif (/^\s+(\S.*)$/) { } 1 641 642
elsif (/^$/) { } 639 2 641
525 4206 { push @names, $1; push @values, $2; push @{$idx{lc $1}}, $#names }
526 elsif (m/^\s+(\S.*)$/) # $1 -- continued value
527 1 { $values[$#values] .= ' '.$1 }
528 639 elsif (m/^$/) { $at_end = 1; last }
529 2 else { croak "unrecognized input: $_" }
530 }
531  
532 640 ▶ carp "end-of-input before end marker" unless $at_end;
Branch: 1 branch
BranchTrueFalseTotal
unless $at_end 1 639 640
533  
534 640 @values = map {Encode::decode_utf8($_)} @values;
535  
536 640 my $ob = [OBJECT_INIT];
537 640 $ob->[NAMES] = \@names;
538 640 $ob->[VALUES] = \@values;
539 640 $ob->[INDEX] = \%idx;
540  
541 640 {our $_total_newly_parsed; $_total_newly_parsed++}
542 640 bless \ $ob, $class;
543 }
544  
545 =item $f-E<gt>as_block
546  
547 =item $f-E<gt>as_string
548  
549 Return the contents as a formatted WARC header or application/warc-fields
550 block. The C<as_block> method uses network line endings and UTF-8 as
551 specified for the WARC format, while the C<as_string> method uses the local
552 line endings and does not perform encoding.
553  
554 =cut
555  
556 sub _as_text {
557 51 ▶ my $self = shift;
Subroutine _as_text: called CC=3 SCAR=11.0
558 51 my $newline = shift;
559 51 my $out = '';
560  
561 51 ▶ for (my $i = 0; $i < @{$$self->[NAMES]}; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$$self->[0];}) 216 51 267
562 216 ▶ next unless defined $$self->[VALUES][$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$self->[1][$i] 7 209 216
563 209 $out .= $$self->[NAMES][$i] . ': ' . $$self->[VALUES][$i] . $newline;
564 }
565  
566 51 return $out;
567 }
568  
569 4 ▶sub as_block { Encode::encode('UTF-8', _as_text(shift, WARC::CRLF)) }
Subroutine as_block: called CC=1 SCAR=0.0
Pod documented
570 47 ▶sub as_string { _as_text(shift, "\n") }
Subroutine as_string: called CC=1 SCAR=0.0
Pod documented
571  
572 =item $f-E<gt>set_readonly
573  
574 Mark a C<WARC::Fields> object read-only. All methods that modify the
575 object will croak() if called on a read-only object.
576  
577 =cut
578  
579 sub set_readonly {
580 766 ▶ my $self = shift;
Subroutine set_readonly: called CC=1 SCAR=0.0
Pod documented
581  
582 766 $$self->[IS_RO] = 1;
583 }
584  
585 =back
586  
587 =head2 Tied Array Access
588  
589 The order of fields can be fully controlled by tying an array to a
590 C<WARC::Fields> object and manipulating the array using ordinary Perl
591 operations. The C<splice> and C<sort> functions are likely to be useful
592 for reordering array elements if desired.
593  
594 C<WARC::Fields> will croak() if an attempt is made to set a field name with
595 a leading ':' using the tied array interface.
596  
597 =cut
598  
599 sub TIEARRAY {
600 4 ▶ my $class = shift;
Subroutine TIEARRAY: called CC=1 SCAR=0.0
601 4 my $ob = shift;
602  
603 # This method must ignore the given class to allow the "empty subclass"
604 # test to pass. If a subclass really wants, an override for TIEARRAY
605 # itself can call SUPER::TIEARRAY and re-bless the returned reference
606 # into the desired class.
607 4 $WARC::Fields::TiedArray::_total_tied++;
608 4 bless \ $ob, 'WARC::Fields::TiedArray';
609 }
610  
611 {
612 package WARC::Fields::TiedArray::Entry;
613  
614 28 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
615  
616 BEGIN { $WARC::Fields::TiedArray::Entry::{$_} = $WARC::Fields::{$_}
617 28 ▶ for WARC::Fields::OBJECT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
618  
619 28 ▶ use constant { NAME => 0, VALUE => 1, TABLE => 2, ROW => 3 };
Subroutine BEGIN: called CC=1 SCAR=0.0
620  
621 28 ▶ use overload '""' => 'name', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
622  
623 =pod
624  
625 The tied array interface accepts simple string values but returns objects
626 with additional information. The returned object has an overloaded string
627 conversion that yields the name for that entry but additionally has
628 C<value> and C<offset> methods.
629  
630 An entry object is bound to a slot in its parent C<WARC::Fields> object,
631 but will be copied if it is assigned to another slot in the same or another
632 C<WARC::Fields> object.
633  
634 Due to complex aliasing rules necessary for array slice assignment to work
635 for permuting rows in the table, entry objects must be short-lived.
636 Storing the object read from a tied array and attempting to use it after
637 modifying its parent C<WARC::Fields> object produces unspecified results.
638  
639 =over
640  
641 =item $entry = $array[$n]
642  
643 =item $entry = $f-E<gt>[$n]
644  
645 The tied array C<FETCH> method returns a "entry object" instead of the name
646 itself.
647  
648 =cut
649  
650 sub _new {
651 223 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
652 223 my $table = shift;
653 223 my $row = shift;
654  
655 223 bless [$$table->[NAMES][$row], $$table->[VALUES][$row],
656 $table, $row], $class;
657 }
658  
659 =item $name = "$entry"
660  
661 =item $name = $entry-E<gt>name
662  
663 =item $name = "$f-E<gt>[$n]"
664  
665 =item $name = $f-E<gt>[$n]-E<gt>name
666  
667 The C<name> method on a entry object returns the field name.
668 String conversion is overloaded to call this method.
669  
670 =cut
671  
672 62 ▶sub name { (shift)->[NAME] }
Subroutine name: called CC=1 SCAR=0.0
673  
674 =item $value = $entry-E<gt>value
675  
676 =item $value = $array[$n]-E<gt>value
677  
678 =item $value = $f-E<gt>[$n]-E<gt>value
679  
680 =item $entry-E<gt>value( $new_value )
681  
682 =item $array[$n]-E<gt>value( $new_value )
683  
684 =item $f-E<gt>[$n]-E<gt>value( $new_value )
685  
686 The C<value> method on a entry object returns the field value for this
687 particular entry. Only a single scalar is returned, even if multiple
688 entries share the same name.
689  
690 If given an argument, the C<value> method replaces the value for this
691 particular entry. The argument will be coerced to a string.
692  
693 =cut
694  
695 sub value {
696 154 ▶ my $self = shift;
Subroutine value: called CC=3 SCAR=11.0
697  
698 154 ▶ if (scalar @_ == 0) { # get this value
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0) { } 34 120 154
699 34 return $self->[VALUE];
700 } else { # update this value
701 120 ▶ croak "attempt to modify read-only object" if ${$self->[TABLE]}->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if ${$$self[2];}->[4] 1 119 120
702 119 my $newval = shift;
703 119 ${$self->[TABLE]}->[VALUES]->[$self->[ROW]] = $self->[VALUE] = "$newval";
704 119 return (); # and return nothing
705 }
706 }
707  
708 =item $offset = $entry-E<gt>offset
709  
710 =item $offset = $array[$n]-E<gt>offset
711  
712 =item $offset = $f-E<gt>[$n]-E<gt>offset
713  
714 The C<offset> method on a entry object returns the position of this entry
715 amongst multiple entries with the same field name. These positions are
716 numbered from zero and are identical to the positions in the array
717 reference returned for this entry's field name from the C<field> method or
718 the tied hash interface.
719  
720 =cut
721  
722 sub offset {
723 21 ▶ my $self = shift;
Subroutine offset: called CC=2 SCAR=6.9
724 21 ▶ $self->[TABLE]->_rebuild_MVOFF unless defined ${$self->[TABLE]}->[MVOFF];
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$self[2];}->[2] 6 15 21
725 21 return ${$self->[TABLE]}->[MVOFF]->[$self->[ROW]];
726 }
727  
728 =back
729  
730 =cut
731  
732 }
733  
734 {
735 28 package WARC::Fields::TiedArray::LooseEntry;
736  
737 28 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
738  
739 BEGIN { $WARC::Fields::TiedArray::LooseEntry::{$_} = $WARC::Fields::{$_}
740 28 ▶ for WARC::Fields::OBJECT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
741  
742 28 ▶ use constant { NAME => 0, VALUE => 1 };
Subroutine BEGIN: called CC=1 SCAR=0.0
743  
744 28 ▶ BEGIN { our @ISA = qw(WARC::Fields::TiedArray::Entry) }
Subroutine BEGIN: called CC=1 SCAR=0.0
745  
746 # This is a special type of "entry object" that is not associated with a
747 # table row, returned from POP, SHIFT, and SPLICE when needed.
748  
749 sub _new {
750 24 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
751 24 my $name = shift;
752 24 my $value = shift;
753  
754 24 bless [$name, $value], $class;
755 }
756  
757 18 ▶ sub name { return (shift)->[NAME] }
Subroutine name: called CC=1 SCAR=0.0
758  
759 sub value {
760 17 ▶ my $self = shift;
Subroutine value: called CC=2 SCAR=6.9
761  
762 17 ▶ if (scalar @_ == 0) # get
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @_ == 0) { } 16 1 17
763 16 { return $self->[VALUE] }
764 else # set
765 1 { croak "Loose array entries are read-only." }
766 }
767  
768 1 ▶ sub offset { return undef }
Subroutine offset: called CC=1 SCAR=0.0
769 }
770  
771 {
772 28 package WARC::Fields::TiedArray;
773  
774 28 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
775  
776 BEGIN { $WARC::Fields::TiedArray::{$_} = $WARC::Fields::{$_}
777 28 ▶ for WARC::Fields::OBJECT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
778  
779 # The underlying object is a reference to a WARC::Fields object.
780  
781 sub FETCH {
782 223 ▶ my $self = shift;
Subroutine FETCH: called CC=1 SCAR=0.0
783 223 my $row = shift;
784 223 return (ref($self).'::Entry')->_new($$self, $row);
785 }
786  
787 sub STORE {
788 13 ▶ my $self = shift;
Subroutine STORE: called CC=6 SCAR=17.9
789 13 my $row = shift;
790 13 my $name = shift;
791  
792 13 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 12 13
793  
794 12 ▶ $self->STORESIZE($row + 1) if $#{$$$self->[NAMES]} < $row;
Branch: 1 branch
BranchTrueFalseTotal
if $#{$$$$self[0];} < $row 1 11 12
795  
796 12 ▶ if (UNIVERSAL::isa($name, ref($self).'::Entry')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($name, ref($self) . '::Entry')) { } 8 4 12
797 # copy entry
798 8 ▶ croak "attempt to set invalid name"
Branch: 1 branch
BranchTrueFalseTotal
unless $name->name =~ /^$PARSE_RE__token$/o 1 7 8
799 if $name->name !~ m/^$PARSE_RE__token$/o;
800 7 $$$self->[NAMES]->[$row] = $name->name;
801 7 $$$self->[VALUES]->[$row] = $name->value;
802 } else {
803 # set name
804 4 ▶ croak "attempt to set invalid name"
Branch: 1 branch
BranchTrueFalseTotal
unless "$name" =~ /^$PARSE_RE__token$/o 2 2 4
805 if "$name" !~ m/^$PARSE_RE__token$/o;
806 2 $$$self->[NAMES]->[$row] = "$name";
807 }
808 9 $$$self->[MVOFF] = undef;
809 9 $$$self->[INDEX] = undef;
810 }
811  
812 sub FETCHSIZE {
813 38 ▶ my $self = shift;
Subroutine FETCHSIZE: called CC=1 SCAR=0.0
814 38 return scalar @{$$$self->[NAMES]};
815 }
816  
817 sub STORESIZE {
818 5 ▶ my $self = shift;
Subroutine STORESIZE: called CC=5 SCAR=16.1
819 5 my $count = shift;
820  
821 5 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] and $count != $self->FETCHSIZE 1 4 5
Condition100%3/3
$$$self->[4] and $count != $self->FETCHSIZE
!ll&&!rl&&r
311
MC/DC100%2/2
$$$self->[4] and $count != $self->FETCHSIZE
$$$self->[4] $count != $self->FETCHSIZE
Truth table100%3/3
A and B
A= $$$self->[4]B= $count != $self->FETCHSIZE
ABresult
0X0
100
111
822 if $$$self->[IS_RO] && $count != $self->FETCHSIZE();
823  
824 4 ▶ if ($count > $self->FETCHSIZE()) {
Branch: 2 branches
BranchTrueFalseTotal
if ($count > $self->FETCHSIZE) { } 2 2 4
elsif ($count < $self->FETCHSIZE) { } 1 1 2
825 2 my $needed = $count - $self->FETCHSIZE();
826 2 push @{$$$self->[NAMES]}, ('X-Undefined-Field-Name') x $needed;
827 2 push @{$$$self->[VALUES]}, (undef) x $needed;
828 } elsif ($count < $self->FETCHSIZE()) {
829 1 splice @{$$$self->[NAMES]}, $count;
830 1 splice @{$$$self->[VALUES]}, $count;
831 1 $$$self->[INDEX] = undef;
832 1 } else { return } # no actual change
833 }
834  
835 ▶ sub EXTEND {
Subroutine EXTEND: not called CC=1 SCAR=0.0
836 # do nothing
837 }
838  
839 sub EXISTS {
840 14 ▶ my $self = shift;
Subroutine EXISTS: called CC=1 SCAR=0.0
841 14 my $row = shift;
842 14 return defined $$$self->[VALUES]->[$row];
843 }
844  
845 sub DELETE {
846 2 ▶ my $self = shift;
Subroutine DELETE: called CC=2 SCAR=6.9
847 2 my $row = shift;
848  
849 2 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 1 2
850  
851 1 my $old_value = $$$self->[VALUES]->[$row];
852 1 $$$self->[VALUES]->[$row] = undef;
853 1 $$$self->[MVOFF] = undef;
854 1 $$$self->[INDEX] = undef;
855 1 return $old_value;
856 }
857  
858 sub CLEAR {
859 3 ▶ my $self = shift;
Subroutine CLEAR: called CC=2 SCAR=6.9
860  
861 3 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 2 3
862  
863 2 $$$self->[NAMES] = [];
864 2 $$$self->[VALUES] = [];
865 2 $$$self->[MVOFF] = undef;
866 2 $$$self->[INDEX] = undef;
867 2 return undef;
868 }
869  
870 sub PUSH {
871 7 ▶ my $self = shift;
Subroutine PUSH: called CC=6 SCAR=17.9
872  
873 7 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] and scalar @_ 1 6 7
Condition100%2/2
$$$self->[4] and scalar @_
!ll
52
MC/DC100%1/1
$$$self->[4] and scalar @_
$$$self->[4]
Truth table100%2/2
A
A= $$$self->[4]
Aresult
00
11
874 if $$$self->[IS_RO] && scalar @_;
875  
876 6 foreach my $item (@_) {
877 103 my $name; my $value;
878 103 ▶ if (UNIVERSAL::isa($item, ref($self).'::Entry'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($item, ref($self) . '::Entry')) { } 2 101 103
879 2 { $name = $item->name; $value = $item->value }
880 else
881 101 { $name = "$item"; $value = undef }
882 103 ▶ croak "attempt to set invalid name" if $name !~ m/^$PARSE_RE__token$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$PARSE_RE__token$/o 2 101 103
883 101 push @{$$$self->[NAMES]}, $name;
884 101 push @{$$$self->[VALUES]}, $value;
885 }
886 4 $$$self->[MVOFF] = undef;
887 4 $$$self->[INDEX] = undef;
888 4 return scalar @{$$$self->[NAMES]};
889 }
890  
891 sub POP {
892 5 ▶ my $self = shift;
Subroutine POP: called CC=3 SCAR=11.0
893  
894 5 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 4 5
895  
896 my $ret = WARC::Fields::TiedArray::LooseEntry->_new
897 4 (pop @{$$$self->[NAMES]}, pop @{$$$self->[VALUES]});
898 4 ▶ pop @{$$$self->[MVOFF]} if defined $$$self->[MVOFF];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$$self->[2] 2 2 4
899 4 $$$self->[INDEX] = undef;
900  
901 4 return $ret;
902 }
903  
904 sub SHIFT {
905 2 ▶ my $self = shift;
Subroutine SHIFT: called CC=2 SCAR=6.9
906  
907 2 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 1 2
908  
909 my $ret = WARC::Fields::TiedArray::LooseEntry->_new
910 1 (shift @{$$$self->[NAMES]}, shift @{$$$self->[VALUES]});
911 1 $$$self->[MVOFF] = undef;
912 1 $$$self->[INDEX] = undef;
913  
914 1 return $ret;
915 }
916  
917 sub UNSHIFT {
918 6 ▶ my $self = shift;
Subroutine UNSHIFT: called CC=6 SCAR=17.9
919  
920 6 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] and scalar @_ 1 5 6
Condition100%2/2
$$$self->[4] and scalar @_
!ll
42
MC/DC100%1/1
$$$self->[4] and scalar @_
$$$self->[4]
Truth table100%2/2
A
A= $$$self->[4]
Aresult
00
11
921 if $$$self->[IS_RO] && scalar @_;
922  
923 5 foreach my $item (@_) {
924 4 my $name; my $value;
925 4 ▶ if (UNIVERSAL::isa($item, ref($self).'::Entry'))
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($item, ref($self) . '::Entry')) { } 2 2 4
926 2 { $name = $item->name; $value = $item->value }
927 else
928 2 { $name = "$item"; $value = undef }
929 4 ▶ croak "attempt to set invalid name" if $name !~ m/^$PARSE_RE__token$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $name =~ /^$PARSE_RE__token$/o 2 2 4
930 2 unshift @{$$$self->[NAMES]}, $name;
931 2 unshift @{$$$self->[VALUES]}, $value;
932 }
933 3 $$$self->[MVOFF] = undef;
934 3 $$$self->[INDEX] = undef;
935 3 return scalar @{$$$self->[NAMES]};
936 }
937  
938 sub SPLICE {
939 10 ▶ my $self = shift;
Subroutine SPLICE: called CC=10 SCAR=23.0
940 10 my $offset = shift;
941 10 my $length = shift;
942  
943 10 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 9 10
944 10 ▶ $length = $self->FETCHSIZE() - $offset unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 3 7 10
945  
946 10 ▶ return () unless ($length != 0 || scalar @_);
Branch: 1 branch
BranchTrueFalseTotal
unless $length != 0 or scalar @_ 1 9 10
Condition100%2/2
$length != 0 or scalar @_
l!l
64
MC/DC100%1/1
$length != 0 or scalar @_
$length != 0
Truth table100%2/2
A
A= $length != 0
Aresult
11
00
947  
948 9 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 2 7 9
949  
950 7 my @new_names = (); my @new_values = (); my @old_names; my @old_values;
951  
952 7 foreach my $item (@_) {
953 17 ▶ if (UNIVERSAL::isa($item, ref($self).'::Entry')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($item, ref($self) . '::Entry')) { } 11 6 17
954 11 push @new_names, $item->name;
955 11 push @new_values, $item->value;
956 } else {
957 6 push @new_names, "$item";
958 6 push @new_values, undef;
959 }
960 }
961  
962 croak "attempt to set invalid name"
963 7 ▶ if grep { $_ !~ m/^$PARSE_RE__token$/o } @new_names;
Branch: 1 branch
BranchTrueFalseTotal
if grep {not $_ =~ /^$PARSE_RE__token$/o;} @new_names 2 5 7
964  
965 5 @old_names = splice @{$$$self->[NAMES]}, $offset, $length, @new_names;
966 5 @old_values = splice @{$$$self->[VALUES]}, $offset, $length, @new_values;
967  
968 5 my @ret = ();
969  
970 5 ▶ for (my $i = 0; $i < scalar @old_names; $i++)
Branch: 1 branch
BranchTrueFalseTotal
if ($i < scalar @old_names) 18 5 23
971 18 { push @ret, WARC::Fields::TiedArray::LooseEntry->_new
972 ($old_names[$i], $old_values[$i]) }
973  
974 5 $$$self->[MVOFF] = undef;
975 5 $$$self->[INDEX] = undef;
976 5 return @ret;
977 }
978  
979 4 ▶ sub UNTIE { our $_total_untied; $_total_untied++ }
Subroutine UNTIE: called CC=1 SCAR=0.0
980  
981 4 ▶ sub DESTROY { our $_total_destroyed; $_total_destroyed++ }
Subroutine DESTROY: called CC=1 SCAR=0.0
982 }
983  
984 =head2 Tied Hash Access
985  
986 The contents of a C<WARC::Fields> object can be easily examined by tying a
987 hash to the object. Reading or setting a hash key is equivalent to the
988 C<field> method, but the tied hash will iterate keys and values in the
989 order in which each key B<first> appears in the internal table.
990  
991 Like the tied array interface, the tied hash interface returns magical
992 objects that internally refer back to the parent C<WARC::Fields> object.
993 These objects remain valid if the underlying C<WARC::Fields> object is
994 changed, but further use may produce surprising and unspecified results.
995  
996 The use of magical objects enables the values in a tied hash to B<always>
997 be arrays, even for keys that do not exist (the array will have zero
998 elements) or that have only one value (the array will have a string
999 conversion that produces that one value). This allows a tied hash to
1000 support autovivification of an array value just as Perl's own hashes do.
1001  
1002 =cut
1003  
1004 28 sub TIEHASH {
1005 70 ▶ my $class = shift;
Subroutine TIEHASH: called CC=1 SCAR=0.0
1006 70 my $ob = shift;
1007  
1008 # This method must ignore the given class to allow the "empty subclass"
1009 # test to pass. If a subclass really wants, an override for TIEHASH
1010 # itself can call SUPER::TIEHASH and re-bless the returned reference
1011 # into the desired class.
1012 70 $WARC::Fields::TiedHash::_total_tied++;
1013 70 bless \ $ob, 'WARC::Fields::TiedHash';
1014 }
1015  
1016 {
1017 package WARC::Fields::TiedHash::ValueArray;
1018  
1019 28 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1020  
1021 BEGIN { $WARC::Fields::TiedHash::ValueArray::{$_} = $WARC::Fields::{$_}
1022 28 ▶ for WARC::Fields::OBJECT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
1023  
1024 28 ▶ use constant { TABLE => 0, KEY => 1, KEYc => 2 }; # KEYc -- canonical KEY
Subroutine BEGIN: called CC=1 SCAR=0.0
1025  
1026 sub TIEARRAY {
1027 458 ▶ my $class = shift;
Subroutine TIEARRAY: called CC=2 SCAR=6.9
1028 458 my $table = shift;
1029 458 ▶ $table->_rebuild_INDEX unless defined $$table->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$table->[3] 5 453 458
1030 458 my $key = $table->_find_key(shift); # needs INDEX
1031  
1032 458 { our $_total_tied; $_total_tied++ }
1033 458 bless [$table, $key, lc $key], $class;
1034 }
1035  
1036 sub FETCH {
1037 768 ▶ my $self = shift;
Subroutine FETCH: called CC=3 SCAR=11.0
1038 768 my $offset = shift;
1039  
1040 768 ▶ $self->[TABLE]->_rebuild_INDEX unless defined ${$self->[TABLE]}->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$self[0];}->[3] 2 766 768
1041  
1042 768 my $row = ${$self->[TABLE]}->[INDEX]{$self->[KEYc]}[$offset];
1043 768 ▶ return defined $row ? ${$self->[TABLE]}->[VALUES][$row] : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $row ? : 752 16 768
1044 }
1045  
1046 sub STORE {
1047 10 ▶ my $self = shift;
Subroutine STORE: called CC=5 SCAR=16.1
1048 10 my $offset = shift;
1049 10 my $value = shift;
1050  
1051 10 my $T = $self->[TABLE];
1052  
1053 10 ▶ croak "attempt to modify read-only object" if $$T->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] 1 9 10
1054  
1055 9 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 8 9
1056  
1057 $self->STORESIZE($offset + 1)
1058 if not defined $$T->[INDEX]{$self->[KEYc]}
1059 9 ▶ or $#{$$T->[INDEX]{$self->[KEYc]}} < $offset;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $$T->[3]{$self->[2]} or $#{$$T->[3]{$self->[2]};} < $offset 2 7 9
Condition100%3/3
not defined $$T->[3]{$self->[2]} or $#{$$T->[3]{$self->[2]};} < $offset
l!l&&r!l&&!r
117
MC/DC100%2/2
not defined $$T->[3]{$self->[2]} or $#{$$T->[3]{$self->[2]};} < $offset
not defined $$T->[3]{$self->[2]} $#{$$T->[3]{$self->[2]};} < $offset
Truth table100%3/3
not(A) or B
A= not defined $$T->[3]{$self->[2]}B= $#{$$T->[3]{$self->[2]};} < $offset
ABresult
1X1
011
000
1060  
1061 9 $$T->[VALUES][$$T->[INDEX]{$self->[KEYc]}[$offset]] = "$value";
1062 }
1063  
1064 sub FETCHSIZE {
1065 792 ▶ my $self = shift;
Subroutine FETCHSIZE: called CC=3 SCAR=11.0
1066  
1067 792 ▶ $self->[TABLE]->_rebuild_INDEX unless defined ${$self->[TABLE]}->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$self[0];}->[3] 3 789 792
1068  
1069 782 return scalar @{${$self->[TABLE]}->[INDEX]{$self->[KEYc]}}
1070 792 ▶ if defined ${$self->[TABLE]}->[INDEX]{$self->[KEYc]};
Branch: 1 branch
BranchTrueFalseTotal
if defined ${$$self[0];}->[3]{$self->[2]} 782 10 792
1071 10 return 0; # otherwise: key does not exist
1072 }
1073  
1074 sub STORESIZE {
1075 8 ▶ my $self = shift;
Subroutine STORESIZE: called CC=7 SCAR=19.5
1076 8 my $count = shift;
1077  
1078 8 my $T = $self->[TABLE];
1079  
1080 8 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] and $count != $self->FETCHSIZE 1 7 8
Condition100%3/3
$$T->[4] and $count != $self->FETCHSIZE
!ll&&!rl&&r
611
MC/DC100%2/2
$$T->[4] and $count != $self->FETCHSIZE
$$T->[4] $count != $self->FETCHSIZE
Truth table100%3/3
A and B
A= $$T->[4]B= $count != $self->FETCHSIZE
ABresult
0X0
100
111
1081 if $$T->[IS_RO] && $count != $self->FETCHSIZE();
1082  
1083 7 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 2 5 7
1084  
1085 7 my @new = ();
1086 6 @new = @{$$T->[VALUES]}[@{$$T->[INDEX]{$self->[KEYc]}}]
1087 7 ▶ if defined $$T->[INDEX]{$self->[KEYc]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$T->[3]{$self->[2]} 6 1 7
1088 7 ▶ if ($count > $self->FETCHSIZE())
Branch: 2 branches
BranchTrueFalseTotal
if ($count > $self->FETCHSIZE) { } 4 3 7
elsif ($count < $self->FETCHSIZE) { } 2 1 3
1089 4 { push @new, (undef) x ($count - $self->FETCHSIZE()) }
1090 elsif ($count < $self->FETCHSIZE())
1091 2 { @new = @new[0..($count-1)] }
1092 1 else { return } # no actual change
1093 6 $T->field($self->[KEY] => \@new);
1094 }
1095  
1096 ▶ sub EXTEND {
Subroutine EXTEND: not called CC=1 SCAR=0.0
1097 # do nothing
1098 }
1099  
1100 sub EXISTS {
1101 5 ▶ my $self = shift;
Subroutine EXISTS: called CC=2 SCAR=6.9
1102 5 my $offset = shift;
1103  
1104 5 ▶ $self->[TABLE]->_rebuild_INDEX unless defined ${$self->[TABLE]}->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$self[0];}->[3] 1 4 5
1105  
1106 5 return exists ${$self->[TABLE]}->[INDEX]{$self->[KEYc]}[$offset];
1107 }
1108  
1109 sub DELETE {
1110 4 ▶ my $self = shift;
Subroutine DELETE: called CC=3 SCAR=11.0
1111 4 my $offset = shift;
1112  
1113 4 ▶ croak "attempt to modify read-only object" if ${$self->[TABLE]}->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if ${$$self[0];}->[4] 1 3 4
1114  
1115 3 ▶ $self->[TABLE]->_rebuild_INDEX unless defined ${$self->[TABLE]}->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined ${$$self[0];}->[3] 1 2 3
1116  
1117 3 my $row = ${$self->[TABLE]}->[INDEX]{$self->[KEYc]}[$offset];
1118 3 my $old_value = ${$self->[TABLE]}->[VALUES][$row];
1119 3 ${$self->[TABLE]}->[VALUES][$row] = undef;
1120 3 return $old_value;
1121 }
1122  
1123 sub CLEAR {
1124 2 ▶ my $self = shift;
Subroutine CLEAR: called CC=2 SCAR=6.9
1125  
1126 2 ▶ croak "attempt to modify read-only object" if ${$self->[TABLE]}->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if ${$$self[0];}->[4] 1 1 2
1127  
1128 1 $self->[TABLE]->field($self->[KEY] => []);
1129 }
1130  
1131 sub PUSH {
1132 11 ▶ my $self = shift;
Subroutine PUSH: called CC=5 SCAR=16.1
1133  
1134 11 my $T = $self->[TABLE];
1135  
1136 11 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] and scalar @_ 1 10 11
Condition100%2/2
$$T->[4] and scalar @_
!ll
92
MC/DC100%1/1
$$T->[4] and scalar @_
$$T->[4]
Truth table100%2/2
A
A= $$T->[4]
Aresult
00
11
1137 if $$T->[IS_RO] && scalar @_;
1138  
1139 10 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 9 10
1140  
1141 10 ▶ if (defined $$T->[INDEX]{$self->[KEYc]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$T->[3]{$self->[2]}) { } 5 5 10
1142 # key exists ==> extend table efficiently
1143 5 my $last_row = $$T->[INDEX]{$self->[KEYc]}[-1];
1144 5 my $new_rows = scalar @_;
1145 5 splice @{$$T->[NAMES]}, 1+$last_row, 0,
1146 (($$T->[NAMES][$last_row]) x $new_rows);
1147 5 splice @{$$T->[VALUES]}, 1+$last_row, 0, map {"$_"} @_;
1148 5 $T->_update_INDEX($last_row, $new_rows);
1149 5 push @{$$T->[INDEX]{$self->[KEYc]}}, 1+$last_row .. $last_row+$new_rows;
1150 } else {
1151 # make key ==> use existing setter
1152 5 $T->_set_multiple_value($self->[KEY], [map {"$_"} @_]);
1153 }
1154 10 $$T->[MVOFF] = undef;
1155 }
1156  
1157 sub POP {
1158 7 ▶ my $self = shift;
Subroutine POP: called CC=6 SCAR=17.9
1159  
1160 7 my $T = $self->[TABLE];
1161  
1162 7 ▶ croak "attempt to modify read-only object" if $$T->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] 1 6 7
1163  
1164 6 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 5 6
1165  
1166 6 ▶ return undef unless defined $$T->[INDEX]{$self->[KEYc]};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3]{$self->[2]} 1 5 6
1167  
1168 5 my $rem_row = $$T->[INDEX]{$self->[KEYc]}[-1];
1169 5 my $value = $$T->[VALUES][$rem_row];
1170  
1171 5 splice @{$$T->[NAMES]}, $rem_row, 1;
1172 5 splice @{$$T->[VALUES]}, $rem_row, 1;
1173 5 ▶ splice @{$$T->[MVOFF]}, $rem_row, 1 if defined $$T->[MVOFF];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$T->[2] 1 4 5
1174 5 $T->_update_INDEX($rem_row, -1);
1175 5 pop @{$$T->[INDEX]{$self->[KEYc]}};
1176 # special case: popped last value
1177 delete $$T->[INDEX]{$self->[KEYc]}
1178 5 ▶ if scalar @{$$T->[INDEX]{$self->[KEYc]}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$$T->[3]{$self->[2]};} == 0 1 4 5
1179  
1180 5 return $value;
1181 }
1182  
1183 sub SHIFT {
1184 6 ▶ my $self = shift;
Subroutine SHIFT: called CC=5 SCAR=16.1
1185  
1186 6 my $T = $self->[TABLE];
1187  
1188 6 ▶ croak "attempt to modify read-only object" if $$T->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] 1 5 6
1189  
1190 5 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 4 5
1191  
1192 5 ▶ return undef unless defined $$T->[INDEX]{$self->[KEYc]};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3]{$self->[2]} 1 4 5
1193  
1194 4 my $rem_row = $$T->[INDEX]{$self->[KEYc]}[0];
1195 4 my $value = $$T->[VALUES][$rem_row];
1196  
1197 4 splice @{$$T->[NAMES]}, $rem_row, 1;
1198 4 splice @{$$T->[VALUES]}, $rem_row, 1;
1199 4 $$T->[MVOFF] = undef;
1200 4 $T->_update_INDEX($rem_row, -1);
1201 4 shift @{$$T->[INDEX]{$self->[KEYc]}};
1202 # special case: shifted last value
1203 delete $$T->[INDEX]{$self->[KEYc]}
1204 4 ▶ if scalar @{$$T->[INDEX]{$self->[KEYc]}} == 0;
Branch: 1 branch
BranchTrueFalseTotal
if scalar @{$$T->[3]{$self->[2]};} == 0 1 3 4
1205  
1206 4 return $value;
1207 }
1208  
1209 sub UNSHIFT {
1210 5 ▶ my $self = shift;
Subroutine UNSHIFT: called CC=5 SCAR=16.1
1211  
1212 5 my $T = $self->[TABLE];
1213  
1214 5 ▶ croak "attempt to modify read-only object"
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] and scalar @_ 1 4 5
Condition100%2/2
$$T->[4] and scalar @_
!ll
32
MC/DC100%1/1
$$T->[4] and scalar @_
$$T->[4]
Truth table100%2/2
A
A= $$T->[4]
Aresult
00
11
1215 if $$T->[IS_RO] && scalar @_;
1216  
1217 4 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 3 4
1218  
1219 4 ▶ if (defined $$T->[INDEX]{$self->[KEYc]}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$T->[3]{$self->[2]}) { } 3 1 4
1220 # key exists ==> extend table efficiently
1221 3 my $first_row = $$T->[INDEX]{$self->[KEYc]}[0];
1222 3 my $new_rows = scalar @_;
1223 3 splice @{$$T->[NAMES]}, $first_row, 0,
1224 (($$T->[NAMES][$first_row]) x $new_rows);
1225 3 splice @{$$T->[VALUES]}, $first_row, 0, map {"$_"} @_;
1226 3 $T->_update_INDEX($first_row - 1, $new_rows);
1227 3 unshift @{$$T->[INDEX]{$self->[KEYc]}},
1228 $first_row .. $first_row-1+$new_rows;
1229 } else {
1230 # make key ==> use existing setter
1231 1 $T->_set_multiple_value($self->[KEY], [map {"$_"} @_]);
1232 }
1233 4 $$T->[MVOFF] = undef;
1234 }
1235  
1236 sub SPLICE {
1237 9 ▶ my $self = shift;
Subroutine SPLICE: called CC=8 SCAR=20.8
1238 9 my $offset = shift;
1239 9 my $length = shift;
1240  
1241 9 ▶ $offset = 0 unless defined $offset;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $offset 1 8 9
1242 9 ▶ $length = $self->FETCHSIZE() - $offset unless defined $length;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $length 2 7 9
1243  
1244 9 ▶ return () unless ($length != 0 || scalar @_);
Branch: 1 branch
BranchTrueFalseTotal
unless $length != 0 or scalar @_ 1 8 9
Condition100%2/2
$length != 0 or scalar @_
l!l
63
MC/DC100%1/1
$length != 0 or scalar @_
$length != 0
Truth table100%2/2
A
A= $length != 0
Aresult
11
00
1245  
1246 8 my $T = $self->[TABLE];
1247  
1248 8 ▶ croak "attempt to modify read-only object" if $$T->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$T->[4] 2 6 8
1249  
1250 6 ▶ $T->_rebuild_INDEX unless defined $$T->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$T->[3] 1 5 6
1251  
1252 6 my @new = ();
1253 5 @new = @{$$T->[VALUES]}[@{$$T->[INDEX]{$self->[KEYc]}}]
1254 6 ▶ if defined $$T->[INDEX]{$self->[KEYc]};
Branch: 1 branch
BranchTrueFalseTotal
if defined $$T->[3]{$self->[2]} 5 1 6
1255 6 my @old = splice @new, $offset, $length, map {"$_"} @_;
1256 6 $self->[TABLE]->field($self->[KEY] => \@new);
1257  
1258 6 return @old;
1259 }
1260  
1261 458 ▶ sub UNTIE { our $_total_untied; $_total_untied++ }
Subroutine UNTIE: called CC=1 SCAR=0.0
1262  
1263 458 ▶ sub DESTROY { our $_total_destroyed; $_total_destroyed++ }
Subroutine DESTROY: called CC=1 SCAR=0.0
1264 }
1265  
1266 {
1267 28 package WARC::Fields::TiedHash::Value;
1268  
1269 # This package is a magical array that appears to be a string if it has
1270 # only one value, otherwise string conversion gives the usual
1271 # nearly-useless debugging value.
1272  
1273 # The actual underlying array is a tied array that forwards mutating
1274 # operations to the original WARC::Fields object.
1275  
1276 28 ▶ use overload '""' => '_as_string', fallback => 1;
Subroutine BEGIN: called CC=1 SCAR=0.0
1277  
1278 28 ▶ use Scalar::Util qw/refaddr reftype/;
Subroutine BEGIN: called CC=1 SCAR=0.0
1279  
1280 sub _new {
1281 458 ▶ my $class = shift;
Subroutine _new: called CC=1 SCAR=0.0
1282 458 my $parent = shift;
1283 458 my $key = shift;
1284  
1285 458 my @values;
1286 458 tie @values, (ref($parent).'::ValueArray'), $$parent, $key;
1287  
1288 458 bless \@values, $class;
1289 }
1290  
1291 sub _as_string {
1292 680 ▶ my $self = shift;
Subroutine _as_string: called CC=2 SCAR=6.9
1293  
1294 680 ▶ return scalar @$self == 1
Branch: 1 branch
BranchTrueFalseTotal
scalar @$self == 1 ? : 672 8 680
1295 ? $self->[0] : sprintf ('%s(0x%x)', reftype $self, refaddr $self);
1296 }
1297  
1298 458 ▶ sub DESTROY { untie @{(shift)} }
Subroutine DESTROY: called CC=1 SCAR=0.0
1299 }
1300  
1301 {
1302 28 package WARC::Fields::TiedHash;
1303  
1304 28 ▶ use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
1305  
1306 BEGIN { $WARC::Fields::TiedHash::{$_} = $WARC::Fields::{$_}
1307 28 ▶ for WARC::Fields::OBJECT_INDEX; }
Subroutine BEGIN: called CC=2 SCAR=6.9
1308  
1309 # The underlying object is a reference to a WARC::Fields object.
1310  
1311 sub FETCH {
1312 461 ▶ my $self = shift;
Subroutine FETCH: called CC=2 SCAR=6.9
1313 461 my $key = shift;
1314 461 ▶ croak "reference to invalid field name" if $key !~ m/^$PARSE_RE__token$/o;
Branch: 1 branch
BranchTrueFalseTotal
unless $key =~ /^$PARSE_RE__token$/o 3 458 461
1315 458 return (ref($self).'::Value')->_new($self, $key);
1316 }
1317  
1318 sub STORE {
1319 11 ▶ my $self = shift;
Subroutine STORE: called CC=2 SCAR=6.9
1320 11 my $key = shift;
1321 11 my $value = shift;
1322  
1323 11 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 2 9 11
1324  
1325 9 $$self->field($key => $value);
1326 }
1327  
1328 sub DELETE {
1329 6 ▶ my $self = shift;
Subroutine DELETE: called CC=2 SCAR=6.9
1330 6 my $key = shift;
1331  
1332 6 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 5 6
1333  
1334 5 $$self->field($key => []);
1335 }
1336  
1337 sub CLEAR {
1338 2 ▶ my $self = shift;
Subroutine CLEAR: called CC=2 SCAR=6.9
1339  
1340 2 ▶ croak "attempt to modify read-only object" if $$$self->[IS_RO];
Branch: 1 branch
BranchTrueFalseTotal
if $$$self->[4] 1 1 2
1341  
1342 1 $$$self->[NAMES] = [];
1343 1 $$$self->[VALUES] = [];
1344 1 $$$self->[MVOFF] = undef;
1345 1 $$$self->[INDEX] = undef;
1346 1 return undef;
1347 }
1348  
1349 sub EXISTS {
1350 15 ▶ my $self = shift;
Subroutine EXISTS: called CC=2 SCAR=6.9
1351 15 my $key = shift;
1352  
1353 15 ▶ $$self->_rebuild_INDEX unless defined $$$self->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$$self->[3] 1 14 15
1354 15 return exists $$$self->[INDEX]->{$$self->_find_key($key)};
1355 }
1356  
1357 sub FIRSTKEY {
1358 64 ▶ my $self = shift;
Subroutine FIRSTKEY: called CC=1 SCAR=0.0
1359  
1360 64 return $$$self->[NAMES][0];
1361 }
1362  
1363 sub NEXTKEY {
1364 313 ▶ my $self = shift;
Subroutine NEXTKEY: called CC=4 SCAR=13.9
1365 313 my $from_key = shift;
1366  
1367 313 ▶ $$self->_rebuild_INDEX unless defined $$$self->[INDEX];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $$$self->[3] 2 311 313
1368  
1369 313 my $i;
1370 313 ▶ for ($i = $$$self->[INDEX]{$$self->_find_key($from_key)}[0] + 1;
Branch: 1 branch
BranchTrueFalseTotal
if (defined $$$self->[0][$i] and $i != $$$self->[3]{$$self->_find_key($$$self->[0][$i])}[0]) 8 313 321
Condition100%3/3
defined $$$self->[0][$i] and $i != $$$self->[3]{$$self->_find_key($$$self->[0][$i])}[0]
!ll&&!rl&&r
642498
MC/DC100%2/2
defined $$$self->[0][$i] and $i != $$$self->[3]{$$self->_find_key($$$self->[0][$i])}[0]
defined $$$self->[0][$i] $i != $$$self->[3]{$$self->_find_key($$$self->[0][$i])}[0]
Truth table100%3/3
A and B
A= defined $$$self->[0][$i]B= $i != $$$self->[3]{$$self->_find_key($$$self->[0][$i])}[0]
ABresult
0X0
100
111
1371 defined $$$self->[NAMES][$i] and
1372 $i != $$$self->[INDEX]{$$self->_find_key($$$self->[NAMES][$i])}[0];
1373 $i++) {}
1374 313 return $$$self->[NAMES][$i];
1375 }
1376  
1377 sub SCALAR {
1378 3 ▶ my $self = shift;
Subroutine SCALAR: called CC=1 SCAR=0.0
1379 3 return scalar @{$$$self->[NAMES]};
1380 }
1381  
1382 70 ▶ sub UNTIE { our $_total_untied; $_total_untied++ }
Subroutine UNTIE: called CC=1 SCAR=0.0
1383  
1384 70 ▶ sub DESTROY { our $_total_destroyed; $_total_destroyed++ }
Subroutine DESTROY: called CC=1 SCAR=0.0
1385 }
1386  
1387 =head2 Overloaded Dereference Operators
1388  
1389 The C<WARC::Fields> class provides overloaded dereference operators for
1390 array and hash dereferencing. The overloaded operators provide an
1391 anonymous tied array or hash as needed, allowing the object itself to be
1392 used as a reference to its tied array and hash interfaces. There is a
1393 caveat, however, so read on.
1394  
1395 =cut
1396  
1397 28 sub _as_tied_array {
1398 # To avoid confusing bugs due to typos producing overloaded dereferences
1399 # instead of intended accesses to the internal object, this feature
1400 # cannot be used from within this module.
1401 168 ▶ if (scalar caller =~ m/^WARC::Fields/) {
Subroutine _as_tied_array: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (scalar((caller) =~ /^WARC::Fields/)) 1 167 168
1402 1 local $Carp::CarpLevel = 1;
1403 1 confess "overloaded array dereference in internal code"
1404 }
1405  
1406 167 my $self = shift;
1407  
1408 167 ▶ return $$self->[C_TA] if defined $$self->[C_TA];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[5] 164 3 167
1409  
1410 3 my @array; $$self->[C_TA] = \@array;
1411 3 Scalar::Util::weaken ${tie @array, ref $self, $self};
1412 3 return $$self->[C_TA];
1413 }
1414  
1415 sub _as_tied_hash {
1416 # To avoid confusing bugs due to typos producing overloaded dereferences
1417 # instead of intended accesses to the internal object, this feature
1418 # cannot be used from within this module.
1419 272 ▶ if (scalar caller =~ m/^WARC::Fields/) {
Subroutine _as_tied_hash: called CC=3 SCAR=11.0
Branch: 1 branch
BranchTrueFalseTotal
if (scalar((caller) =~ /^WARC::Fields/)) 1 271 272
1420 1 local $Carp::CarpLevel = 1;
1421 1 confess "overloaded hash dereference in internal code"
1422 }
1423  
1424 271 my $self = shift;
1425  
1426 271 ▶ return $$self->[C_TH] if defined $$self->[C_TH];
Branch: 1 branch
BranchTrueFalseTotal
if defined $$self->[6] 208 63 271
1427  
1428 63 my %hash; $$self->[C_TH] = \%hash;
1429 63 Scalar::Util::weaken ${tie %hash, ref $self, $self};
1430 63 return $$self->[C_TH];
1431 }
1432  
1433 =head3 Reference Count Trickery with Overloaded Dereference Operators
1434  
1435 To avoid problems, the underlying tied object is a reference to the parent
1436 object. For ordinary use of C<tie>, this is a strong reference, however,
1437 the anonymous tied array and hash are cached in the object to avoid having
1438 to C<tie> a new object every time the dereference operators are used.
1439  
1440 To prevent memory leaks due to circular references, the overloaded
1441 dereference operators tie a I<weak> reference to the parent object. The
1442 tied aggregate always holds a strong reference to its object, but when the
1443 dereference operators are used, that inner object is a I<weak> reference to
1444 the actual C<WARC::Fields> object.
1445  
1446 The caveat is thus: do not attempt to save a reference to the array or hash
1447 produced by dereferencing a C<WARC::Fields> object. The parent
1448 C<WARC::Fields> object must remain in scope for as long as any anonymous
1449 tied aggregates exist.
1450  
1451 =cut
1452  
1453 28 1;
1454 __END__
« WARC/Index/File/CDX.pm ↑ Summary WARC/Record/Block.pm »