I thought Parse::RecDescent would be an easy way to deal with these records, and it's worked fine as long as our customers used the format with a '~' as end-of-record marker, but with just bare newlines it fails miserably.
Can anybody tell me what I'm doing wrong here (in this highly simplified version)? I'm ready to do s/$/~/g, but that's so cheesy :-(
use Parse::RecDescent; use strict; my $text_to_parse_with_tildes = <<EOL; IEA*1*000002669~ IEA*2*000003333~ EOL my $text_to_parse_plain = <<EOL; IEA*1*000002669 IEA*2*000003333 EOL my $grammar = q{ doc: segment(s) { $return =1; } segment: segmentid "*" element(s /[*]/) segment_end { print STDERR "got a $item{segmentid} ($main::segment_end)\n"; $return =1; } segmentid: /[a-zA-Z0-9]+/ { $return = $item[1]; } element: /[^*~\n]*/ { $return = $item[1];} segment_end: /$main::segment_end/ { $return = 1;} }; my $parser = new Parse::RecDescent ($grammar) or die "Bad grammar!\n"; #tildes as record-ends, works fine $main::segment_end = '~\n'; defined $parser->doc($text_to_parse_with_tildes) #works fine or print "parse failure! bad text!\n"; #plain record-ends, fails miserably $main::segment_end = '\n'; defined $parser->doc($text_to_parse_plain) #fails or print "parse failure! bad text!\n";
In reply to newlines in Parse::RecDescent by kgoess
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |