File Coverage

lib/Net/HL7/Segments/MSH.pm
Criterion Covered Total %
statement 32 32 100.0
branch 10 10 100.0
condition n/a
subroutine 6 6 100.0
pod 1 1 100.0
total 49 49 100.0


line stmt bran cond sub pod time code
1             ################################################################################
2             #
3             # File : Segment.pm
4             # Author : Duco Dokter
5             # Created : Tue Mar 4 13:03:00 2003
6             # Version : $Id: MSH.pm,v 1.6 2004/02/10 14:31:54 wyldebeast Exp $
7             # Copyright : Wyldebeast & Wunderliebe
8             #
9             ################################################################################
10              
11             package Net::HL7::Segments::MSH;
12              
13 5     5   6650 use 5.004;
  5         15  
  5         198  
14 5     5   25 use strict;
  5         425  
  5         204  
15 5     5   26 use base qw(Net::HL7::Segment);
  5         8  
  5         437  
16 5     5   2817 use POSIX qw(strftime);
  5         24551  
  5         27  
17              
18             =pod
19              
20             =head1 NAME
21              
22             Net::HL7::Segments::MSH
23              
24             =head1 SYNOPSIS
25              
26             my $seg = new Net::HL7::Segments::MSH();
27              
28             $seg->setField(9, "ADT^A24");
29             print $seg->getField(1);
30              
31              
32             =head1 DESCRIPTION
33              
34             The Net::HL7::Segments::MSH is an implementation of the
35             L class. The MSH segment is a bit
36             different from other segments, in that the first field is the field
37             separator after the segment name. Other fields thus start counting
38             from 2! The setting for the field separator for a whole message can
39             be changed by the setField method on index 1 of the MSH for that
40             message. The MSH segment also contains the default settings for field
41             2, COMPONENT_SEPARATOR, REPETITION_SEPARATOR, ESCAPE_CHARACTER and
42             SUBCOMPONENT_SEPARATOR. These fields default to ^, ~, \ and &
43             respectively.
44              
45              
46             =head1 METHODS
47              
48             =over 4
49              
50             =item B<$msh = new Net::HL7::Segments::MSH([$fields])>
51              
52             Create an instance of the MSH segment. If a reference to an array is
53             given, all fields will be filled from that array. Note that for
54             composed fields and subcomponents, the array may hold subarrays and
55             subsubarrays. If the reference is not given, the MSH segment will be
56             created with the MSH 1,2,7,10 and 12 fields filled in for convenience.
57              
58             =cut
59              
60             sub _init {
61            
62 25     25   58 my ($self, $fieldsRef) = @_;
63              
64 25         72 $self->SUPER::_init("MSH", $fieldsRef);
65              
66             # Only fill default fields if no fields ref is given
67             #
68 25 100       46 if (! $fieldsRef) {
69 7         19 $self->setField(1, $Net::HL7::FIELD_SEPARATOR);
70 7         22 $self->setField(
71             2,
72             $Net::HL7::COMPONENT_SEPARATOR .
73             $Net::HL7::REPETITION_SEPARATOR .
74             $Net::HL7::ESCAPE_CHARACTER .
75             $Net::HL7::SUBCOMPONENT_SEPARATOR
76             );
77            
78 7         894 $self->setField(7, strftime("%Y%m%d%H%M%S", localtime));
79            
80             # Set ID field
81             #
82 7         122 my $ext = rand(1);
83 7         101 $ext =~ s/[^0-9]//g;
84 7         15 $ext = "." . substr($ext, 1, 5);
85            
86 7         229 $self->setField(10, $self->getField(7) . $ext);
87 7         13 $self->setField(12, $Net::HL7::HL7_VERSION);
88             }
89            
90 25         55 return $self;
91             }
92              
93              
94             =pod
95              
96             =item B
97              
98             Set the field specified by index to value. Indices start at 1, to stay
99             with the HL7 standard. Trying to set the value at index 0 has no
100             effect. Setting the value on index 1, will effectively change the
101             value of Net::HL7::Message::FIELD_SEPARATOR for the message containing
102             this segment, if the value has length 1; setting the field on index 2
103             will change the values of COMPONENT_SEPARATOR, REPETITION_SEPARATOR,
104             ESCAPE_CHARACTER and SUBCOMPONENT_SEPARATOR for the message, if the
105             string is of length 4.
106              
107             =back
108              
109             =cut
110              
111             sub setField {
112              
113 242     242 1 257 my ($self, $index, $value) = @_;
114            
115 242 100       350 if ($index == 1) {
116 28 100       58 if (length($value) != 1) {
117 1         3 return undef;
118             }
119             }
120            
121 241 100       295 if ($index == 2) {
122 28 100       60 if (length($value) != 4) {
123 1         2 return undef;
124             }
125             }
126            
127 240         429 return $self->SUPER::setField($index, $value);
128             }
129              
130              
131             1;
132              
133              
134             =head1 AUTHOR
135              
136             D.A.Dokter
137              
138             =head1 LICENSE
139              
140             Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is
141             free software; you can redistribute it and/or modify it under the same
142             terms as Perl itself.
143              
144             =cut