in reply to Re: How do I truncate a string while preserving words?
in thread How do I truncate a string while preserving words?

You needn't cut it if the string is not longer than maxlength. Just return the original string then.
return substr($string, 0, rindex($string, ' ') - 1);
I'd cut it on any whitespace, or at least, in front of the last partial word, like this:
$string =~ s/\s*[\w\-]*$/.../; return $string;
I'm assuming that a hyphen is a part of a word. Your idea may differ from mine.

p.s. Hmm, that'll leave any trailing nonword/nonspace characters, for example punctuation characters, intact, making the string just one character too long.

Perhaps this is better?

$string =~ s/\s*(?:[\w\-]+|\W)$/.../;
or
$string =~ s/\s*[\w\-]+$/.../ or $string =~ s/\s*\W$/.../;