- my ($self, $body_str) = @_;
- my $charset = ct($self)->{attributes}->{charset} or
- Carp::confess('body_str was given, but no charset is defined');
- body_set($self, \(encode($charset, $body_str, Encode::FB_CROAK)));
+ my ($self, $str) = @_;
+ my $cs = ct($self)->{attributes}->{charset} //
+ croak('body_str was given, but no charset is defined');
+ my $enc = find_encoding($cs) // croak "unknown encoding `$cs'";
+ my $tmp;
+ {
+ my @w;
+ local $SIG{__WARN__} = sub { push @w, @_ };
+ $tmp = $enc->encode($str, Encode::FB_WARN);
+ croak(@w) if @w;
+ };
+ body_set($self, \$tmp);