my $new_string; my $width = 50; while ( length( $string ) > $width ) { my $brksp = rindex( $string, ' ', $width ); $new_string .= substr( $string, 0, $brksp ) . '

'; $string = substr( $string, $brksp+1 ); } $new_string .= $string;