lib/Net/HL7/Message.pm

filter: Statementstmt 96.1% 151 / 157 Branchbran 88.7% 55 / 62 Conditioncond 88.8% 8 / 9 MC/DCmcdc 83.3% 5 / 6 Subroutinesub 95.0% 19 / 20 Podpod 100% 12 / 12 totaltotal 93.9% 250 / 266 CC 38 SCAR 17.6
CC
38
Cov
94%
_init
14.0
insertSegment
5.2
setSegment
5.0
CRAP 5.8
« blib/lib/Net/HL7/Daemon.pm ↑ Summary lib/Net/HL7/Messages/ACK.pm »
1 ################################################################################
2 #
3 # File : Message.pm
4 # Author : Duco Dokter
5 # Created : Mon Nov 11 17:37:11 2002
6 # Version : $Id: Message.pm,v 1.21 2015/01/29 15:30:11 wyldebeast Exp $
7 # Copyright : D.A.Dokter, Wyldebeast & Wunderliebe
8 #
9 ################################################################################
10  
11 package Net::HL7::Message;
12  
13 7 ▶use 5.004;
Subroutine BEGIN: called CC=1 SCAR=0.0
14 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
15 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 7 ▶use Net::HL7::Segment;
Subroutine BEGIN: called CC=1 SCAR=0.0
17 7 ▶use Net::HL7;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 =pod
20  
21 =head1 NAME
22  
23 Net::HL7::Message
24  
25 =head1 SYNOPSIS
26  
27 my $request = new Net::HL7::Request();
28 my $conn = new Net::HL7::Connection('localhost', 8089);
29  
30 my $msh = new Net::HL7::Segments::MSH();
31  
32 my $seg1 = new Net::HL7::Segment("PID");
33  
34 $seg1->setField(1, "foo");
35  
36 $request->addSegment($msh);
37 $request->addSegment($seg1);
38  
39 my $response = $conn->send($request);
40  
41  
42 =head1 DESCRIPTION
43  
44 In general one needn't create an instance of the Net::HL7::Message
45 class directly, but use the L<Net::HL7::Request|Net::HL7::Request>
46 class. When adding segments, note that the segment index starts at 0,
47 so to get the first segment, segment, do
48 C<$msg-E<gt>getSegmentByIndex(0)>.
49  
50 The segment separator defaults to \015. To change this, set the
51 variable $Net::HL7::SEGMENT_SEPARATOR.
52  
53  
54 =head1 METHODS
55  
56 =over 4
57  
58 =item B<$msg = new Net::HL7::Message([$msg])>
59  
60 The constructor takes an optional string argument that is a string
61 representation of a HL7 message. If the string representation is not a
62 valid HL7 message. according to the specifications, undef is returned
63 instead of a new instance. This means that segments should be
64 separated within the message with the segment separator (defaults to
65 \015) or a newline, and segments should be syntactically correct.
66 When using the string argument constructor, make sure that you have
67 escaped any characters that would have special meaning in Perl. For
68 instance (using a different subcomponent separator):
69  
70 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x@y@z^z\r");>
71  
72 would actually mean
73  
74 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x^z\r");>
75  
76 since '@y@z' would be interpreted as two empty arrays, so do:
77  
78 C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x\@y\@z^z\r");>
79  
80 instead.
81  
82 The control characters and field separator will take the values from
83 the MSH segment, if set. Otherwise defaults will be used. Changing the
84 MSH fields specifying the field separator and control characters after
85 the MSH has been added to the message will result in setting these
86 values for the message.
87  
88 If the message couldn't be created, for example due to a erroneous HL7
89 message string, undef is returned.
90  
91 =cut
92  
93 sub new {
94
95 23 ▶ my $class = shift;
Subroutine new: called CC=2 SCAR=6.9
Pod documented
96 23 bless my $self = {}, $class;
97
98 23 ▶ $self->_init(@_) || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$self->_init(@_) or return undef 2 21 23
99
100 21 return $self;
101 }
102  
103  
104 sub _init {
105  
106 23 ▶ my ($self, $hl7str) = @_;
Subroutine _init: called CC=14 SCAR=26.4
107  
108 # Array holding the segments
109 #
110 23 $self->{SEGMENTS} = [];
111  
112 # Control characters and other HL7 properties
113 #
114 23 $self->{SEGMENT_SEPARATOR} = $Net::HL7::SEGMENT_SEPARATOR;
115 23 $self->{FIELD_SEPARATOR} = $Net::HL7::FIELD_SEPARATOR;
116 23 $self->{COMPONENT_SEPARATOR} = $Net::HL7::COMPONENT_SEPARATOR;
117 23 $self->{SUBCOMPONENT_SEPARATOR} = $Net::HL7::SUBCOMPONENT_SEPARATOR;
118 23 $self->{REPETITION_SEPARATOR} = $Net::HL7::REPETITION_SEPARATOR;
119 23 $self->{ESCAPE_CHARACTER} = $Net::HL7::ESCAPE_CHARACTER;
120 23 $self->{HL7_VERSION} = $Net::HL7::HL7_VERSION;
121  
122 # If an HL7 string is given to the constructor, parse it.
123 23 ▶ if ($hl7str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($hl7str) 11 12 23
124  
125 11 my @segments = split("[\n\\" . $self->{SEGMENT_SEPARATOR} . "]", $hl7str);
126  
127 # the first segment should be the control segment
128 #
129 11 $segments[0] =~ /^([A-Z0-9]{3})(.)(.)(.)(.)(.)(.)/;
130  
131 11 my ($hdr, $fldSep, $compSep, $repSep, $esc, $subCompSep, $fldSepCtrl) =
132 ($1, $2, $3, $4, $5, $6, $7);
133  
134 # Check whether field separator is repeated after 4 control characters
135  
136 11 ▶ if ($fldSep ne $fldSepCtrl) {
Branch: 1 branch
BranchTrueFalseTotal
if ($fldSep ne $fldSepCtrl) 1 10 11
137  
138 1 return undef;
139 }
140  
141 # Set field separator based on control segment
142 10 $self->{FIELD_SEPARATOR} = $fldSep;
143
144 # Set other separators
145 10 $self->{COMPONENT_SEPARATOR} = $compSep;
146 10 $self->{SUBCOMPONENT_SEPARATOR} = $subCompSep;
147 10 $self->{ESCAPE_CHARACTER} = $esc;
148 10 $self->{REPETITION_SEPARATOR} = $repSep;
149
150 # Do all segments
151 #
152 10 ▶ for (my $i = 0; $i < @segments; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @segments) 19 9 28
153
154 19 my @fields = split('\\' . $self->{FIELD_SEPARATOR}, $segments[$i]);
155  
156 19 my $name = shift(@fields);
157  
158 # Now decompose fields if necessary, into refs to arrays
159 #
160 19 ▶ for (my $j = 0; $j < @fields; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < @fields) 82 19 101
161  
162 # Skip control field
163 82 ▶ if ($i == 0 && $j == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i == 0 and $j == 0) 10 72 82
Condition100%3/3
$i == 0 and $j == 0
!ll&&!rl&&r
264610
MC/DC100%2/2
$i == 0 and $j == 0
$i == 0 $j == 0
Truth table100%3/3
A and B
A= $i == 0B= $j == 0
ABresult
0X0
100
111
164
165 10 next;
166 }
167
168 72 my @comps = split('\\' . $self->{COMPONENT_SEPARATOR}, $fields[$j]);
169
170 72 ▶ for (my $k = 0; $k < @comps; $k++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k < @comps) 50 72 122
171  
172 50 my @subComps = split('\\' . $self->{SUBCOMPONENT_SEPARATOR}, $comps[$k]);
173
174 # Make it a ref or just the value
175 50 ▶ if (@subComps <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@subComps <= 1) { } 47 3 50
176 47 $comps[$k] = $subComps[0];
177 }
178 else {
179 3 $comps[$k] = \@subComps;
180 }
181  
182 }
183  
184 72 ▶ if (@comps <= 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@comps <= 1) { } 66 6 72
185 66 $fields[$j] = $comps[0];
186 }
187 else {
188 6 $fields[$j] = \@comps;
189 }
190 }
191  
192 19 my $seg;
193  
194 # untaint
195 19 my $segClass = "";
196  
197 19 ▶ if ($name =~ /^[A-Z][A-Z0-9]{2}$/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($name =~ /^[A-Z][A-Z0-9]{2}$/) 18 1 19
198 18 $segClass = "Net::HL7::Segments::$name";
199 18 $segClass =~ /^(.*)$/;
200 18 $segClass = $1;
201 }
202  
203 # Let's see whether it's a special segment
204 #
205 19 ▶ if ( $segClass && eval("require $segClass;") ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segClass and eval "require $segClass;") { } 10 9 19
Condition100%3/3
$segClass and eval "require $segClass;"
!ll&&!rl&&r
1810
MC/DC100%2/2
$segClass and eval "require $segClass;"
$segClass eval "require $segClass;"
Truth table100%3/3
A and B
A= $segClassB= eval "require $segClass;"
ABresult
0X0
100
111
206 10 unshift(@fields, $self->{FIELD_SEPARATOR});
207 10 $seg = eval{ "$segClass"->new(\@fields); };
208 }
209 else {
210 9 $seg = new Net::HL7::Segment($name, \@fields);
211 }
212
213 19 ▶ $seg || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$seg or return undef 1 18 19
214  
215 18 $self->addSegment($seg);
216 }
217 }
218  
219 21 return 1;
220 }
221  
222  
223 =pod
224  
225 =item B<addSegment($segment)>
226  
227 Add the segment. to the end of the message. The segment should be an
228 instance of L<Net::HL7::Segment|Net::HL7::Segment>.
229  
230 =cut
231  
232 sub addSegment {
233  
234 42 ▶ my ($self, $segment) = @_;
Subroutine addSegment: called CC=2 SCAR=6.9
Pod documented
235  
236 42 ▶ if (@{ $self->{SEGMENTS} } == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$self->{"SEGMENTS"};} == 0) 22 20 42
237 22 $self->_resetCtrl($segment);
238 }
239  
240 42 push( @{ $self->{SEGMENTS} }, $segment);
241 }
242  
243  
244 =pod
245  
246 =item B<insertSegment($segment, $idx)>
247  
248 Insert the segment. The segment should be an instance of
249 L<Net::HL7::Segment|Net::HL7::Segment>. If the index is not given,
250 nothing happens.
251  
252 =cut
253  
254 sub insertSegment {
255  
256 4 ▶ my ($self, $segment, $idx) = @_;
Subroutine insertSegment: called CC=5 SCAR=16.5
Pod documented
257  
258 4 ▶ (! defined $idx) && return;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx or return 0 4 4
259 4 ▶ ($idx > @{ $self->{SEGMENTS} }) && return;
Branch: 1 branch
BranchTrueFalseTotal
$idx > @{$self->{"SEGMENTS"};} and return 1 3 4
260  
261 3 ▶ if ($idx == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ($idx == 0) { } 0 3 3
elsif ($idx == @{$self->{'SEGMENTS'};}) { } 1 2 3
262  
263 0 $self->_resetCtrl($segment);
264 0 unshift(@{ $self->{SEGMENTS} }, $segment);
265 }
266 3 elsif ($idx == @{ $self->{SEGMENTS} }) {
267  
268 1 push(@{ $self->{SEGMENTS} }, $segment);
269 }
270 else {
271 2 @{ $self->{SEGMENTS} } =
272 2 (@{ $self->{SEGMENTS} }[0..$idx-1],
273 $segment,
274 2 @{ $self->{SEGMENTS} }[$idx..@{ $self->{SEGMENTS} } -1]
275 );
276 }
277 }
278  
279  
280 =pod
281  
282 =item B<getSegmentByIndex($index)>
283  
284 Return the segment specified by $index. Segment count within the
285 message starts at 0.
286  
287 =cut
288  
289 sub getSegmentByIndex {
290  
291 150 ▶ my ($self, $index) = @_;
Subroutine getSegmentByIndex: called CC=1 SCAR=0.0
Pod documented
292  
293 150 return $self->{SEGMENTS}->[$index];
294 }
295  
296  
297 =pod
298  
299 =item B<getSegmentsByName($name)>
300  
301 Return an array of all segments with the given name
302  
303 =cut
304  
305 sub getSegmentsByName {
306  
307 2 ▶ my ($self, $name) = @_;
Subroutine getSegmentsByName: called CC=3 SCAR=11.0
Pod documented
308  
309 2 my @segments = ();
310  
311 2 foreach (@{ $self->{SEGMENTS} }) {
312 7 ▶ ($_->getName() eq $name) && push(@segments, $_);
Branch: 1 branch
BranchTrueFalseTotal
$_->getName eq $name and push @segments, $_ 3 4 7
313 }
314  
315 2 return @segments;
316 }
317  
318  
319 =pod
320  
321 =item B<removeSegmentByIndex($index)>
322  
323 Remove the segment indexed by $index. If it doesn't exist, nothing
324 happens, if it does, all segments after this one will be moved one
325 index up.
326  
327 =cut
328  
329 sub removeSegmentByIndex {
330  
331 4 ▶ my ($self, $index) = @_;
Subroutine removeSegmentByIndex: called CC=2 SCAR=6.9
Pod documented
332  
333 4 ▶ ($index < @{ $self->{SEGMENTS} }) && splice( @{ $self->{SEGMENTS} }, $index, 1);
Branch: 1 branch
BranchTrueFalseTotal
$index < @{$self->{"SEGMENTS"};} and splice @{$$self{"SEGMENTS"};}, $index, 1 3 1 4
334 }
335  
336  
337 =pod
338  
339 =item B<setSegment($seg, $index)>
340  
341 Set the segment on index. If index is out of range, or not provided,
342 do nothing. Setting MSH on index 0 will revalidate field separator,
343 control characters and hl7 version, based on MSH(1), MSH(2) and
344 MSH(12).
345  
346 =cut
347  
348 sub setSegment {
349  
350 3 ▶ my ($self, $segment, $idx) = @_;
Subroutine setSegment: called CC=5 SCAR=16.2
Pod documented
351  
352 3 ▶ (! defined $idx) && return;
Branch: 1 branch
BranchTrueFalseTotal
defined $idx or return 1 2 3
353 2 ▶ ($idx > @{ $self->{SEGMENTS} }) && return;
Branch: 1 branch
BranchTrueFalseTotal
$idx > @{$self->{"SEGMENTS"};} and return 0 2 2
354  
355 2 ▶ if ($segment->getName() eq "MSH" && $idx == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->getName eq "MSH" and $idx == 0) 1 1 2
Condition66%2/3
$segment->getName eq "MSH" and $idx == 0
!ll&&!rl&&r
101
MC/DC50%1/2
$segment->getName eq "MSH" and $idx == 0
$segment->getName eq "MSH" $idx == 0
Truth table66%2/3
A and B
A= $segment->getName eq "MSH"B= $idx == 0
ABresult
0X0
100
111
356  
357 1 $self->_resetCtrl($segment);
358 }
359
360 2 @{ $self->{SEGMENTS} }[$idx] = $segment;
361 }
362  
363  
364 # After change of MSH, reset control fields
365 #
366 sub _resetCtrl {
367  
368 33 ▶ my ($self, $segment) = @_;
Subroutine _resetCtrl: called CC=4 SCAR=13.9
369  
370 33 ▶ if ($segment->getField(1)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->getField(1)) 33 0 33
371 33 $self->{FIELD_SEPARATOR} = $segment->getField(1);
372 }
373
374 33 ▶ if ($segment->getField(2) =~ /(.)(.)(.)(.)/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->getField(2) =~ /(.)(.)(.)(.)/) 33 0 33
375
376 33 $self->{COMPONENT_SEPARATOR} = $1;
377 33 $self->{REPETITION_SEPARATOR} = $2;
378 33 $self->{ESCAPE_CHARACTER} = $3;
379 33 $self->{SUBCOMPONENT_SEPARATOR} = $4;
380 }
381
382 33 ▶ if ($segment->getField(12)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($segment->getField(12)) 17 16 33
383 17 $self->{HL7_VERSION} = $segment->getField(12);
384 }
385 }
386  
387  
388 =pod
389  
390 =item B<getSegments()>
391  
392 Return an array containing all segments in the right order.
393  
394 =cut
395  
396 sub getSegments {
397  
398 0 ▶ my $self = shift;
Subroutine getSegments: not called CC=1 SCAR=6.9
Pod documented
399  
400 0 return @{ $self->{SEGMENTS} };
401 }
402  
403  
404 =pod
405  
406 =item B<toString([$pretty])>
407  
408 Return a string representation of this message. This can be used to
409 send the message over a socket to an HL7 server. To print to other
410 output, use the $pretty argument as some true value. This will not use
411 the default segment separator, but '\n' instead.
412  
413 =cut
414  
415 sub toString {
416
417 10 ▶ my ($self, $pretty) = @_;
Subroutine toString: called CC=3 SCAR=11.0
Pod documented
418 10 my $msg = "";
419  
420 # Make sure MSH(1) and MSH(2) are ok, even if someone has changed
421 # these values
422 #
423 10 my $msh = $self->{SEGMENTS}->[0];
424  
425 10 $self->_resetCtrl($msh);
426  
427 10 ▶ for (my $i = 0; $i < @{ $self->{SEGMENTS} }; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @{$self->{"SEGMENTS"};}) 16 10 26
428
429 16 $msg .= $self->getSegmentAsString($i);
430  
431 16 ▶ $pretty ? ($msg .= "\n") : ($msg .= $self->{SEGMENT_SEPARATOR});
Branch: 1 branch
BranchTrueFalseTotal
$pretty ? : 3 13 16
432 }
433
434 10 return $msg;
435 }
436  
437  
438 =pod
439  
440 =item B<getSegmentAsString($index)>
441  
442 Get the string representation of the segment, in the context of this
443 message. That means the string representation will use the message's
444 separators.
445  
446 =cut
447  
448 sub getSegmentAsString {
449  
450 20 ▶ my ($self, $index) = @_;
Subroutine getSegmentAsString: called CC=4 SCAR=13.9
Pod documented
451  
452 20 my $seg = $self->getSegmentByIndex($index);
453  
454 20 ▶ $seg || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$seg or return undef 0 20 20
455  
456 20 my $segStr = $seg->getName() . $self->{FIELD_SEPARATOR};
457
458 20 ▶ my $start = $seg->getName() eq "MSH" ? 2 : 1;
Branch: 1 branch
BranchTrueFalseTotal
$seg->getName eq 'MSH' ? : 11 9 20
459  
460 {
461 7 ▶ no warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
462
463 20 foreach ($start..$seg->size()) {
464
465 79 $segStr .= $self->getSegmentFieldAsString($index, $_);
466 79 $segStr .= $self->{FIELD_SEPARATOR};
467 }
468 }
469
470 20 return $segStr;
471 }
472  
473  
474 =pod
475  
476 =item B<getSegmentFieldAsString($segmentIndex, $fieldIndex)>
477  
478  
479 =cut
480  
481 sub getSegmentFieldAsString {
482
483 81 ▶ my ($self, $segIndex, $fldIndex) = @_;
Subroutine getSegmentFieldAsString: called CC=2 SCAR=7.0
Pod documented
484  
485 81 my $seg = $self->getSegmentByIndex($segIndex);
486  
487 81 ▶ $seg || return undef;
Branch: 1 branch
BranchTrueFalseTotal
$seg or return undef 0 81 81
488  
489 81 return $seg->getFieldAsString($fldIndex);
490 }
491  
492  
493 =pod
494  
495 =item B<removeSegmentByName($name)>
496  
497 Remove the segment indexed by $name. If it doesn't exist, nothing
498 happens, if it does, all segments after this one will be moved one
499 index up.
500  
501 =back
502  
503 =cut
504  
505 sub removeSegmentByName {
506  
507 1 ▶ my ($self, $name) = @_;
Subroutine removeSegmentByName: called CC=3 SCAR=11.0
Pod documented
508 1 my $i = 0;
509  
510 1 foreach (@{ $self->{SEGMENTS} }) {
511 2 ▶ if ($_->getName() eq $name) {
Branch: 1 branch
BranchTrueFalseTotal
if ($_->getName eq $name) { } 1 1 2
512 1 splice( @{ $self->{SEGMENTS} }, $i, 1);
513 }
514 else {
515 1 $i++;
516 }
517 }
518 }
519  
520  
521 7 1;
522  
523 =pod
524  
525 =head1 AUTHOR
526  
527 D.A.Dokter <dokter@wyldebeast-wunderliebe.com>
528  
529 =head1 LICENSE
530  
531 Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is
532 free software; you can redistribute it and/or modify it under the same
533 terms as Perl itself.
534  
535 =cut
« blib/lib/Net/HL7/Daemon.pm ↑ Summary lib/Net/HL7/Messages/ACK.pm »