http://qs321.pair.com?node_id=390417

kiat has asked for the wisdom of the Perl Monks concerning the following question:

Hi monks,

Please suggest on how I can improve the code below that inserts appropriate breaklines into a long paragraph to achieve a desired vertical spacing between the lines.

use strict; my $string = q~Suppose you run a Web site that includes a banner adver +tisement service. You contract with companies that want their ads dis +played when poeple visit the pages on your site. Each time a visitor +hits one of your pages, you serve an ad embedded in the page that is +sent to the visitor's browser and assess the company a small fee. To +represent this information, you maintain three tables. One table, com +pany, has columns for company name, number, address, and telephone nu +mber. Another table, ad lists ad numbers, the number for the company +that owns the ad, and the amount you charge per hit. The third table, + hit, logs each ad hit by number and the date on which the ad was ser +ved.~; my @words = split / /, $string; my ($string_len, $new_string); foreach (@words) { my $space = 1; $string_len += length($_); if ($string_len >= 50) { $new_string .= "$_<br /><br />"; $string_len = 0; $space = 0; } else { $new_string .= $_; $new_string .= ' ' if $space; } } # output Suppose you run a Web site that includes a banner advertisement<br />< +br />service. You contract with companies that want their ads display +ed<br /><br />when poeple visit the pages on your site. Each time a v +isitor<br /><br />hits one of your pages, you serve an ad embedded in + the page that<br /><br />is sent to the visitor's browser and assess + the company a small<br /><br />fee. To represent this information, y +ou maintain three tables.<br /><br />One table, company, has columns +for company name, number, address,<br /><br />and telephone number. A +nother table, ad lists ad numbers, the<br /><br />number for the comp +any that owns the ad, and the amount you charge<br /><br />per hit. T +he third table, hit, logs each ad hit by number and the<br /><br />da +te on which the ad was served.
As usual, many thanks in advance :)

Update: Thanks all for your wonderful solutions :)