This is cool but it suffers from a problem that the first one I wrote did too. Give it:
my $mouse = 'This is a story about mice, "I dint know mice were so sma
+rt."';
print trimTo($mouse,30), "\n";
And you can get back things like: This is a story about mice,... The comma (or semi-color, period, quote, etc) is a bit jarring.
This is a recent stab I've ended up using but I would love to see other ideas/answers/hybrids:
sub chop_to_size {
my ($text, $length) = @_;
return $text if length $text < $length;
# make room for ellipsis
my $chop = $length - 3;
$text =~ s/^\s*(.{$chop})\s*.+$/$1/;
$text =~ s/[\s[:punct:]]+$//;
$text .= "...";
$text;
}