Boulder/Stream.pm

filter: Statementstmt 69.2% 144 / 208 Branchbran 44.0% 44 / 100 Conditioncond 47.5% 19 / 40 MC/DCmcdc 5.0% 1 / 20 Subroutinesub 80.7% 21 / 26 Podpod 25.0% 4 / 16 totaltotal 56.8% 233 / 410 CC 72 SCAR 30.8
CC
72
Cov
59%
rearrange
71.9
read_one_record
37.9
to_fh
30.5
CRAP 21.7
« Boulder/Blast.pm ↑ Summary Boulder/Labbase.pm »
1 package Boulder::Stream;
2  
3 # CHANGE HISTORY:
4  
5 # version 1.07
6 # patches from Andy Law to quash warnings under -w switch
7  
8 # changes from 1.04 to 1.05
9 # - new() will now accept filehandle globs, IO::File, and FileHandle objects
10  
11 # changes from 1.03 to 1.04
12 # - Fixed regexp bug that broke on tags with embedded spaces -pete
13  
14 # Changes from 1.01 to 1.03
15 # - Fixed a problem in escaping the {} characters
16  
17 # Changes from 1.00 to 1.01
18 # - Added the asTable() method to Boulder::Stream
19  
20 =head1 NAME
21  
22 Boulder::Stream - Read and write tag/value data from an input stream
23  
24 =head1 SYNOPSIS
25  
26 #!/bin/perl
27 # Read a series of People records from STDIN.
28 # Add an "Eligible" tag to all those whose
29 # Age >= 35 and Friends list includes "Fred"
30 use Boulder::Stream;
31
32 # filestream way:
33 my $stream = Boulder::Stream->newFh;
34 while ( my $record = <$stream> ) {
35 next unless $record->Age >= 35;
36 my @friends = $record->Friends;
37 next unless grep {$_ eq 'Fred'} @friends;
38  
39 $record->insert(Eligible => 'yes');
40 print $stream $record;
41 }
42  
43 # object oriented way:
44 my $stream = Boulder::Stream->new;
45 while (my $record = $stream->get ) {
46 next unless $record->Age >= 35;
47 my @friends = $record->Friends;
48 next unless grep {$_ eq 'Fred'} @friends;
49  
50 $record->insert(Eligible => 'yes');
51 print $stream $record;
52 }
53  
54  
55  
56 =head1 DESCRIPTION
57  
58 Boulder::Stream provides stream-oriented access to L<Boulder> IO
59 hierarchical tag/value data. It can be used in a magic tied
60 filehandle mode, as shown in the synopsis, or in object-oriented mode.
61 Using tied filehandles, L<Stone> objects are read from input using the
62 standard <> operator. Stone objects printed to the tied filehandle
63 appear on the output stream in L<Boulder> format.
64  
65 By default, data is read from the magic ARGV filehandle (STDIN or a
66 list of files provided on the command line) and written to STDOUT.
67 This can be changed to the filehandles of your choice.
68  
69 =head2 Pass through behavior
70  
71 When using the object-oriented form of Boulder::Stream, tags which
72 aren't specifically requested by the get() method are passed through
73 to output unchanged. This allows pipes of programs to be constructed
74 easily. Most programs will want to put the tags back into the boulder
75 stream once they're finished, potentially adding their own. Of course
76 some programs will want to behave differently. For example, a
77 database query program will generate but not read a B<boulderio>
78 stream, while a report generator will read but not write the stream.
79  
80 This convention allows the following type of pipe to be set up:
81  
82 query_database | find_vector | find_dups | \
83 | blast_sequence | pick_primer | mail_report
84  
85 If all the programs in the pipe follow the conventions, then it will be
86 possible to interpose other programs, such as a repetitive element finder,
87 in the middle of the pipe without disturbing other components.
88  
89 =head1 SKELETON BOULDER PROGRAM
90  
91 Here is a skeleton example.
92  
93 #!/bin/perl
94 use Boulder::Stream;
95
96 my $stream = Boulder::Stream->newFh;
97
98 while ( my $record = <$stream> ) {
99 next unless $record->Age >= 35;
100 my @friends = $record->Friends;
101 next unless grep {$_ eq 'Fred'} @friends;
102  
103 $record->insert(Eligible => 'yes');
104 print $stream $record;
105 }
106  
107 The code starts by creating a B<Boulder::Stream> object to handle the
108 I/O. It reads from the stream one record at a time, returning a
109 L<Stone> object. We recover the I<Age> and I<Friends> tags, and
110 continue looping unless the Age is greater or equal to 35, and the
111 list of Friends contains "Fred". If these criteria match, then we
112 insert a new tag named Eligible and print the record to the stream.
113 The output may look like this:
114  
115 Name=Janice
116 Age=36
117 Eligible=yes
118 Friends=Susan
119 Friends=Fred
120 Friends=Ralph
121 =
122 Name=Ralph
123 Age=42
124 Eligible=yes
125 Friends=Janice
126 Friends=Fred
127 =
128 Name=Susan
129 Age=35
130 Eligible=yes
131 Friends=Susan
132 Friends=Fred
133 =
134  
135 Note that in this case only records that meet the criteria are echoed
136 to standard output. The object-oriented version of the program looks
137 like this:
138  
139 #!/bin/perl
140 use Boulder::Stream;
141
142 my $stream = Boulder::Stream->new;
143
144 while ( my $record = $stream->get('Age','Friends') ) {
145 next unless $record->Age >= 35;
146 my @friends = $record->Friends;
147 next unless grep {$_ eq 'Fred'} @friends;
148  
149 $record->insert(Eligible => 'yes');
150 $stream->put($record);
151 }
152  
153 The get() method is used to fetch Stones containing one or more of the
154 indicated tags. The put() method is used to send the result to
155 standard output. The pass-through behavior might produce a set of
156 records like this one:
157  
158 Name=Janice
159 Age=36
160 Eligible=yes
161 Friends=Susan
162 Friends=Fred
163 Friends=Ralph
164 =
165 Name=Phillip
166 Age=30
167 =
168 Name=Ralph
169 Age=42
170 Eligible=yes
171 Friends=Janice
172 Friends=Fred
173 =
174 Name=Barbara
175 Friends=Agatha
176 Friends=Janice
177 =
178 Name=Susan
179 Age=35
180 Eligible=yes
181 Friends=Susan
182 Friends=Fred
183 =
184  
185 Notice that there are now two records ("Phillip" and "Barbara") that
186 do not contain the Eligible tag.
187  
188 =head1 Boulder::Stream METHODS
189  
190 =head2 $stream = Boulder::Stream->new(*IN,*OUT)
191  
192 =head2 $stream = Boulder::Stream->new(-in=>*IN,-out=>*OUT)
193  
194 The B<new()> method creates a new B<Boulder::Stream> object. You can
195 provide input and output filehandles. If you leave one or both
196 undefined B<new()> will default to standard input or standard output.
197 You are free to use files, pipes, sockets, and other types of file
198 handles. You may provide the filehandle arguments as bare words,
199 globs, or glob refs. You are also free to use the named argument style
200 shown in the second heading.
201  
202 =head2 $fh = Boulder::Stream->newFh(-in=>*IN, -out=>*OUT)
203  
204 Returns a filehandle object tied to a Boulder::Stream object. Reads
205 on the filehandle perform a get(). Writes invoke a put().
206  
207 To retrieve the underlying Boulder::Stream object, call Perl's
208 built-in tied() function:
209  
210 $stream = tied $fh;
211  
212 =head2 $stone = $stream->get(@taglist)
213  
214 =head2 @stones = $stream->get(@taglist)
215  
216 Every time get() is called, it will return a new Stone object. The
217 Stone will be created from the input stream, using just the tags
218 provided in the argument list. Pass no tags to receive whatever tags
219 are present in the input stream.
220  
221 If none of the tags that you specify are in the current boulder
222 record, you will receive an empty B<Stone>. At the end of the input
223 stream, you will receive B<undef>.
224  
225 If called in an array context, get() returns a list of all stones from
226 the input stream that contain one or more of the specified tags.
227  
228 =head2 $stone = $stream->read_record(@taglist)
229  
230 Identical to get(>, but the name is longer.
231  
232 =head2 $stream->put($stone)
233  
234 Write a B<Stone> to the output filehandle.
235  
236 =head2 $stream->write_record($stone)
237  
238 Identical to put(), but the name is longer.
239  
240 =head2 Useful State Variables in a B<Boulder::Stream>
241  
242 Every Boulder::Stream has several state variables that you can adjust.
243 Fix them in this fashion:
244  
245 $a = new Boulder::Stream;
246 $a->{delim}=':';
247 $a->{record_start}='[';
248 $a->{record_end}=']';
249 $a->{passthru}=undef;
250  
251 =over 4
252  
253 =item * delim
254  
255 This is the delimiter character between tags and values, "=" by default.
256  
257 =item * record_start
258  
259 This is the start of nested record character, "{" by default.
260  
261 =item * record_end
262  
263 This is the end of nested record character, "}" by default.
264  
265 =item * passthru
266  
267 This determines whether unrecognized tags should be passed through
268 from the input stream to the output stream. This is 'true' by
269 default. Set it to undef to override this behavior.
270  
271 =back
272  
273 =head1 BUGS
274  
275 Because the delim, record_start and record_end characters in the
276 B<Boulder::Stream> object are used in optimized (once-compiled)
277 pattern matching, you cannot change these values once get() has once
278 been called. To change the defaults, you must create the
279 Boulder::Stream, set the characters, and only then begin reading from
280 the input stream. For the same reason, different Boulder::Stream
281 objects cannot use different delimiters.
282  
283 =head1 AUTHOR
284  
285 Lincoln D. Stein <lstein@cshl.org>, Cold Spring Harbor Laboratory,
286 Cold Spring Harbor, NY. This module can be used and distributed on
287 the same terms as Perl itself.
288  
289 =head1 SEE ALSO
290  
291 L<Boulder>,
292 L<Boulder::Blast>, L<Boulder::Genbank>, L<Boulder::Medline>, L<Boulder::Unigene>,
293 L<Boulder::Omim>, L<Boulder::SwissProt>
294  
295 =cut
296  
297 2 require 5.004;
298 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
299 2 ▶use Stone;
Subroutine BEGIN: called CC=1 SCAR=0.0
300 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
301 2 ▶use Symbol();
Subroutine BEGIN: called CC=1 SCAR=0.0
302  
303 2 ▶use vars '$VERSION';
Subroutine BEGIN: called CC=1 SCAR=0.0
304 2 $VERSION=1.07;
305  
306 # Pseudonyms and deprecated methods.
307 2 *get = \&read_record;
308 2 *next = \&read_record;
309 2 *put = \&write_record;
310  
311 # Call this with IN and OUT filehandles of your choice.
312 # If none specified, defaults to <>/STDOUT.
313 2 ▶sub new {
Subroutine new: called CC=3 SCAR=11.2
Pod documented
314 2 my $package = shift;
315 2 my ($in,$out) = rearrange(['IN','OUT'],@_);
316  
317 2 ▶ $in = $package->to_fh($in) || \*main::ARGV;
Condition50%1/2
$package->to_fh($in) || \*ARGV
l!l
20
MC/DC0%0/1
$package->to_fh($in) || \*ARGV
$package->to_fh($in)
Truth table50%1/2
A
A= $package->to_fh($in)
Aresult
11
00
318 2 ▶ $out = $package->to_fh($out,1) || \*main::STDOUT;
Condition50%1/2
$package->to_fh($out, 1) || \*STDOUT
l!l
20
MC/DC0%0/1
$package->to_fh($out, 1) || \*STDOUT
$package->to_fh($out, 1)
Truth table50%1/2
A
A= $package->to_fh($out, 1)
Aresult
11
00
319 2 my $pack = caller;
320  
321 2 return bless {
322 'IN'=>$in,
323 'OUT'=>$out,
324 'delim'=>'=',
325 'record_stop'=>"=\n",
326 'line_end'=>"\n",
327 'subrec_start'=>"\{",
328 'subrec_end'=>"\}",
329 'binary'=>'true',
330 'passthru'=>'true'
331 },$package;
332 }
333  
334 # You are free to redefine the following magic variables:
335 # $a = new Boulder::Stream;
336 # $a->{delim} separates tag = value ['=']
337 # $a->{line_end} separates tag=value pairs [ newline ]
338 # $a->{record_stop} ends records ["=\n"]
339 # $a->{subrec_start} begins a nested record [ "{" ]
340 # $a->{subrec_end} ends a nested record [ "}" ]
341 # $a->{passthru} if true, passes unread tags -> output [ 'true' ]
342 # $a->{binary} if true, escapes and unescapes records [ 'true' ]
343  
344 # Since escaping/unescaping has some overhead, you might want to undef
345 # 'binary' in order to improve performance.
346  
347 # Read in and return a Rolling Stone record. Will return
348 # undef() when an empty record is hit. You can specify
349 # keys that you are interested in getting, as in the
350 # original boulder package.
351 5 ▶sub read_one_record {
Subroutine read_one_record: called CC=21 SCAR=36.4
Pod undocumented
352 5 my($self,@keywords) = @_;
353  
354 5 ▶ return if $self->done;
Branch: 1 branch
BranchTrueFalseTotal
if $self->done 0 5 5
355  
356 5 my(%interested,$key,$value);
357 5 grep($interested{$_}++,@keywords);
358  
359 5 my $out=$self->{OUT};
360 5 my $delim=$self->{'delim'};
361 5 my $subrec_start=$self->{'subrec_start'};
362 5 my $subrec_end=$self->{'subrec_end'};
363 5 my ($pebble,$found);
364  
365 # This is a small hack to ensure that we respect the
366 # record delimiters even when we don't make an
367 # intervening record write.
368 5 ▶ if (!$self->{WRITE} && $self->{INVOKED} && !$self->{LEVEL}
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{'WRITE'} and $self->{'INVOKED'} and not $self->{'LEVEL'} and $self->{'passthru'} and $self->{'PASSED'}) { } 0 5 5
Condition75%9/12
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"} and $self->{"PASSED"}
!ll&&!rl&&r
410
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"}
!ll&&!rl&&r
401
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"}
!ll&&!rl&&r
131
not $self->{"WRITE"} and $self->{"INVOKED"}
!ll&&!rl&&r
014
MC/DC0%0/5
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"} and $self->{"PASSED"}
not $self->{"WRITE"} $self->{"INVOKED"} not $self->{"LEVEL"} $self->{"passthru"} $self->{"PASSED"}
Truth table50%3/6
not(A) and B and not(C) and D and E
A= not $self->{"WRITE"}B= $self->{"INVOKED"}C= not $self->{"LEVEL"}D= $self->{"passthru"}E= $self->{"PASSED"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
369 && $self->{'passthru'} && $self->{PASSED}) {
370 0 print $out ($self->{'record_stop'});
371 } else {
372 5 $self->{INVOKED}++; # keep track of our invocations
373 }
374  
375 5 undef $self->{WRITE};
376 5 undef $self->{PASSED};
377  
378 5 my $stone = new Stone();
379  
380 5 while (1) {
381  
382 21 ▶ last unless $_ = $self->next_pair;
Branch: 1 branch
BranchTrueFalseTotal
unless $_ = $self->next_pair 2 19 21
383  
384 19 ▶ if (/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^#/) 0 19 19
385 0 ▶ print $out ("$_$self->{line_end}") if $self->{'passthru'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"passthru"} 0 0 0
386 0 next;
387 }
388  
389 19 ▶ if (/^\s*$delim/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^\s*$delim/o) 0 19 19
390 0 undef $self->{LEVEL};
391 0 last;
392 }
393  
394 19 ▶ if (/$subrec_end$/o) {
Branch: 1 branch
BranchTrueFalseTotal
if (/$subrec_end$/o) 3 16 19
395 3 ▶ $self->{LEVEL}--,last if $self->{LEVEL};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"LEVEL"} 3 0 3
396 0 ▶ print $out ("$_$self->{line_end}") if $self->{'passthru'};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"passthru"} 0 0 0
397 0 next;
398 }
399  
400 16 ▶ next unless ($key,$value) = /^\s*(.+?)\s*$delim\s*(.*)/o;
Branch: 1 branch
BranchTrueFalseTotal
unless () = /^\s*(.+?)\s*$delim\s*(.*)/o 0 16 16
401  
402 16 ▶ if ((!@keywords) || $interested{$key}) {
Branch: 2 branches
BranchTrueFalseTotal
if (not @keywords or $interested{$key}) { } 16 0 16
elsif ($self->{'passthru'}) { } 0 0 0
Condition33%1/3
not @keywords or $interested{$key}
l!l&&r!l&&!r
1600
MC/DC0%0/2
not @keywords or $interested{$key}
not @keywords $interested{$key}
Truth table33%1/3
not(A) or B
A= not @keywordsB= $interested{$key}
ABresult
1X1
011
000
403  
404 16 $found++;
405 16 ▶ if ($value=~/^\s*$subrec_start/o) {
Branch: 1 branch
BranchTrueFalseTotal
if ($value =~ /^\s*$subrec_start/o) 3 13 16
406 3 $self->{LEVEL}++;
407 3 $pebble = read_one_record($self); # call ourselves recursively
408 3 ▶ $pebble = new Stone() unless defined($pebble); # an empty record is still valid
Branch: 1 branch
BranchTrueFalseTotal
unless defined $pebble 0 3 3
409 3 $stone->insert($self->unescapekey($key)=>$pebble);
410 3 next;
411 }
412  
413 13 $stone->insert($self->unescapekey($key)=>$self->unescapeval($value));
414  
415 } elsif ($self->{'passthru'}) {
416 0 print $out ("$_$self->{line_end}");
417 0 $self->{PASSED}++; # flag that we will need to write a record delimiter
418 }
419 }
420
421 5 ▶ return undef unless $found;
Branch: 1 branch
BranchTrueFalseTotal
unless $found 0 5 5
422 5 return $stone;
423 }
424  
425 # Write out the specified Stone record.
426 2 ▶sub write_record {
Subroutine write_record: called CC=6 SCAR=17.9
Pod documented
427 2 my($self,@stone)=@_;
428 2 for my $stone (@stone) {
429 2 $self->{'WRITE'}++;
430 2 my $out=$self->{OUT};
431  
432 # Write out a Stone record in boulder format.
433 2 my ($key,$value,@value);
434 2 foreach $key ($stone->tags) {
435 7 @value = $stone->get($key);
436 7 $key = $self->escapekey($key);
437 7 foreach $value (@value) {
438 7 ▶ next unless ref $value;
Branch: 1 branch
BranchTrueFalseTotal
unless ref $value 0 7 7
439 7 ▶ if (exists $value->{'.name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{'.name'}) { } 4 3 7
440 4 $value = $self->escapeval($value);
441 4 print $out ("$key$self->{delim}$value\n");
442 } else {
443 3 print $out ("$key$self->{delim}$self->{subrec_start}\n");
444 3 _write_nested($self,1,$value);
445 }
446 }
447 }
448 2 print $out ("$self->{delim}\n");
449 }
450 2 1;
451 }
452  
453 # read_record() returns one stone if called in a scalar
454 # context and all the stones if called in an array
455 # context.
456 3 ▶sub read_record {
Subroutine read_record: called CC=6 SCAR=23.5
Pod documented
457 3 my($self,@tags) = @_;
458 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) { } 0 3 3
459 0 my(@result,$s);
460 0 ▶ while (!$self->done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->done) 0 0 0
461 0 $s = $self->read_one_record(@tags);
462 0 ▶ push(@result,$s) if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 0 0 0
463 }
464 0 return @result;
465 } else {
466 3 my $s;
467 3 ▶ while (!$self->done) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->done) 2 1 3
468 2 $s = $self->read_one_record(@tags);
469 2 ▶ return $s if $s;
Branch: 1 branch
BranchTrueFalseTotal
if $s 2 0 2
470 }
471 1 return undef;
472 }
473 }
474  
475 # ----------------------------------------------------------------
476 # TIED INTERFACE METHODS
477 # ----------------------------------------------------------------
478  
479 # newFh() is a class method that returns a tied filehandle
480 #
481 0 ▶sub newFh {
Subroutine newFh: not called CC=2 SCAR=17.9
Pod documented
482 0 my $class = shift;
483 0 ▶ return unless my $self = $class->new(@_);
Branch: 1 branch
BranchTrueFalseTotal
unless my $self = $class->new(@_) 0 0 0
484 0 return $self->fh;
485 }
486  
487 # fh() returns a filehandle that you can read stones from
488 0 ▶sub fh {
Subroutine fh: not called CC=2 SCAR=17.9
Pod undocumented
489 0 my $self = shift;
490 0 ▶ my $class = ref($self) || $self;
Condition0%0/3
ref $self || $self
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table0%0/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
491 0 my $s = Symbol::gensym;
492 0 tie $$s,$class,$self;
493 0 return $s;
494 }
495  
496 0 ▶sub TIEHANDLE {
Subroutine TIEHANDLE: not called CC=1 SCAR=6.9
497 0 my $class = shift;
498 0 return bless {stream => shift},$class;
499 }
500  
501 0 ▶sub READLINE {
Subroutine READLINE: not called CC=1 SCAR=6.9
502 0 my $self = shift;
503 0 return $self->{stream}->read_record();
504 }
505  
506 0 ▶sub PRINT {
Subroutine PRINT: not called CC=1 SCAR=6.9
507 0 my $self = shift;
508 0 $self->{stream}->write_record(@_);
509 }
510  
511 #--------------------------------------
512 # Internal (private) procedures.
513 #--------------------------------------
514 # This finds an array of key/value pairs and
515 # stashes it where we can find it.
516 2 ▶sub read_next_rec {
Subroutine read_next_rec: called CC=6 SCAR=19.2
Pod undocumented
517 2 my($self) = @_;
518 2 my($olddelim) = $/;
519  
520 2 $/="\n".$self->{record_stop};
521 2 my($in) = $self->{IN};
522  
523 2 my $data = <$in>;
524 2 ▶ chomp($data) if defined $data;
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 2 0 2
525  
526 2 ▶ if ($in !~ /ARGV/) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $in =~ /ARGV/) { } 2 0 2
527 2 ▶ $self->{EOF}++ if eof($in);
Branch: 1 branch
BranchTrueFalseTotal
if eof $in 1 1 2
528 } else {
529 0 ▶ $self->{EOF}++ if eof();
Branch: 1 branch
BranchTrueFalseTotal
if eof() 0 0 0
530 }
531  
532 2 $/=$olddelim;
533 2 ▶ $self->{PAIRS}=[grep($_,split($self->{'line_end'},$data))]
Branch: 1 branch
BranchTrueFalseTotal
if defined $data 2 0 2
534 if defined $data;
535 }
536  
537 # This returns TRUE when we've reached the end
538 # of the input stream
539 8 ▶sub done {
Subroutine done: called CC=3 SCAR=11.0
Pod undocumented
540 8 my $self = shift;
541 8 ▶ return if defined $self->{PAIRS} && @{$self->{PAIRS}};
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"PAIRS"} and @{$self->{"PAIRS"};} 3 5 8
Condition66%2/3
defined $self->{"PAIRS"} and @{$self->{"PAIRS"};}
!ll&&!rl&&r
503
MC/DC50%1/2
defined $self->{"PAIRS"} and @{$self->{"PAIRS"};}
defined $self->{"PAIRS"} @{$self->{"PAIRS"};}
Truth table66%2/3
A and B
A= defined $self->{"PAIRS"}B= @{$self->{"PAIRS"};}
ABresult
0X0
100
111
542 5 return $self->{EOF};
543 }
544  
545 # This returns the next key/value pair.
546 21 ▶sub next_pair {
Subroutine next_pair: called CC=4 SCAR=13.9
Pod undocumented
547 21 my $self = shift;
548 21 ▶ $self->read_next_rec unless $self->{PAIRS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PAIRS"} 2 19 21
549 21 ▶ return unless $self->{PAIRS};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"PAIRS"} 0 21 21
550 21 ▶ return shift @{$self->{PAIRS}} if @{$self->{PAIRS}};
Branch: 1 branch
BranchTrueFalseTotal
if @{$self->{"PAIRS"};} 19 2 21
551 2 undef $self->{PAIRS};
552 2 return undef;
553 }
554  
555 3 ▶sub _write_nested {
Subroutine _write_nested: called CC=4 SCAR=14.1
556 3 my($self,$level,$stone) = @_;
557 3 my $indent = ' ' x $level;
558 3 my($key,$value,@value);
559 3 my $out = $self->{OUT};
560  
561 3 foreach $key ($stone->tags) {
562 8 @value = $stone->get($key);
563 8 $key = $self->escapekey($key);
564 8 foreach $value (@value) {
565 9 ▶ if (exists $value->{'.name'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (exists $value->{'.name'}) { } 9 0 9
566 9 $value = $self->escapeval($value);
567 9 print $out ($indent,"$key$self->{delim}$value\n");
568 } else {
569 0 print $out ($indent,"$key$self->{delim}$self->{subrec_start}\n");
570 0 _write_nested($self,$level+1,$value);
571 }
572 }
573 }
574  
575 3 print $out (' ' x ($level-1),$self->{'subrec_end'},"\n");
576 }
577  
578 # Escape special characters.
579 15 ▶sub escapekey {
Subroutine escapekey: called CC=2 SCAR=7.2
Pod undocumented
580 15 my($s,$toencode)=@_;
581 15 ▶ return $toencode unless $s->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"binary"} 0 15 15
582 15 my $specials=" $s->{delim}$s->{subrec_start}$s->{subrec_end}$s->{line_end}$s->{record_stop}%";
583 0 $toencode=~s/([$specials])/uc sprintf("%%%02x",ord($1))/oge;
584 15 return $toencode;
585 }
586  
587 13 ▶sub escapeval {
Subroutine escapeval: called CC=2 SCAR=7.2
Pod undocumented
588 13 my($s,$toencode)=@_;
589 13 ▶ return $toencode unless $s->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"binary"} 0 13 13
590 13 my $specials="$s->{delim}$s->{subrec_start}$s->{subrec_end}$s->{line_end}$s->{record_stop}%";
591 0 $toencode=~s/([$specials])/uc sprintf("%%%02x",ord($1))/oge;
592 13 return $toencode;
593 }
594  
595 # Unescape special characters
596 16 ▶sub unescapekey {
Subroutine unescapekey: called CC=1 SCAR=0.0
Pod undocumented
597 16 unescape(@_);
598 }
599  
600 13 ▶sub unescapeval {
Subroutine unescapeval: called CC=1 SCAR=0.0
Pod undocumented
601 13 unescape(@_);
602 }
603  
604 # Unescape special characters
605 29 ▶sub unescape {
Subroutine unescape: called CC=2 SCAR=7.4
Pod undocumented
606 29 my($s,$todecode)=@_;
607 29 ▶ return $todecode unless $s->{binary};
Branch: 1 branch
BranchTrueFalseTotal
unless $s->{"binary"} 0 29 29
608 0 $todecode =~ s/%([0-9a-fA-F]{2})/pack("c",hex($1))/ge;
609 29 return $todecode;
610 }
611  
612 # utility routine to turn type globs, barewords, IO::File structs, etc into
613 # filehandles.
614 4 ▶sub to_fh {
Subroutine to_fh: called CC=7 SCAR=34.2
Pod undocumented
615 4 my ($pack,$thingy,$write) = @_;
616 4 ▶ return unless $thingy;
Branch: 1 branch
BranchTrueFalseTotal
unless $thingy 0 4 4
617 4 ▶ return $thingy if defined fileno($thingy);
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $thingy 4 0 4
618  
619 0 my $caller;
620 0 ▶ while (my $package = caller(++$caller)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $package = caller ++$caller) 0 0 0
621 0 my $qualified_thingy = Symbol::qualify_to_ref($thingy,$package);
622 0 ▶ return $qualified_thingy if defined fileno($qualified_thingy);
Branch: 1 branch
BranchTrueFalseTotal
if defined fileno $qualified_thingy 0 0 0
623 }
624
625 # otherwise try to open it as a file
626 0 my $fh = Symbol::gensym();
627 0 ▶ $thingy = ">$thingy" if $write;
Branch: 1 branch
BranchTrueFalseTotal
if $write 0 0 0
628 0 ▶ open ($fh,$thingy) || croak "$pack open of $thingy: $!";
Branch: 1 branch
BranchTrueFalseTotal
open $fh, $thingy or croak("$pack open of ${thingy}: $!") 0 0 0
629 0 return \*$fh;
630 }
631  
632 1 ▶sub DESTROY {
Subroutine DESTROY: called CC=6 SCAR=24.3
633 1 my $self = shift;
634 1 my $out=$self->{OUT};
635 print $out ($self->{'delim'},"\n")
636 1 ▶ if !$self->{WRITE} && $self->{INVOKED} && !$self->{LEVEL} && $self->{'passthru'} && $self->{PASSED};
Branch: 1 branch
BranchTrueFalseTotal
if not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"} and $self->{"PASSED"} 0 1 1
Condition33%4/12
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"} and $self->{"PASSED"}
!ll&&!rl&&r
100
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"}
!ll&&!rl&&r
100
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"}
!ll&&!rl&&r
100
not $self->{"WRITE"} and $self->{"INVOKED"}
!ll&&!rl&&r
100
MC/DC0%0/5
not $self->{"WRITE"} and $self->{"INVOKED"} and not $self->{"LEVEL"} and $self->{"passthru"} and $self->{"PASSED"}
not $self->{"WRITE"} $self->{"INVOKED"} not $self->{"LEVEL"} $self->{"passthru"} $self->{"PASSED"}
Truth table16%1/6
not(A) and B and not(C) and D and E
A= not $self->{"WRITE"}B= $self->{"INVOKED"}C= not $self->{"LEVEL"}D= $self->{"passthru"}E= $self->{"PASSED"}
ABCDEresult
0XXXX0
10XXX0
110XX0
1110X0
111100
111111
637 }
638  
639  
640 #####################################################################
641 ###################### private routines #############################
642 2 ▶sub rearrange {
Subroutine rearrange: called CC=11 SCAR=42.8
Pod undocumented
643 2 my($order,@param) = @_;
644 2 ▶ return unless @param;
Branch: 1 branch
BranchTrueFalseTotal
unless @param 0 2 2
645 2 my %param;
646  
647 2 ▶ if (ref $param[0] eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param[0] eq 'HASH') { } 0 2 2
648 0 %param = %{$param[0]};
649 } else {
650 2 ▶ return @param unless (defined($param[0]) && substr($param[0],0,1) eq '-');
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param[0] and substr($param[0], 0, 1) eq "-" 2 0 2
Condition33%1/3
defined $param[0] and substr($param[0], 0, 1) eq "-"
!ll&&!rl&&r
020
MC/DC0%0/2
defined $param[0] and substr($param[0], 0, 1) eq "-"
defined $param[0] substr($param[0], 0, 1) eq "-"
Truth table33%1/3
A and B
A= defined $param[0]B= substr($param[0], 0, 1) eq "-"
ABresult
0X0
100
111
651  
652 0 my $i;
653 0 ▶ for ($i=0;$i<@param;$i+=2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @param) 0 0 0
654 0 $param[$i]=~s/^\-//; # get rid of initial - if present
655 0 $param[$i]=~tr/a-z/A-Z/; # parameters are upper case
656 }
657  
658 0 %param = @param; # convert into associative array
659 }
660
661 0 my(@return_array);
662
663 0 local($^W) = 0;
664 0 my($key)='';
665 0 foreach $key (@$order) {
666 0 my($value);
667 0 ▶ if (ref($key) eq 'ARRAY') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $key eq 'ARRAY') { } 0 0 0
668 0 foreach (@$key) {
669 0 ▶ last if defined($value);
Branch: 1 branch
BranchTrueFalseTotal
if defined $value 0 0 0
670 0 $value = $param{$_};
671 0 delete $param{$_};
672 }
673 } else {
674 0 $value = $param{$key};
675 0 delete $param{$key};
676 }
677 0 push(@return_array,$value);
678 }
679 0 ▶ push (@return_array,{%param}) if %param;
Branch: 1 branch
BranchTrueFalseTotal
if %param 0 0 0
680 0 return @return_array;
681 }
682  
683 2 1;
684  
« Boulder/Blast.pm ↑ Summary Boulder/Labbase.pm »