my $request = new Net::HL7::Request(); my $conn = new Net::HL7::Connection('localhost', 8089);

my $msh = new Net::HL7::Segments::MSH();

my $seg1 = new Net::HL7::Segment(``PID'');

$seg1->setField(1, ``foo'');

$request->addSegment($msh); $request->addSegment($seg1);

my $response = $conn->send($request);


In general one needn't create an instance of the Net::HL7::Message class directly, but use the Net::HL7::Request class. When adding segments, note that the segment index starts at 0, so to get the first segment, segment, do $msg->getSegmentByIndex(0).

The segment separator defaults to \015. To change this, set the variable $Net::HL7::SEGMENT_SEPARATOR.


$msg = new Net::HL7::Message([$msg])
The constructor takes an optional string argument that is a string representation of a HL7 message. If the string representation is not a valid HL7 message. according to the specifications, undef is returned instead of a new instance. This means that segments should be separated within the message with the segment separator (defaults to \015) or a newline, and segments should be syntactically correct. When using the string argument constructor, make sure that you have escaped any characters that would have special meaning in Perl. For instance (using a different subcomponent separator):
    C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x@y@z^z\r");>

would actually mean

    C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x^z\r");>

since '@y@z' would be interpreted as two empty arrays, so do:

    C<$msg = new Net::HL7::Message("MSH*^~\\@*1\rPID***x^x\@y\@z^z\r");>


The control characters and field separator will take the values from the MSH segment, if set. Otherwise defaults will be used. Changing the MSH fields specifying the field separator and control characters after the MSH has been added to the message will result in setting these values for the message.

If the message couldn't be created, for example due to a erroneous HL7 message string, undef is returned.

Add the segment. to the end of the message. The segment should be an instance of Net::HL7::Segment.

insertSegment($segment, $idx)
Insert the segment. The segment should be an instance of Net::HL7::Segment. If the index is not given, nothing happens.

Return the segment specified by $index. Segment count within the message starts at 0.

Return an array of all segments with the given name

Remove the segment indexed by $index. If it doesn't exist, nothing happens, if it does, all segments after this one will be moved one index up.

setSegment($seg, $index)
Set the segment on index. If index is out of range, or not provided, do nothing. Setting MSH on index 0 will revalidate field separator, control characters and hl7 version, based on MSH(1), MSH(2) and MSH(12).

Return an array containing all segments in the right order.

Return a string representation of this message. This can be used to send the message over a socket to an HL7 server. To print to other output, use the $pretty argument as some true value. This will not use the default segment separator, but '\n' instead.

Get the string representation of the segment, in the context of this message. That means the string representation will use the message's separators.

getSegmentFieldAsString($segmentIndex, $fieldIndex)
Remove the segment indexed by $name. If it doesn't exist, nothing happens, if it does, all segments after this one will be moved one index up.


D.A.Dokter <>


Copyright (c) 2002 D.A.Dokter. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.