Just my two cents. Hope this helpsuse strict; use warnings; use Text::Autoformat; my $string = 'A lengthy text with more than xxx characters to demonstr +ate truncate'; print "Before: $string\n"; print 'Length: ' , length($string) , "\n"; my $truncated = &truncate($string,20); print "After: $truncated\n"; print 'Length: ' , length($truncated) , "\n"; ########################################################### sub truncate { my $string = shift @_; my $length = 75; $length = shift if @_; $string = autoformat($string, { left => 0, right => $length , widow => 0, }); ($string) = split(/\n/,$string); # Just the first element return $string; } ## ###########################################################
In reply to Re^3: How do I truncate a string while preserving words?
by tomfahle
in thread How do I truncate a string while preserving words?
by Cap'n Steve
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |