http://www.perlmonks.org?node_id=470222

Cody Pendant has asked for the wisdom of the Perl Monks concerning the following question:

I want to process a text file in human-compatible chunks of a certain size or below, as in, not "sysread" for chunks of 2048 bytes, but something which won't have end up dividing words or sentences.

By which I mean I want to go paragraph by paragraph, until I get to a chunk of a certain size, so I got something like this:

open( X, 'x.txt' ); my ( $chunk, $line ); while (<X>) { $line = $_; if ( ( length($chunk) + length($line) ) > 2048 ) { # if the next line would take us over the set size doSomethingWithChunk($chunk); $chunk = $line; } else { $chunk .= $line; # append line and keep going } } doSomethingWithChunk($chunk); # process whatever's left in $chunk at the end
which is fine, but what I'd really like to do is have some kind of sub which would return me those chunks until the file ran out.

Something like how Algorithm::Permute does this:

my $p = new Algorithm::Permute(['a'..'d']); while (@res = $p->next) { # do something with @res }

Is there a module or a recognised way to do this? I'm blanking on the way to do it.



($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
=~y~b-v~a-z~s; print