$text =~ s/[^\0-\x7f]//g; #### use Encode; ... my $bytes = encode('utf8', $text); # insert $bytes instead of $text