Thanks for giving my "wouldn't it be cool if..." the full treatment.
In the meantime, I suppose we'll have to:
open my $ifh, '<', $filename
or die "Cannot open '$filename' for reading: $!";
local $/;
foreach my $chunk ( split /Separator\s+\d+/, scalar(<$ifh>) ) {
# yay chunk!
}
Unfortunately this will not do well for very large files. We'd have to check against the regexp as each byte is read into memory.
# I might be way off-base here:
no warnings 'uninitialized';
my $pattern = qr{Separator\s\d+};
my $callback = sub { warn "Chunk: @_" };
binmode($ifh);
my $offset = 0;
my $buffer = '';
while( sysread($ifh, my $byte, 1, $offset++) ) {
$buffer .= $byte;
if( $buffer =~ $pattern ) {
$callback->( $buffer );
$buffer = '';
}
}
Even that won't work correctly, and it would be really, really slow.
I only wrote it here for the sake of discussion.
|