| 1 | #!/usr/bin/perl -w | ||
| 2 | |||
| 3 | use Moose; | ||
| 4 | use v5.10; | ||
| 5 | use Time::Piece; | ||
| 6 | use Data::Dumper; | ||
| 7 | |||
| 8 | use xDT::Parser; | ||
| 9 | |||
| 10 | my $xdt_file = shift // croak('Error: no xDT file given'); | ||
| 11 | my $config_file = shift; | ||
| 12 | |||
| 13 | my $parser = xDT::Parser->new($config_file); | ||
| 14 | $parser->open($xdt_file); | ||
| 15 | |||
| 16 | while (my $object = $parser->next_object) { | ||
| 17 | last if $object->is_empty; | ||
| 18 | |||
| 19 | say _extract_core_data($object); | ||
| 20 | say _extract_measurements($object); | ||
| 21 | } | ||
| 22 | |||
| 23 | $parser->close(); | ||
| 24 | |||
| 25 | |||
| 26 | sub _extract_core_data { | ||
| 27 | my $object = shift // croak('Error: parameter $object missing.'); | ||
| 28 | |||
| 29 | return sprintf( | ||
| 30 | '%s: %s %s (%s, %s)', | ||
| 31 | $object->get_value('patient_number'), | ||
| 32 | $object->get_value('firstname'), | ||
| 33 | $object->get_value('surname'), | ||
| 34 | Time::Piece->strptime($object->get_value('birthdate'), '%d%m%Y')->ymd, | ||
| 35 | $object->get_value('sex') | ||
| 36 | ); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub _extract_measurements { | ||
| 40 | my $object = shift // croak('Error: parameter $object missing.'); | ||
| 41 | my @measurements = (); | ||
| 42 | my $measurement = (); | ||
| 43 | |||
| 44 | while (my $record = $object->next_record) { | ||
| 45 | last unless defined $record; | ||
| 46 | |||
| 47 | if ($record->get_accessor eq 'test_identification') { | ||
| 48 | push @measurements, $measurement if defined $measurement->{test_identification}; | ||
| 49 | $measurement = (); | ||
| 50 | $measurement->{test_identification} = $record->get_value; | ||
| 51 | } else { | ||
| 52 | foreach my $accessor ('collection_date', 'collection_time', 'result', 'unit') { | ||
| 53 | next unless $record->get_accessor eq $accessor; | ||
| 54 | $measurement->{$accessor} = $record->get_value; | ||
| 55 | } | ||
| 56 | } | ||
| 57 | } | ||
| 58 | push @measurements, $measurement; | ||
| 59 | |||
| 60 | return join "\n", map { | ||
| 61 | my $date = Time::Piece->strptime($_->{collection_date} // '', '%d%m%Y')->ymd; | ||
| 62 | sprintf( | ||
| 63 | '%s - %s: %s %s', | ||
| 64 | $date, | ||
| 65 | $_->{test_identification}, | ||
| 66 | $_->{result}, | ||
| 67 | $_->{unit} | ||
| 68 | ); | ||
| 69 | } @measurements; | ||
| 70 | } |