in reply to limiting length of utf8 string in bytes

sub limit_bytes { my ($str, $max_bytes) = @_; utf8::encode $str; if (length($str) > $max_bytes) { substr($str, $max_bytes+1) = ''; $str =~ s/(?:[\xC0-\xFF]?[\x80-\xBF]+|.)\z//; } utf8::decode $str; return $str; }

Using use bytes; to encode is a bad idea, but if that's what you want, don't forget to also use utf8::upgrade.

sub limit_bytes { my ($str, $max_bytes) = @_; utf8::upgrade $str; use bytes; if (length($str) > $max_bytes) { substr($str, $max_bytes+1) = ''; $str =~ s/(?:[\xC0-\xFF]?[\x80-\xBF]+|.)\z//; } return $str; }