Shoebox/Entry.pm

filter: Statementstmt 80.2% 146 / 182 Branchbran 68.9% 51 / 74 Conditioncond 70.3% 19 / 27 MC/DCmcdc 56.2% 9 / 16 Subroutinesub 85.7% 30 / 35 Podpod 100% 23 / 23 totaltotal 77.8% 278 / 357 CC 53 SCAR 18.2
CC
53
Cov
76%
assert_keys_unique
30.0
dump
12.0
__ANON__
12.0
CRAP 6.2
« Shoebox/Lexicon.pm ↑ Summary
1  
2 3 require 5;
3 package Text::Shoebox::Entry;
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use vars qw(@ISA $Debug $VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶use integer;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 ▶use Text::Shoebox 1.02 ();
Subroutine BEGIN: called CC=1 SCAR=0.0
8 3 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 3 ▶$Debug = 0 unless defined $Debug;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $Text::Shoebox::Entry::Debug 3 0 3
11 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
12 3 $VERSION = "1.02";
13 }
14 3 my $Array_Class;
15  
16 0 ▶unless($Text::Shoebox::Lexicon::VERSION) { require Text::Shoebox::Lexicon; }
Branch: 1 branch
BranchTrueFalseTotal
unless ($Text::Shoebox::Lexicon::VERSION) 0 3 3
17  
18 ###########################################################################
19  
20 =head1 NAME
21  
22 Text::Shoebox::Entry - class for Shoebox SF lexicon entries
23  
24 =head1 SYNOPSIS
25  
26 use Text::Shoebox::Lexicon;
27 my $lex = Text::Shoebox::Lexicon->read_file( "haida.sf" );
28  
29 foreach my $entry ($lex->entries) {
30 #
31 # Each $entry is a Text::Shoebox::Entry object
32 #
33 my %e = $entry->as_list;
34 print "Looky, stuff! [", %e, "]!\n";
35 }
36  
37 =head1 DESCRIPTION
38  
39 An object of this class represents an entry in an SF lexicon
40 (L<Text::Shoebox::Lexicon>).
41  
42 An entry consists of a number of fields. Each field has two scalars,
43 a key, and a value. The first field in an entry is considered the
44 headword field, and its key must occur there and only there in that
45 entry. There is no requirement on uniqueness of keys in the rest of
46 the entry.
47  
48  
49 =head1 METHODS
50  
51  
52 =over
53  
54 =item $entry = Text::Shoebox::Entry->new();
55  
56 =item $entry = Text::Shoebox::Entry->new( 'foo' => 'bar', 'baz' => 'quux' );
57  
58 The C<new> method creates a new Text::Shoebox::Entry method. If you
59 provide parameters, as in the second example, those are used as the
60 contents of the new object.
61  
62 Normally you don't need to expressly create objects of this class,
63 as Text::Shoebox::Lexicon will create them as needed when you call
64 a Text::Shoebox::Lexicon C<read_file> or C<read_handle> method.
65  
66 =item $entry2 = $entry->copy
67  
68 This returns a copy of the object in $entry.
69  
70 =cut
71  
72 22 ▶sub new { # Text::Shoebox is free to not use this, for speed's sake
Subroutine new: called CC=5 SCAR=19.6
Pod documented
73 22 my $class = shift;
74 22 ▶ $class = ref($class) || $class; # be an object or a class method
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
0220
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
75 22 ▶ print "New object in class $class\n" if $Debug;
Branch: 1 branch
BranchTrueFalseTotal
if $Text::Shoebox::Entry::Debug 0 22 22
76 22 ▶ if(@_ == 1 and ref($_[0]) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1 and ref $_[0] eq 'ARRAY') { } 0 22 22
Condition33%1/3
@_ == 1 and ref $_[0] eq "ARRAY"
!ll&&!rl&&r
2200
MC/DC0%0/2
@_ == 1 and ref $_[0] eq "ARRAY"
@_ == 1 ref $_[0] eq "ARRAY"
Truth table33%1/3
A and B
A= @_ == 1B= ref $_[0] eq "ARRAY"
ABresult
0X0
100
111
77 # listref form -- call as: Text::Shoebox::Entry->new([foo => 'bar']);
78 0 return bless $_[0], $class;
79 } else {
80 # list form -- call as: Text::Shoebox::Entry->new(foo => 'bar');
81 22 return bless [@_], $class;
82 }
83 }
84  
85 1 ▶sub copy {
Subroutine copy: called CC=2 SCAR=7.1
Pod documented
86 1 my $original = $_[0];
87 1 ▶ Carp::croak("Text::Shoebox::entry is strictly an object method.")
Branch: 1 branch
BranchTrueFalseTotal
unless ref $original 0 1 1
88 unless ref $original;
89 1 return bless( [@$original], ref($original) );
90 # bless into the same class as the original
91 # presumably a deep copy isn't necessary!
92 }
93  
94 #--------------------------------------------------------------------------
95  
96 =item @keys = $entry->keys;
97  
98 This returns the names of all the keys (a/k/a fieldnames) in this entry.
99 For example, if $entry is
100 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
101 then $entry->keys will return the list C<('hw', 'ex', 'ex')>.
102  
103 =item @values = $entry->values;
104  
105 This returns the values of all fields in this entry.
106 For example, if $entry is
107 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
108 then $entry->values will return the list C<('foo', 'Things', 'Stuff')>.
109  
110 =cut
111  
112 2 ▶sub keys {
Subroutine keys: called CC=2 SCAR=6.9
Pod documented
113 2 my @out;
114 2 ▶ for(my $i = 0; $i < @{$_[0]}; $i += 2) { push @out, $_[0][$i] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 5 2 7
115 2 return @out;
116 }
117  
118 1 ▶sub values {
Subroutine values: called CC=2 SCAR=6.9
Pod documented
119 1 my @out;
120 1 ▶ for(my $i = 1; $i < @{$_[0]}; $i += 2) { push @out, $_[0][$i] }
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 3 1 4
121 1 return @out;
122 }
123  
124 #--------------------------------------------------------------------------
125  
126 =item $headword = $entry->headword;
127  
128 This returns the headword value of this entry. This is basically
129 a shortcut for C<< ($entry->values)[0] >>
130  
131 =item $headword_field = $entry->headword_field;
132  
133 This returns the fieldname of this entry's headword field. This
134 is basically a shortcut for C<< ($entry->keys)[0] >>
135  
136 =item $value = $entry->_(I<keyname>)
137  
138 Yes, this method really is called _ !
139  
140 This gets all the values of the pairs that have the key I<keyname>. How
141 it returns those values (of which there may be none, one, or many) depends
142 on context:
143  
144 In list context, this simply returns the list of found values.
145 E.g., if $entry is
146 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
147 then C<< $entry->_('hw') >> returns the one-item list C<('foo')>,
148 then C<< $entry->_('zorp') >> returns the zero-item list C<()>,
149 and C<< $entry->_('ex') >> returns the two-item list C<('Things', 'Stuff')>.
150  
151 In scalar context, this returns undef if no values were found.
152 Otherwise it returns a magical arrayref containing the list of
153 (one or more) found values. What's special about these arrayrefs
154 is that if you treat one as a plain string, you get the useful value
155 C<join "; ", @$array_ref)>, instead of nonsense like
156 "ARRAY(0x1555294)".
157  
158 (Internally, this is implemented just like L<Array::Autojoin>, which see.)
159  
160 =cut
161  
162  
163 1 ▶sub headword { @{$_[0]} ? $_[0][1] : undef } # simply the first value
Subroutine headword: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} ? : 1 0 1
164 1 ▶sub headword_field { @{$_[0]} ? $_[0][0] : undef } # simply the first key
Subroutine headword_field: called CC=2 SCAR=7.2
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
@{$_[0];} ? : 1 0 1
165  
166 19 ▶sub Text::Shoebox::Entry::_ {
Subroutine _: called CC=6 SCAR=18.2
167 19 my($self, $key) = @_;
168 19 ▶ return unless defined $key;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $key 0 19 19
169  
170 19 my @out;
171 19 ▶ for(my $i = 0; $i < @$self; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$self) 54 19 73
172 54 ▶ push @out, $self->[$i+1] if $key eq $self->[$i];
Branch: 1 branch
BranchTrueFalseTotal
if $key eq $self->[$i] 16 38 54
173 }
174 19 ▶ if(wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 19 19
175 0 return @out;
176 } else {
177 19 ▶ return unless @out;
Branch: 1 branch
BranchTrueFalseTotal
unless @out 4 15 19
178 15 return bless \@out, $Array_Class;
179 }
180 }
181  
182  
183  
184 =item my $num_pairs = $entry->pair_count;
185  
186 This returns an integer expressing the number of pairs in this entry.
187 It's basically the same as scalar($entry->keys).
188  
189  
190 =item ($key, $val) = $entry->pair($n)
191  
192 This returns the key and value of pair number $n for this entry.
193 E.g., if $entry is
194 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
195 then $entry->pair(1) is the list C<('ex', 'Things')>.
196  
197  
198 =item ($key, $val, $k2, $v2) = $entry->pairs($n, $m)
199  
200 This returns the key and value of pair number $n
201 and the key and value of pair number $m for this entry.
202 E.g., if $entry is
203 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
204 then $entry->pair(0,2) is the list C<('hw' => 'foo', 'ex', "Stuff")>.
205  
206 (Actually, C<< $entry->pair(...) >> is just an alias to
207 C<< $entry->pairs(...) >>.)
208  
209  
210 =cut
211  
212 1 ▶sub pair_count { return @{$_[0]} / 2; }
Subroutine pair_count: called CC=1 SCAR=0.0
Pod documented
213  
214 4 ▶sub pair { (shift)->pairs(@_) } #alias
Subroutine pair: called CC=1 SCAR=0.0
Pod documented
215  
216 4 ▶sub pairs { # also good for accessing one pair, or none!
Subroutine pairs: called CC=1 SCAR=0.0
Pod documented
217 # get pair #3 (assuming counting from 0) : ($k,$v) = $e->pairs(3);
218 4 my $o = shift;
219 4 map { @{$o}[$_ * 2, $_ * 2 + 1] } @_;
220 # map to slices. Better be legal offsets!
221 # e.g., 3 maps to @{$o}[6,7]
222 }
223  
224 #--------------------------------------------------------------------------
225  
226 =item $true_or_false = $entry->are_keys_unique;
227  
228 This returns true iff every keyname in this entry appears only once.
229  
230 =item $entry->assert_keys_unique;
231  
232 This dies unless C<< $entry->are_keys_unique >> is true.
233  
234 =cut
235  
236 7 ▶sub are_keys_unique {
Subroutine are_keys_unique: called CC=4 SCAR=13.9
Pod documented
237 # returns true iff the keys are unique in this entry.
238 # i.e., if no headword occurs twice (or more)
239 7 ▶ return 1 if @{$_[0]} < 2; # can't have collisions with just one key!
Branch: 1 branch
BranchTrueFalseTotal
if @{$_[0];} < 2 1 6 7
240  
241 6 my %seen;
242 6 ▶ for(my $i = 0; $i < @{$_[0]}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 15 4 19
243 15 ▶ return 0 if $seen{$_[0][$i]}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$_[0][$i]}++ 2 13 15
244 }
245 4 return 1;
246 }
247  
248 0 ▶sub assert_keys_unique {
Subroutine assert_keys_unique: not called CC=5 SCAR=34.0
Pod documented
249 0 ▶ return $_[0] if $_[0]->are_keys_unique;
Branch: 1 branch
BranchTrueFalseTotal
if $_[0]->are_keys_unique 0 0 0
250 0 my $e = shift;
251 0 my @k = $e->keys;
252 0 my %seen;
253 0 for my $k (@k) { ++$seen{$k} }
254 0 ▶ for my $k (@k) { $k = uc $k if $seen{$k} > 1 }
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$k} > 1 0 0 0
255 0 Carp::croak "Entry $e \"$$e[1]\" has duplicate keys: [@k]\nAborting";
256 }
257  
258  
259 =item $true_or_false = $entry->is_null
260  
261 This returns true iff this entry is empty. This is basically the same
262 as C<< 0 == $entry->pair_count >>.
263  
264 =item $true_or_false = $entry->is_sane
265  
266 This returns true iff this entry is non-null, contains no
267 references, and if no keyname is undef or zero-length.
268  
269 =item $entry->scrunch;
270  
271 For all values in this entry, this compacts all whitespace, deletes
272 leading and trailing whitespace, and deletes any pairs where the
273 value is blank. (Where "blank" means undef, zero-length, or
274 is all-whitespace.)
275  
276 =item $entry->dump;
277  
278 This prints (not returns!) a representation of this object's contents.
279  
280 =cut
281  
282  
283 3 ▶sub is_null { return( @{$_[0]} == 0 ) }
Subroutine is_null: called CC=1 SCAR=0.0
Pod documented
284  
285 7 ▶sub is_sane {
Subroutine is_sane: called CC=7 SCAR=19.5
Pod documented
286 7 my $e = $_[0];
287 7 ▶ return 0 unless @$e; # empty entries are not sane
Branch: 1 branch
BranchTrueFalseTotal
unless @$e 1 6 7
288 6 ▶ for(my $i = 0; $i < @{$_[0]}; $i += 2) { # scan keys
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 10 1 11
289 10 ▶ return 0 unless defined $e->[$i] and length $e->[$i];
Branch: 1 branch
BranchTrueFalseTotal
unless defined $e->[$i] and length $e->[$i] 4 6 10
Condition100%3/3
defined $e->[$i] and length $e->[$i]
!ll&&!rl&&r
226
MC/DC100%2/2
defined $e->[$i] and length $e->[$i]
defined $e->[$i] length $e->[$i]
Truth table100%3/3
A and B
A= defined $e->[$i]B= length $e->[$i]
ABresult
0X0
100
111
290 # all keys have to be defined and be non-null
291 6 ▶ return 0 if ref $e->[$i] or ref $e->[$i+1];
Branch: 1 branch
BranchTrueFalseTotal
if ref $e->[$i] or ref $$e[$i + 1] 1 5 6
Condition66%2/3
ref $e->[$i] or ref $$e[$i + 1]
l!l&&r!l&&!r
015
MC/DC50%1/2
ref $e->[$i] or ref $$e[$i + 1]
ref $e->[$i] ref $$e[$i + 1]
Truth table66%2/3
A or B
A= ref $e->[$i]B= ref $$e[$i + 1]
ABresult
1X1
011
000
292 # no references anywhere!
293 }
294 1 return 1;
295 }
296  
297 #--------------------------------------------------------------------------
298  
299 3 ▶sub scrunch {
Subroutine scrunch: called CC=4 SCAR=13.9
Pod documented
300 3 my $e = $_[0];
301 3 ▶ for(my $i = 1; $i < @$e; $i += 2) { # scan keys
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$e) 8 3 11
302 8 ▶ unless( defined $e->[$i] and $e->[$i] =~ m/\S/ ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $e->[$i] and $e->[$i] =~ /\S/) 2 6 8
Condition66%2/3
defined $e->[$i] and $e->[$i] =~ /\S/
!ll&&!rl&&r
026
MC/DC50%1/2
defined $e->[$i] and $e->[$i] =~ /\S/
defined $e->[$i] $e->[$i] =~ /\S/
Truth table66%2/3
A and B
A= defined $e->[$i]B= $e->[$i] =~ /\S/
ABresult
0X0
100
111
303 2 splice @$e, $i-1, 2; # nix K=>V where V is null or all-whitespace
304 2 $i-=2;
305 }
306 8 $e->[$i] =~ s/^\s+//s;
307 8 $e->[$i] =~ s/\s+$//s;
308 8 $e->[$i] =~ s/[ \t]*[\n\r]+[ \t]*/ /g;
309 # remove newlines and any whitespace around them
310 }
311 3 return $e;
312 }
313  
314 #--------------------------------------------------------------------------
315  
316 0 ▶sub dump {
Subroutine dump: not called CC=3 SCAR=24.8
Pod documented
317 0 my $e = $_[0];
318  
319 0 print "Entry $e contains:\n";
320  
321 0 my $safe;
322 0 my $toggle = 0;
323 0 foreach my $v (@$e) {
324 0 ($safe = $v) =~
325 s<([^\x20\x21\x23\x27-\x3F\x41-\x5B\x5D-\x7E\xA1-\xFE])>
326 0 <$Text::Shoebox::p{$1}>eg;
327 0 ▶ print(
Branch: 1 branch
BranchTrueFalseTotal
($toggle ^= 1) ? : 0 0 0
328 ($toggle ^= 1) ? qq{ $safe = } : qq{"$safe"\n}
329 );
330 }
331 0 print "\n";
332 0 return $e;
333 }
334  
335 #--------------------------------------------------------------------------
336  
337 =item @it = $entry->as_list
338  
339 This returns a list expressing the contents of $entry.
340 For example, if $entry is
341 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
342 then this returns just that,
343 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>.
344  
345 =item $them = $entry->as_arrayref
346  
347 This returns an arrayref (probably blessed) to the contents
348 of $entry. For example, if $entry is
349 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
350 then this will return an arrayred to just that list.
351  
352 Note that this (and as_HoLS) is like the other C<as_I<thing>>
353 methods, in that this doesn't return any sort of copy; it returns
354 a reference to the entry's array itself -- if you do $them->[1] = 'x',
355 then $entry's contents change to
356 C<< ('hw' => 'x', 'ex' => 'Things', 'ex' => "Stuff") >>.)
357  
358 (Internally, this method is implemented by simply returning
359 $entry itself, since I<in the current implementation>,
360 $entry I<is> just a blessed arrayref to the C<(k,v,k,v,...)>
361 list it contains.)
362  
363 =item $h = $entry->as_hashref
364  
365 This returns a hashref expressing the contents of $entry
366 as a C<< {key => value,...} >> hash, I<discarding duplicates.>
367 For example, if $entry is
368 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
369 then this returns
370 C<< { 'hw' => 'foo', 'ex' => "Stuff" } >>.
371  
372 =cut
373  
374 8 ▶sub as_list { return @{$_[0]} }
Subroutine as_list: called CC=1 SCAR=0.0
Pod documented
375 1 ▶sub as_arrayref { return $_[0] }
Subroutine as_arrayref: called CC=1 SCAR=0.0
Pod documented
376 1 ▶sub as_hashref { return {@{$_[0]}} }
Subroutine as_hashref: called CC=1 SCAR=0.0
Pod documented
377  
378  
379  
380  
381 =item $hol = $entry->as_HoL
382  
383 This returns a reference to a hash-of-lists expressing
384 the contents of this entry, i.e., a reference to a hash where
385 every value is an arrayref. Note that this doesn't destroy
386 duplicates.
387  
388 For example, if $entry is
389 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
390 then this returns
391 C<< { 'hw' => ['foo'], 'ex' => ['Things', 'Stuff'] } >>.
392  
393 And there's a useful bit of magic added -- the arrayrefs aren't just
394 plain arrayrefs, they're special arrayrefs (implemented
395 just like L<Array::Autojoin>) such that if you treat one
396 as a plain string, you get the useful value
397 C<join "; ", @$array_ref)>, instead of nonsense like
398 "ARRAY(0x1555294)".
399  
400 =cut
401  
402  
403 2 ▶sub as_HoL {
Subroutine as_HoL: called CC=4 SCAR=13.9
Pod documented
404 2 my $e = $_[0];
405 2 my %h;
406 2 ▶ for(my $i = 0; $i < @$e; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$e) 6 2 8
407 6 ▶ push @{ $h{ $e->[$i] } ||= []}, $e->[$i+1];
Condition100%2/2
$h{$e->[$i]} ||= []
l!l
15
MC/DC100%1/1
$h{$e->[$i]} ||= []
$h{$e->[$i]}
Truth table100%2/2
A
A= $h{$e->[$i]}
Aresult
11
00
408 }
409 2 foreach my $v (CORE::values %h) { bless $v, $Array_Class }
410 2 \%h;
411 }
412  
413  
414 =item $hol = $entry->as_HoLS
415  
416 This returns a hashref where every value is a reference to an array
417 of scalar-refs to the value-slots in $entry. This is so you can
418 alter $entry. (This and $entry->as_arrayref are really the only ways
419 to alter an entry objects's content.)
420  
421 This sounds (and is) very circuitous, but it's like this:
422 If $entry is
423 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
424 then this returns
425 C<< { 'hw' => [$fooslot], 'ex' => [$thingsslot, $stuffslot] } >>,
426 where if you do C<$$thingslot = 'gack'>, then $entry then becomes
427 C<< ('hw' => 'foo', 'ex' => B<'gack'>, 'ex' => "Stuff") >>.
428  
429 =cut
430  
431 1 ▶sub as_HoLS { # ref to a hash of list of refs to each of the value slots
Subroutine as_HoLS: called CC=3 SCAR=11.0
Pod documented
432 1 my $e = $_[0];
433 1 my %h;
434 1 ▶ for(my $i = 0; $i < @$e; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$e) 3 1 4
435 push @{
436 3 ▶ $h{ $e->[$i] } ||= []
Condition100%2/2
$h{$e->[$i]} ||= []
l!l
12
MC/DC100%1/1
$h{$e->[$i]} ||= []
$h{$e->[$i]}
Truth table100%2/2
A
A= $h{$e->[$i]}
Aresult
11
00
437 }, \$e->[$i + 1];
438 }
439 1 \%h;
440 }
441  
442  
443  
444 =item $hol = $entry->as_doublets
445  
446 This returns this entry as a list of "doublets" -- i.e.,
447 as a list of two-item arrayrefs.
448  
449 For example, if $entry is
450 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
451 then this returns the list
452 C<< (['hw','foo'], ['ex','Things'], ['ex','Stuff']) >>.
453  
454 =cut
455  
456 1 ▶sub as_doublets {
Subroutine as_doublets: called CC=2 SCAR=6.9
Pod documented
457 # returns this entry...
458 # (hw => 'shash', english => 'bear')
459 # as this...
460 # ([hw => 'shash'], [english => 'bear'])
461 1 my @out;
462 1 ▶ for(my $i = 0; $i < @{$_[0]}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 3 1 4
463 3 push @out, [ @{ $_[0] }[$i, $i+1] ];
464 }
465 1 return @out;
466 }
467  
468  
469  
470 =item $xml_source = $entry->as_xml()
471  
472 =item $xml_source = $entry->as_xml( I<TagNameHash> )
473  
474 This returns an XML representation of this entry's contents. In short,
475 For example, if $entry is
476 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
477 then this returns this string:
478  
479 <hw>foo</hw>
480 <ex>Things</ex>
481 <ex>Stuff</ex>
482  
483 The only details arise from the problem of how to turn $entry's keynames
484 into XML tag names. For each key, if it's present in the optional TagNameHash
485 hashref parameter, then that value (C<< $tagnamehash->{$keyname} >>) is used;
486 otherwise, $keyname itself is used, stripped of characters other than
487 C<a-zA-Z0-9>, colon, underscore, period, and dash.
488  
489  
490  
491 =item $xml_source = $entry->as_xml_pairs()
492  
493 =item $xml_source = $entry->as_xml_pairs( I<PairTagName, KeyAttrName, ValAttrName>)
494  
495 This returns an XML representation of this entry's contents. In short,
496 For example, if $entry is
497 C<< ('hw' => 'foo', 'ex' => 'Things', 'ex' => "Stuff") >>,
498 then this returns this string:
499  
500 <pair key="hw" value="foo" />
501 <pair key="ex" value="Things" />
502 <pair key="ex" value="Stuff" />
503  
504 This avoids the problem of how to turn keynames into XML tagnames.
505 If you don't like the choice of the pair tagname (by default, "pair")
506 or the key attribute name (by default, "key"),
507 or the value attribute name (by default, "value"), then you can
508 specify new values as the parameters. So if you call
509 C<<
510 $entry->as_xml_pairs( 'fee' , 'fie', 'Foe:Fum')
511 >>, the return value is:
512  
513 <fee fie="hw" Foe:Fum="foo" />
514 <fee fie="ex" Foe:Fum="Things" />
515 <fee fie="ex" Foe:Fum="Stuff" />
516  
517 =cut
518  
519 1 ▶sub as_xml {
Subroutine as_xml: called CC=6 SCAR=18.6
Pod documented
520 # Yes, VERY simpleminded. And note that the result is NOT wrapped
521 # in an <entry>...</entry> or anything.
522  
523 # returns this entry...
524 # (hw => 'shash', english => 'bear')
525 # as this...
526 # " <hw>shash</hw>\n<english>bear</english>\n"
527  
528 # Consider this entry more as a suggestion, and as a debugging tool, than
529 # anything else.
530  
531 # Optional first parameter: a reference to a hash mapping key names
532 # to tags. E.g., $e->as_xml({hw => 'headword', english => 'gloss'})
533 # will give you this:
534 # " <headword>shash</headword>\n<gloss>bear</gloss>\n"
535  
536 1 ▶ my $map = ref($_[1]) ? $_[1] : {};
Branch: 1 branch
BranchTrueFalseTotal
ref $_[1] ? : 0 1 1
537  
538 1 my(@out, $k, $v);
539 1 ▶ for(my $i = 0; $i < @{$_[0]}; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$_[0];}) 3 1 4
540 3 ($k,$v) = @{$_[0]}[$i, 1 + $i];
541  
542 3 ▶ if(exists $map->{$k}){
Branch: 1 branch
BranchTrueFalseTotal
if (exists $map->{$k}) { } 0 3 3
543 0 $k = $map->{$k};
544 } else {
545 # spiff up the key name so it's an okay GI (tag name)
546 3 $k =~ tr<-._:a-zA-Z0-9><_>cd; # Yes, this is conservative
547 3 ▶ if(length $k) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $k) { } 3 0 3
548 3 ▶ $k = '_' . $k unless $k =~ m<^[_:a-zA-Z]>s;
Branch: 1 branch
BranchTrueFalseTotal
unless $k =~ /^[_:a-zA-Z]/s 0 3 3
549 # prefix unsafe things.
550 } else { # to avoid a null GI
551 0 $k = 'NULL';
552 }
553 }
554  
555 3 $v =~ s/&/&amp;/g;
556 3 $v =~ s/</&lt;/g ;
557 3 $v =~ s/>/&gt;/g ;
558 3 push @out, " <$k>$v</$k>\n";
559 }
560 1 return join '', @out;
561 }
562  
563 2 ▶sub as_xml_pairs {
Subroutine as_xml_pairs: called CC=6 SCAR=17.9
Pod documented
564 # A bit less pointless. And note that the result is still not wrapped
565 # in an <entry>...</entry> or anything.
566  
567 # Returns this entry...
568 # (hw => 'shash', english => 'bear')
569 # as this...
570 # " <pair key="hw" value="shash" /><pair key="english" value="bear"/>\n"
571  
572 # Consider this entry more as a suggestion, and as a debugging tool, than
573 # anything else.
574  
575 # Calling format: $e->as_xml_pairs(TAGNAME, KEYNAME, VALUENAME)
576 # TAGNAME defaults to 'pair'.
577 # KEYNAME defaults to 'key'.
578 # VALUENAME defaults to 'value'.
579  
580 2 my($o, $gi, $key_name, $value_name) = @_;
581 2 ▶ $gi ||= 'pair' ;
Condition100%2/2
$gi ||= "pair"
l!l
11
MC/DC100%1/1
$gi ||= "pair"
$gi
Truth table100%2/2
A
A= $gi
Aresult
11
00
582 2 ▶ $key_name ||= 'key' ;
Condition100%2/2
$key_name ||= "key"
l!l
11
MC/DC100%1/1
$key_name ||= "key"
$key_name
Truth table100%2/2
A
A= $key_name
Aresult
11
00
583 2 ▶ $value_name ||= 'value' ;
Condition100%2/2
$value_name ||= "value"
l!l
11
MC/DC100%1/1
$value_name ||= "value"
$value_name
Truth table100%2/2
A
A= $value_name
Aresult
11
00
584  
585 2 my(@out, $k, $v);
586 2 ▶ for(my $i = 0; $i < @$o; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$o) 6 2 8
587 6 ($k,$v) = @{$o}[$i, 1 + $i];
588 6 foreach my $x ($k, $v) {
589 # NB: Doesn't entitify apostrophes. No point, really.
590 12 $x =~ s/&/&amp;/g;
591 12 $x =~ s/"/&quot;/g;
592 12 $x =~ s/</&lt;/g;
593 12 $x =~ s/>/&gt;/g;
594 12 $x =~ s<([\n\t\cm\cj])>
595 0 <'&#'.(ord($1)).';'>seg;
596 # turn newlines into character references
597 }
598 6 push @out, " <$gi $key_name=\"$k\" $value_name=\"$v\" />\n"
599 }
600  
601 2 return join '', @out;
602 }
603  
604 ###########################################################################
605 {
606 # Basically just the guts of Array::Autojoin:
607  
608 package Text::Shoebox::Entry::_Autojoin;
609 3 $Array_Class = __PACKAGE__;
610  
611 3 ▶ use overload(
Subroutine BEGIN: called CC=1 SCAR=3.8
612  
613 36 ▶ '""' => sub { join '; ', @{$_[0]}},
Subroutine __ANON__: called CC=1 SCAR=0.0
614  
615 0 ▶ '0+' => sub {0 + ( $_[0][0] || 0 ) },
Subroutine __ANON__: not called CC=2 SCAR=17.9
Condition0%0/2
$_[0][0] || 0
l!l
00
MC/DC0%0/1
$_[0][0] || 0
$_[0][0]
Truth table0%0/2
A
A= $_[0][0]
Aresult
11
00
616 # stringifies and then just numerifies, but feh.
617  
618 'fallback' => 1, # turn on cleverness
619  
620 0 ▶ 'bool', => sub { # true iff there's any true items in it
Subroutine __ANON__: not called CC=3 SCAR=24.8
621 0 ▶ for (@{$_[0]}) { return 1 if $_ };
Branch: 1 branch
BranchTrueFalseTotal
if $_ 0 0 0
622 0 return '';
623 },
624  
625 0 ▶ '.=' => sub { # sure, why not.
Subroutine __ANON__: not called CC=2 SCAR=17.9
626 0 ▶ if(@{$_[0]}) { $_[0][-1] .= $_[1] } else { push @{$_[0]}, $_[1] }
Branch: 1 branch
BranchTrueFalseTotal
if (@{$_[0];}) { } 0 0 0
627 0 $_[0];
628 }, # but can't overload ||= or the like
629  
630 3 );
631 }
632 ###########################################################################
633 3 1;
634  
635 __END__
« Shoebox/Lexicon.pm ↑ Summary