The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.


X12::Parser - Perl module for parsing X12 Transaction files


    use X12::Parser;

    # Create a parser object
    my $p = new X12::Parser;

    # Parse a file with the transaction specific configuration file
    $p->parsefile ( 
        file => '837.txt',
        conf => '' 

    # Step through the file 
    while ( my $loop = $p->get_next_loop ) {
        my @loop = $p->get_loop_segments; 

    # or use this method instead 
    while ( my ($pos, $loop) = $p->get_next_pos_loop ) { 
        my @loop = $p->get_loop_segments; 

    # or use
    while ( my ($pos, $level, $loop) = $p->get_next_pos_level_loop ) {
        my @loop = $p->get_loop_segments; 


X12::Parser package provides a efficient way to parse X12 transaction files. Although this package is built keeping HIPAA related X12 transactions in mind, it is flexible and can be adapted to parse any X12 or similar transactions.


The X12::Parser is a token based parser for parsing X12 transaction files. The parsing of transaction files requires the presence of configuration files for the different transaction types.

The following methods are available:

    $p = new X12::Parser;

This is the object constructor method. It does not take any arguments. It only initializes the members variables required for parsing the transaction file.

    $p->parsefile(file => '837.txt', conf => '');

This method takes two arguments. The first argument is the transaction file that needs to be parsed. The second argument specifies the configuration file to be used for parsing the transaction file.

To create your own configuration file read the X12::Parser::Readme man page.

    open($file, '837.txt');
    $p->parse(handle => $file, conf => '');

This method is similar to parsefile, except it takes an open file handle as input.


Close a X12 file. If you parse an X12 file till the very end the Parser will close the file. But if you only parse a few segments you can explicitly close the file by calling closefile();


This function returns the name of the next loop that is present in the file being parsed. The loop name is as specified in the cf file.


This function returns the segments in the loop that was returned by get_next_loop(). This function is to be used in tandem with the get_next_loop. If not it may return/produce undesired results.


This function returns the next loop name and the segment position. Note position 1 corresponds to the first segment.


Same as get_next_pos_loop() except that in addition this function returns the level of the loop. The level corresponds to the level of the loop in the loop hierarchy. The top level loop has level 1.


Prints the transaction file in a tree format.


Get the segment separator.


Get the element separator.


Get the sub-element separator.

The configuration files provided with this package and the corresponding transaction type are mentioned below. These are the X12 HIPAA transactions.

            type    configuration file
            ----    ------------------
        1)   270
        2)   271
        3)   276
        4)   277
        5)   278
        6)   278
        7)   820
        8)   834
        9)   835
        10)  837I
        11)  837D
        12)  837P

These cf files are installed in under the X12/Parser/cf folder.

The sample cf files provided with this package are good to the best of the authors knowledge. However the user should ensure the validity of these files. The user may use them as is at their own risk.


None by default.


For details on Transaction sets refer to: National Electronic Data Interchange Transaction Set Implementation Guide. Implementation guides are available for all the Transaction sets.

X12::Parser::Readme for more information on the Parser and configuration files.



If you have a mailing list set up for your module, mention it here.

If you have a web site set up for your module, mention it here.


Prasad Balan,


Copyright 2009 by Prasad Balan

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.