in reply to Re^2: Display shortened paragraph
in thread Display shortened paragraph

Since people have shown how the use of a regex approach tends to be slower, here's a way to observe word boundaries (well, spaces between words, anyway) without using a regex:
my $maxlen = 20; my $longtext = "This is some very long string that needs to be truncat +ed to $maxlen characters..."; my $trunctext = substr( $longtext, 0, rindex( $longtext, " ", $maxlen +)); print "$longtext\n$trunctext\n";
The rindex function, like substr, is faster than a regex match.