while () { $_ = substr $_, 0, $maxlength if length > $maxlength; # ... }