#!/usr/bin/perl use strict; open my $fh, $ARGV[0] or die "Couldn't open '$ARGV[0]': $!"; my $iter = records( blocks($fh), qr/\s*,\s*/ ); while ( defined(my $rec = $iter->()) ) { print $rec, "----\n"; } sub blocks { my $fh = shift; my $blocksize = shift || 8192; sub { return unless read $fh, my($block), $blocksize; return $block; } } sub records { my $blocks = shift; my $terminator = @_ ? shift : quotemeta($/); my @records; my ($buf, $finished) = (""); sub { while (@records == 0 && ! $finished) { if (defined(my $block = $blocks->())) { $buf .= $block; my @newrecs = split /($terminator)/, $buf; while (@newrecs > 2) { push @records, shift(@newrecs).shift(@newrecs); } $buf = join "", @newrecs; } else { @records = $buf; $finished = 1; } } return shift(@records); } }