| 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 | use 5.004; | ||
| 14 | use strict; | ||
| 15 | use base qw(Net::HL7::Segment); | ||
| 16 | use POSIX qw(strftime); | ||
| 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<Net::HL7::Segment|Net::HL7::Segment> 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 | my ($self, $fieldsRef) = @_; | ||
| 63 | |||
| 64 | $self->SUPER::_init("MSH", $fieldsRef); | ||
| 65 | |||
| 66 | # Only fill default fields if no fields ref is given | ||
| 67 | # | ||
| 68 | if (! $fieldsRef) { | ||
| 69 | $self->setField(1, $Net::HL7::FIELD_SEPARATOR); | ||
| 70 | $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 | $self->setField(7, strftime("%Y%m%d%H%M%S", localtime)); | ||
| 79 | |||
| 80 | # Set ID field | ||
| 81 | # | ||
| 82 | my $ext = rand(1); | ||
| 83 | $ext =~ s/[^0-9]//g; | ||
| 84 | $ext = "." . substr($ext, 1, 5); | ||
| 85 | |||
| 86 | $self->setField(10, $self->getField(7) . $ext); | ||
| 87 | $self->setField(12, $Net::HL7::HL7_VERSION); | ||
| 88 | } | ||
| 89 | |||
| 90 | return $self; | ||
| 91 | } | ||
| 92 | |||
| 93 | |||
| 94 | =pod | ||
| 95 | |||
| 96 | =item B<setField($index, $value)> | ||
| 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 | my ($self, $index, $value) = @_; | ||
| 114 | |||
| 115 | if ($index == 1) { | ||
| 116 | if (length($value) != 1) { | ||
| 117 | return undef; | ||
| 118 | } | ||
| 119 | } | ||
| 120 | |||
| 121 | if ($index == 2) { | ||
| 122 | if (length($value) != 4) { | ||
| 123 | return undef; | ||
| 124 | } | ||
| 125 | } | ||
| 126 | |||
| 127 | return $self->SUPER::setField($index, $value); | ||
| 128 | } | ||
| 129 | |||
| 130 | |||
| 131 | 1; | ||
| 132 | |||
| 133 | |||
| 134 | =head1 AUTHOR | ||
| 135 | |||
| 136 | D.A.Dokter <dokter@wyldebeast-wunderliebe.com> | ||
| 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 |