This should be a simple problem, but it's stumped me.

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.