use strict; use warnings; while ( my $string = ) { chomp $string; my ( @stringlets ) = $string =~ m/(?:.{0,40}(?:\s|$))|(?:\S{40})/gs; print "$_\n" foreach @stringlets; print "\n\n"; } __DATA__ Now is the time for all good men to come to the aid of their country. Nowisthetimeforallgoodmentocometotheaidoftheircountry.