in reply to How to wrap text to a maximum width of 70 characters per line?

(Assuming you don't want paragraphs to wrap in the middle of words.)

Here's a nice little regex solution:

$paragraph =~ s/([^\n]{0,70})(?:\b\s*|\n)/$1\n/gi;
Or genericized to any width, determined by a variable:
my $max_width = 40; $paragraph =~ s/([^\n]{0,$max_width})(?:\b\s*|\n)/$1\n/gio;

Smart Substrings has some interesting insight. Combining that with the solution above:

$paragraph =~ s/([^\n]{50,70})(?:\b\s*|\n)/$1\n/gi; while ( $paragraph =~ m/([^\n]{71})/ ) { $paragraph =~ s/([^\n]{70})([^\n])/$1\n$2/g; }

In other words, split lines which are more than 70 chars long and include no spaces. (Not sure the loop is necessary...)