sub truncate_string($$) { my ( $string, $max ) = @_; # always do nothing if already short enough ( length( $string ) <= $max ) and return $string; # issue warning if forced to chop a word anyway if ( $string =~ /\s/ ) { warn "cannot truncate string on word boundary"; return substr( $string, 0, $max ); } # truncate pre-existing trailing whitespace $string =~ /^(.*)\s+$/ and return $1; # otherwise truncate on word boundary $string =~ s/\S+$// and return $string; die; # unreachable }