the last chunk isn't read at all

eh? That shouldn't be. Perl returns whatever's after the last $/.

hope that there's a nice and clean way [...] without any ugly corrective code.

I don't know if you'll consider the following "nice and clean", but at least there's no "ugly corrective code" (or any corrective code at all) in the following. It employs a single-line lookahead.

(Update: I've replaced the code I had here originally with a version that hides the guts in an iterator. It's longer, but the usage is much simpler.)

Usage:

my $rec_reader = make_rec_reader('myrecordsep'); while (my $rec = $rec_reader->($fh)) { print("Record\n"); print("======\n"); print "$_\n" for @$rec; print("\n"); }

Guts:

sub make_rec_reader { my ($sep) = @_; my $first = 1; my $line; my @rec; return sub { my ($fh) = @_; # Skip what's before first record. if ($first) { $first = 0; for (;;) { $line = <$fh>; last if not defined $line; chomp($line); last if $line eq $sep; } } while (defined($line)) { my @rec; for (;;) { push @rec, $line; $line = <$fh>; last if not defined $line; chomp($line); last if $line eq $sep; } return \@rec; } }; }

In reply to Re: Change the behavior of Perl's IRS by ikegami
in thread Change the behavior of Perl's IRS by LighthouseJ

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.