blib/lib/Net/HL7/Segment.pm untested

filter: Statementstmt 0.0% 0 / 67 Branchbran 0.0% 0 / 14 Conditioncond 0.0% 0 / 21 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 11 Podpod 0.0% 0 / 8 totaltotal 0.0% 0 / 121 SCAR 42.8
CC
8
Cov
0%
getFieldAsString
20.0
getField
12.0
_init
6.0
CRAP 72.0
1 ################################################################################
2 #
3 # File : Segment.pm
4 # Author : Duco Dokter
5 # Created : Tue Mar 4 13:03:00 2003
6 # Version : $Id: Segment.pm,v 1.15 2014/09/11 09:17:20 wyldebeast Exp $
7 # Copyright : Wyldebeast & Wunderliebe
8 #
9 ################################################################################
10  
11 package Net::HL7::Segment;
12  
13 use 5.004;
14 use strict;
15 use Net::HL7::Message;
16 #use Net::HL7::Segments::CtrlImpl;
17  
18  
19 =pod
20  
21 =head1 NAME
22  
23 Net::HL7::Segment
24  
25 =head1 SYNOPSIS
26  
27 my $seg = new Net::HL7::Segment("PID");
28  
29 $seg->setField(3, "12345678");
30 print $seg->getField(1);
31  
32 =head1 DESCRIPTION
33  
34 The Net::HL7::Segment class represents segments of the HL7 message.
35  
36 =head1 METHODS
37  
38 =over 4
39  
40 =item B<$seg = new Net::HL7::Segment($name, [$fields])>
41  
42 Create an instance of this segment. A segment may be created with just
43 a name or a name and a reference to an array of field values. If the
44 name is not given, no segment is created. The segment name should be
45 three characters long, and upper case. If it isn't, no segment is
46 created, and undef is returned. If a reference to an array is given,
47 all fields will be filled from that array. Note that for composed
48 fields and subcomponents, the array may hold subarrays and
49 subsubarrays. Repeated fields can not be supported the same way, since
50 we can't distinguish between composed fields and repeated fields.
51  
52 =cut
53  
54 sub new {
55  
56 my $class = shift;
57 bless my $self = {}, $class;
58  
59 $self->_init(@_) || return undef;
60  
61 return $self;
62 }
63  
64  
65 sub _init {
66  
67 my ($self, $name, $fieldsRef) = @_;
68  
69 # Is the name 3 upper case characters?
70 #
71 ($name && (length($name) == 3)) || return undef;
72 (uc($name) eq $name) || return undef;
73  
74 $self->{FIELDS} = [];
75  
76 $self->{FIELDS}->[0] = $name;
77  
78 if ($fieldsRef && ref($fieldsRef) eq "ARRAY") {
79  
80 for (my $i = 0; $i < @{ $fieldsRef }; $i++) {
81  
82 $self->setField($i + 1, $fieldsRef->[$i]);
83 }
84 }
85  
86 return 1;
87 }
88  
89  
90 =pod
91  
92 =item B<setField($index, $value)>
93  
94 Set the field specified by index to value, and return some true value
95 if the operation succeeded. Indices start at 1, to stay with the HL7
96 standard. Trying to set the value at index 0 has no effect. The value
97 may also be a reference to an array (that may itself contain arrays)
98 to support composed fields (and subcomponents).
99  
100 To set a field to the HL7 null value, instead of omitting a field, can
101 be achieved with the Net::HL7::NULL type, like:
102  
103 $segment->setField(8, $Net::HL7::NULL);
104  
105 This will render the field as the double quote ("").
106 If values are not provided at all, the method will just return.
107  
108 =cut
109  
110 sub setField {
111  
112 my ($self, $index, $value) = @_;
113  
114 return undef unless ($index and defined($value));
115  
116 $self->{FIELDS}->[$index] = $value;
117  
118 return 1;
119 }
120  
121  
122 =pod
123  
124 =item B<getField($index)>
125  
126 Get the field at index. If the field is a composed field, you might
127 ask for the result to be an array like so:
128  
129 my @subfields = $seg->getField(9)
130  
131 otherwise the thing returned will be a reference to an array.
132  
133 =cut
134  
135 sub getField {
136  
137 my ($self, $index) = @_;
138  
139 if (wantarray) {
140 if (ref($self->{FIELDS}->[$index]) eq "ARRAY") {
141 return @{ $self->{FIELDS}->[$index]};
142 }
143 else {
144 return ($self->{FIELDS}->[$index]);
145 }
146 }
147 else {
148 return $self->{FIELDS}->[$index];
149 }
150 }
151  
152  
153  
154 =pod
155  
156 =item B<getFieldAsString()>
157  
158 Get the string representation of the field
159  
160 =cut
161  
162 sub getFieldAsString {
163  
164 my ($self, $index) = @_;
165  
166 my $fieldStr = "";
167 my $field = $self->{FIELDS}->[$index];
168  
169 if (ref($field) eq "ARRAY") {
170  
171 for (my $i = 0; $i < @{ $field }; $i++) {
172  
173 if (ref($field->[$i]) eq "ARRAY") {
174  
175 $fieldStr .= join($Net::HL7::SUBCOMPONENT_SEPARATOR, @{ $field->[$i] });
176 }
177 else {
178 $fieldStr .= $field->[$i];
179 }
180  
181 if ($i < (@{ $field } - 1)) {
182 $fieldStr .= $Net::HL7::COMPONENT_SEPARATOR;
183 }
184 }
185 }
186 else {
187 $fieldStr .= $field;
188 }
189  
190 return $fieldStr;
191 }
192  
193  
194 =pod
195  
196 =item B<size()>
197  
198 Get the number of fields for this segment, not including the name
199  
200 =cut
201  
202 sub size {
203  
204 my $self = shift;
205  
206 return @{ $self->{FIELDS} } - 1;
207 }
208  
209  
210 =pod
211  
212 =item B<getFields([$from, [$to]])>
213  
214 Get the fields in the specified range, or all if nothing specified. If
215 only the 'from' value is provided, all fields from this index till the
216 end of the segment will be returned.
217  
218 =cut
219  
220 sub getFields {
221  
222 my ($self, $from, $to) = @_;
223  
224 $from || ($from = 0);
225 $to || ($to = $#{$self->{FIELDS}});
226  
227 return @{ $self->{FIELDS} }[$from..$to];
228 }
229  
230  
231 =pod
232  
233 =item B<getName()>
234  
235 Get the name of the segment. This is basically the value at index 0
236  
237 =back
238  
239 =cut
240  
241 sub getName {
242  
243 my $self = shift;
244  
245 return $self->{FIELDS}->[0];
246 }
247  
248  
249 1;
250  
251  
252 =pod
253  
254 =head1 AUTHOR
255  
256 D.A.Dokter <dokter@wyldebeast-wunderliebe.com>
257  
258 =head1 LICENSE
259  
260 Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is
261 free software; you can redistribute it and/or modify it under the same
262 terms as Perl itself.
263  
264 =cut