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


in reply to splitting up a long text at regular intervals

Since preserving paragraphs is more important than exact linecount; this is about what you want: keep reading paragraphs, until they exceed a predefined number of lines.

use Fatal qw(open); use constant MAXLINES => 2; open my $i, 'data.txt'; (local $/, my ($sum, $buf)) = ( '', 0 , undef); while (<>) { $buf .= $_; $sum += tr /\n/\n/; next if $sum < MAXLINES ; print "$buf----------";; ($sum, $buf) = (0, undef ); }