File Coverage

blib/lib/App/HL7/Dump.pm
Criterion Covered Total %
statement 24 60 40.0
branch 0 16 0.0
condition 0 6 0.0
subroutine 8 10 80.0
pod 2 2 100.0
total 34 94 36.1


line stmt bran cond sub pod time code
1             package App::HL7::Dump;
2              
3 2     2   78176 use strict;
  2         11  
  2         59  
4 2     2   11 use warnings;
  2         4  
  2         60  
5              
6 2     2   1070 use Class::Utils qw(set_params);
  2         62755  
  2         41  
7 2     2   152 use English;
  2         4  
  2         13  
8 2     2   945 use Error::Pure qw(err);
  2         5  
  2         102  
9 2     2   4279 use Getopt::Std;
  2         104  
  2         131  
10 2     2   1064 use Net::HL7::Message;
  2         7813  
  2         64  
11 2     2   1069 use Perl6::Slurp qw(slurp);
  2         3247  
  2         12  
12              
13             our $VERSION = 0.04;
14              
15             # Constructor.
16             sub new {
17 0     0 1   my ($class, @params) = @_;
18              
19             # Create object.
20 0           my $self = bless {}, $class;
21              
22             # Process params.
23 0           set_params($self, @params);
24              
25             # Process arguments.
26 0           $self->{'_opts'} = {
27             'c' => 0,
28             'h' => 0,
29             };
30 0 0 0       if (! getopts('ch', $self->{'_opts'}) || @ARGV < 1
      0        
31             || $self->{'_opts'}->{'h'}) {
32              
33 0           print STDERR "Usage: $0 [-c] [-h] [--version] hl7_file\n";
34 0           print STDERR "\t-c\t\tColor mode.\n";
35 0           print STDERR "\t-h\t\tHelp.\n";
36 0           print STDERR "\t--version\tPrint version.\n";
37 0           exit 1;
38             }
39 0           $self->{'_hl7_file'} = $ARGV[0];
40              
41 0 0         if ($ENV{'COLOR'}) {
42 0           $self->{'_opts'}->{'c'} = 1;
43             }
44              
45             # Load Term::ANSIColor.
46 0 0         if ($self->{'_opts'}->{'c'}) {
47 0           eval "require Term::ANSIColor;";
48 0 0         if ($EVAL_ERROR) {
49 0           err "Cannot load 'Term::ANSIColor'.",
50             'Eval error', $EVAL_ERROR;
51             }
52             }
53              
54             # Object.
55 0           return $self;
56             }
57              
58             # Run.
59             sub run {
60 0     0 1   my $self = shift;
61              
62             # Get hl7_file.
63 0           my $hl7 = slurp($self->{'_hl7_file'});
64              
65             # Create message.
66 0           my $msg = Net::HL7::Message->new($hl7);
67 0 0         if (! $msg) {
68 0           err 'Cannot parse HL7 file.', 'File', $self->{'_hl7_file'};
69 0           return 1;
70             }
71              
72             # Segment name: size
73 0           foreach my $seg ($msg->getSegments) {
74 0           foreach my $index (1 .. $seg->size) {
75 0           my $val = $seg->getField($index);
76 0 0         if (defined $val) {
77 0           my $print_val;
78 0 0         if (ref $val eq 'ARRAY') {
79 0           $print_val = $seg->getFieldAsString($index);
80             } else {
81 0           $print_val = $val;
82             }
83 0 0         if ($self->{'_opts'}->{'c'}) {
84 0           print Term::ANSIColor::color('green').$seg->getName.
85             Term::ANSIColor::color('reset').'-'.
86             Term::ANSIColor::color('red').$index.
87             Term::ANSIColor::color('reset').':'.
88             Term::ANSIColor::color('bold white').
89             $print_val.Term::ANSIColor::color('reset')."\n";
90             } else {
91 0           print $seg->getName.'-'.$index.':'.$print_val."\n";
92             }
93             }
94             }
95             }
96              
97 0           return 0;
98             }
99              
100             1;
101              
102              
103             __END__