| 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: 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 | 4 |  |  | 4 |  | 1125 | use 5.004; | 
|  | 4 |  |  |  |  | 11 |  | 
| 14 | 4 |  |  | 4 |  | 17 | use strict; | 
|  | 4 |  |  |  |  | 5 |  | 
|  | 4 |  |  |  |  | 80 |  | 
| 15 | 4 |  |  | 4 |  | 788 | use Net::HL7::Message; | 
|  | 4 |  |  |  |  | 7 |  | 
|  | 4 |  |  |  |  | 2275 |  | 
| 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 | 44 |  |  | 44 | 1 | 2237 | my $class = shift; | 
| 57 | 44 |  |  |  |  | 76 | bless my $self = {}, $class; | 
| 58 |  |  |  |  |  |  |  | 
| 59 | 44 | 100 |  |  |  | 102 | $self->_init(@_) || return undef; | 
| 60 |  |  |  |  |  |  |  | 
| 61 | 40 |  |  |  |  | 121 | return $self; | 
| 62 |  |  |  |  |  |  | } | 
| 63 |  |  |  |  |  |  |  | 
| 64 |  |  |  |  |  |  |  | 
| 65 |  |  |  |  |  |  | sub _init { | 
| 66 |  |  |  |  |  |  |  | 
| 67 | 44 |  |  | 44 |  | 148 | my ($self, $name, $fieldsRef) = @_; | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | # Is the name 3 upper case characters? | 
| 70 |  |  |  |  |  |  | # | 
| 71 | 44 | 100 | 100 |  |  | 212 | ($name && (length($name) == 3)) || return undef; | 
| 72 | 41 | 100 |  |  |  | 94 | (uc($name) eq $name) || return undef; | 
| 73 |  |  |  |  |  |  |  | 
| 74 | 40 |  |  |  |  | 93 | $self->{FIELDS} = []; | 
| 75 |  |  |  |  |  |  |  | 
| 76 | 40 |  |  |  |  | 75 | $self->{FIELDS}->[0] = $name; | 
| 77 |  |  |  |  |  |  |  | 
| 78 | 40 | 100 | 66 |  |  | 138 | if ($fieldsRef && ref($fieldsRef) eq "ARRAY") { | 
| 79 |  |  |  |  |  |  |  | 
| 80 | 22 |  |  |  |  | 28 | for (my $i = 0; $i < @{ $fieldsRef }; $i++) { | 
|  | 167 |  |  |  |  | 378 |  | 
| 81 |  |  |  |  |  |  |  | 
| 82 | 145 |  |  |  |  | 327 | $self->setField($i + 1, $fieldsRef->[$i]); | 
| 83 |  |  |  |  |  |  | } | 
| 84 |  |  |  |  |  |  | } | 
| 85 |  |  |  |  |  |  |  | 
| 86 | 40 |  |  |  |  | 80 | return 1; | 
| 87 |  |  |  |  |  |  | } | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  |  | 
| 90 |  |  |  |  |  |  | =pod | 
| 91 |  |  |  |  |  |  |  | 
| 92 |  |  |  |  |  |  | =item B | 
| 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 | 239 |  |  | 239 | 1 | 643 | my ($self, $index, $value) = @_; | 
| 113 |  |  |  |  |  |  |  | 
| 114 | 239 | 100 | 100 |  |  | 898 | return undef unless ($index and defined($value)); | 
| 115 |  |  |  |  |  |  |  | 
| 116 | 158 |  |  |  |  | 247 | $self->{FIELDS}->[$index] = $value; | 
| 117 |  |  |  |  |  |  |  | 
| 118 | 158 |  |  |  |  | 274 | return 1; | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  |  | 
| 121 |  |  |  |  |  |  |  | 
| 122 |  |  |  |  |  |  | =pod | 
| 123 |  |  |  |  |  |  |  | 
| 124 |  |  |  |  |  |  | =item B | 
| 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 | 181 |  |  | 181 | 1 | 232 | my ($self, $index) = @_; | 
| 138 |  |  |  |  |  |  |  | 
| 139 | 181 | 100 |  |  |  | 313 | if (wantarray) { | 
| 140 | 29 | 100 |  |  |  | 62 | if (ref($self->{FIELDS}->[$index]) eq "ARRAY") { | 
| 141 | 4 |  |  |  |  | 6 | return @{ $self->{FIELDS}->[$index]}; | 
|  | 4 |  |  |  |  | 20 |  | 
| 142 |  |  |  |  |  |  | } | 
| 143 |  |  |  |  |  |  | else { | 
| 144 | 25 |  |  |  |  | 66 | return ($self->{FIELDS}->[$index]); | 
| 145 |  |  |  |  |  |  | } | 
| 146 |  |  |  |  |  |  | } | 
| 147 |  |  |  |  |  |  | else { | 
| 148 | 152 |  |  |  |  | 524 | return $self->{FIELDS}->[$index]; | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  | } | 
| 151 |  |  |  |  |  |  |  | 
| 152 |  |  |  |  |  |  |  | 
| 153 |  |  |  |  |  |  |  | 
| 154 |  |  |  |  |  |  | =pod | 
| 155 |  |  |  |  |  |  |  | 
| 156 |  |  |  |  |  |  | =item B | 
| 157 |  |  |  |  |  |  |  | 
| 158 |  |  |  |  |  |  | Get the string representation of the field | 
| 159 |  |  |  |  |  |  |  | 
| 160 |  |  |  |  |  |  | =cut | 
| 161 |  |  |  |  |  |  |  | 
| 162 |  |  |  |  |  |  | sub getFieldAsString { | 
| 163 |  |  |  |  |  |  |  | 
| 164 | 57 |  |  | 57 | 1 | 61 | my ($self, $index) = @_; | 
| 165 |  |  |  |  |  |  |  | 
| 166 | 57 |  |  |  |  | 63 | my $fieldStr = ""; | 
| 167 | 57 |  |  |  |  | 85 | my $field = $self->{FIELDS}->[$index]; | 
| 168 |  |  |  |  |  |  |  | 
| 169 | 57 | 100 |  |  |  | 89 | if (ref($field) eq "ARRAY") { | 
| 170 |  |  |  |  |  |  |  | 
| 171 | 9 |  |  |  |  | 12 | for (my $i = 0; $i < @{ $field }; $i++) { | 
|  | 32 |  |  |  |  | 77 |  | 
| 172 |  |  |  |  |  |  |  | 
| 173 | 23 | 100 |  |  |  | 40 | if (ref($field->[$i]) eq "ARRAY") { | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 5 |  |  |  |  | 8 | $fieldStr .= join($Net::HL7::SUBCOMPONENT_SEPARATOR, @{ $field->[$i] }); | 
|  | 5 |  |  |  |  | 12 |  | 
| 176 |  |  |  |  |  |  | } | 
| 177 |  |  |  |  |  |  | else { | 
| 178 | 18 |  |  |  |  | 27 | $fieldStr .= $field->[$i]; | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 23 | 100 |  |  |  | 18 | if ($i < (@{ $field } - 1)) { | 
|  | 23 |  |  |  |  | 52 |  | 
| 182 | 14 |  |  |  |  | 27 | $fieldStr .= $Net::HL7::COMPONENT_SEPARATOR; | 
| 183 |  |  |  |  |  |  | } | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  | else { | 
| 187 | 48 |  |  |  |  | 60 | $fieldStr .= $field; | 
| 188 |  |  |  |  |  |  | } | 
| 189 |  |  |  |  |  |  |  | 
| 190 | 57 |  |  |  |  | 138 | return $fieldStr; | 
| 191 |  |  |  |  |  |  | } | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  |  | 
| 194 |  |  |  |  |  |  | =pod | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =item B | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | Get the number of fields for this segment, not including the name | 
| 199 |  |  |  |  |  |  |  | 
| 200 |  |  |  |  |  |  | =cut | 
| 201 |  |  |  |  |  |  |  | 
| 202 |  |  |  |  |  |  | sub size { | 
| 203 |  |  |  |  |  |  |  | 
| 204 | 18 |  |  | 18 | 1 | 24 | my $self = shift; | 
| 205 |  |  |  |  |  |  |  | 
| 206 | 18 |  |  |  |  | 18 | return @{ $self->{FIELDS} } - 1; | 
|  | 18 |  |  |  |  | 62 |  | 
| 207 |  |  |  |  |  |  | } | 
| 208 |  |  |  |  |  |  |  | 
| 209 |  |  |  |  |  |  |  | 
| 210 |  |  |  |  |  |  | =pod | 
| 211 |  |  |  |  |  |  |  | 
| 212 |  |  |  |  |  |  | =item B | 
| 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 | 8 |  |  | 8 | 1 | 1347 | my ($self, $from, $to) = @_; | 
| 223 |  |  |  |  |  |  |  | 
| 224 | 8 | 100 |  |  |  | 19 | $from || ($from = 0); | 
| 225 | 8 | 100 |  |  |  | 19 | $to || ($to = $#{$self->{FIELDS}}); | 
|  | 7 |  |  |  |  | 12 |  | 
| 226 |  |  |  |  |  |  |  | 
| 227 | 8 |  |  |  |  | 24 | return @{ $self->{FIELDS} }[$from..$to]; | 
|  | 8 |  |  |  |  | 51 |  | 
| 228 |  |  |  |  |  |  | } | 
| 229 |  |  |  |  |  |  |  | 
| 230 |  |  |  |  |  |  |  | 
| 231 |  |  |  |  |  |  | =pod | 
| 232 |  |  |  |  |  |  |  | 
| 233 |  |  |  |  |  |  | =item B | 
| 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 | 56 |  |  | 56 | 1 | 71 | my $self = shift; | 
| 244 |  |  |  |  |  |  |  | 
| 245 | 56 |  |  |  |  | 182 | return $self->{FIELDS}->[0]; | 
| 246 |  |  |  |  |  |  | } | 
| 247 |  |  |  |  |  |  |  | 
| 248 |  |  |  |  |  |  |  | 
| 249 |  |  |  |  |  |  | 1; | 
| 250 |  |  |  |  |  |  |  | 
| 251 |  |  |  |  |  |  |  | 
| 252 |  |  |  |  |  |  | =pod | 
| 253 |  |  |  |  |  |  |  | 
| 254 |  |  |  |  |  |  | =head1 AUTHOR | 
| 255 |  |  |  |  |  |  |  | 
| 256 |  |  |  |  |  |  | D.A.Dokter | 
| 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 |