while (length($str)) { my $rec = substr($str, 0, 128, ''); ... $rec contains 128 bytes ... }