use warnings; use strict; my $chunksize = 50; my $filenumber = 0; my $outsize = $chunksize + 1; my $eof = 0; while () { if ($outsize > $chunksize and /^11/) { $outsize = 0; $filenumber++; print "*** New file number $filenumber\n"; } print "$_"; $outsize += length; } __DATA__ 11 The first invoice. We get this whole thing even though it exceeds the 50 character limit because the conditional code requires an invoice marker before it will start a new file. 11 Second 11 Third 11 Fourth invoice 11 Fifth invoice This one is longer, but not excessive 11 Sixth: break before because limit hit in fifth 11 seventh and final #### *** New file number 1 11 The first invoice. We get this whole thing even though it exceeds the 50 character limit because the conditional code requires an invoice marker before it will start a new file. *** New file number 2 11 Second 11 Third 11 Fourth invoice 11 Fifth invoice This one is longer, but not excessive *** New file number 3 11 Sixth: break before because limit hit in fifth 11 seventh and final