| 1 | # Copyright 2009 by Prasad Balan | ||||||||||
| 2 | # All rights reserved. | ||||||||||
| 3 | # | ||||||||||
| 4 | # This library is free software; you can redistribute it and/or modify | ||||||||||
| 5 | # it under the same terms as Perl itself. | ||||||||||
| 6 | package X12::Parser::Cf; | ||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 3 | require Exporter; | |||||||||
| 9 | 3 | our @ISA = qw(Exporter); | |||||||||
| 10 | |||||||||||
| 11 | # Items to export into callers namespace by default. Note: do not export | ||||||||||
| 12 | # names by default without a very good reason. Use EXPORT_OK instead. | ||||||||||
| 13 | # Do not simply export all your public functions/methods/constants. | ||||||||||
| 14 | # This allows declaration use X12::Parser::Cf ':all'; | ||||||||||
| 15 | # If you do not need this, moving things directly into @EXPORT or @EXPORT_OK | ||||||||||
| 16 | # will save memory. | ||||||||||
| 17 | 3 | our %EXPORT_TAGS = ( | |||||||||
| 18 | 'all' => [ | ||||||||||
| 19 | qw( | ||||||||||
| 20 | ) | ||||||||||
| 21 | ] | ||||||||||
| 22 | ); | ||||||||||
| 23 | 3 | our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } ); | |||||||||
| 24 | 3 | our @EXPORT = qw( | |||||||||
| 25 | ); | ||||||||||
| 26 | 3 | our $VERSION = '0.80'; | |||||||||
| 27 | |||||||||||
| 28 | # Preloaded methods go here. | ||||||||||
| 29 | 3 | ▶ | use X12::Parser::Tree; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 30 | |||||||||||
| 31 | 6 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||
| 32 | 6 | my $self = { _LINES => undef, }; | |||||||||
| 33 | 6 | return bless $self; | |||||||||
| 34 | } | ||||||||||
| 35 | |||||||||||
| 36 | 6 | ▶ | sub load { | ||||||||
|
Subroutine
load: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||
| 37 | 6 | my $self = shift; | |||||||||
| 38 | 6 | my %params = @_; | |||||||||
| 39 | |||||||||||
| 40 | #open the config file and read it into array | ||||||||||
| 41 | open( FILE, $params{file} ) | ||||||||||
| 42 | 6 | ▶ | || die "error: cannot open cf file $params{file}\n"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 43 | 6 | @{ $self->{_LINES} } = <FILE>; | |||||||||
| 44 | 6 | chomp( @{ $self->{_LINES} } ); | |||||||||
| 45 | 6 | close(FILE); | |||||||||
| 46 | |||||||||||
| 47 | #create the root tree object | ||||||||||
| 48 | 6 | my $root = X12::Parser::Tree->new(); | |||||||||
| 49 | 6 | $root->set_name("X12"); | |||||||||
| 50 | 6 | my $pos = $self->_get_loop_pos("LOOPS"); | |||||||||
| 51 | |||||||||||
| 52 | #parse the LOOPS section of the config file | ||||||||||
| 53 | 6 | ▶ | for ( my $i = $pos ; $i < scalar @{ $self->{_LINES} } ; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 54 | 62 | ▶ | if ( $self->{_LINES}->[$i] !~ /^[a-zA-Z0-9#]/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 55 | 6 | last; | |||||||||
| 56 | } | ||||||||||
| 57 | else { | ||||||||||
| 58 | 56 | $self->_parse_loop( $root, $self->{_LINES}->[$i] ); | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | 6 | return $root; | |||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | #get the position of a loop section e.g. [2300] | ||||||||||
| 65 | 131 | ▶ | sub _get_loop_pos { | ||||||||
|
Subroutine
_get_loop_pos: called CC=3 SCAR=11.2
| |||||||||||
| 66 | 131 | my ( $self, $loop ) = @_; | |||||||||
| 67 | 131 | ▶ | for ( my $i = 0 ; $i < @{ $self->{_LINES} } ; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 19622 | ▶ | if ( $self->{_LINES}->[$i] =~ /^\[$loop\]/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 131 | return $i + 1; | |||||||||
| 70 | } | ||||||||||
| 71 | } | ||||||||||
| 72 | 0 | return undef; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 125 | ▶ | sub _parse_loop { | ||||||||
|
Subroutine
_parse_loop: called CC=6 SCAR=17.9
| |||||||||||
| 76 | 125 | my ( $self, $node, $loop ) = @_; | |||||||||
| 77 | 125 | my $pos = $self->_get_loop_pos($loop); | |||||||||
| 78 | 125 | my $end = undef; | |||||||||
| 79 | 125 | my $child = X12::Parser::Tree->new(); | |||||||||
| 80 | 125 | $child->set_name($loop); | |||||||||
| 81 | 125 | ▶ | if ( $self->{_LINES}->[$pos] =~ /^segment=/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 125 | $end = substr $self->{_LINES}->[$pos], 8; | |||||||||
| 83 | } | ||||||||||
| 84 | 125 | $child->set_loop_start_parm( split( /:/, $end ) ); | |||||||||
| 85 | 125 | $child->set_parent($node); | |||||||||
| 86 | 125 | $node->add_child($child); | |||||||||
| 87 | 125 | ▶ | for ( my $i = $pos ; $i < scalar( @{ $self->{_LINES} } ) ; $i++ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 868 | ▶ | if ( $self->{_LINES}->[$i] =~ /^segment=/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 89 | 674 | next; | |||||||||
| 90 | } | ||||||||||
| 91 | 194 | ▶ | if ( $self->{_LINES}->[$i] =~ /^loop=/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 92 | 69 | $end = substr $self->{_LINES}->[$i], 5; | |||||||||
| 93 | 69 | $self->_parse_loop( $child, $end ); | |||||||||
| 94 | } | ||||||||||
| 95 | 194 | ▶ | if ( $self->{_LINES}->[$i] !~ /^[a-zA-Z0-9#]/ ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 125 | last; | |||||||||
| 97 | } | ||||||||||
| 98 | } | ||||||||||
| 99 | } | ||||||||||
| 100 | 3 | 1; | |||||||||
| 101 | __END__ | ||||||||||