use strict; my $line_length = 10; my $line = '123 1234 12345 123456 1234567'; my @lines = ($line =~ /(.{1,$line_length}(?:\s|$))/g); print "-" x $line_length,"\n"; print "$_\n" for @lines;