sub encode_utf8_structure {
++$depth;
my @ret;
- for my $_ (@_) {
+ for $_ (@_) {
if (ref($_) eq 'HASH') {
push @ret, {encode_utf8_structure(%{$depth == 1 ? dclone($_):$_})};
}
=cut
-our %iconv_converters;
-
sub convert_to_utf8 {
my ($data,$charset,$internal_call) = @_;
$internal_call //= 0;
if ($charset eq 'RAW') {
croak("Charset must not be raw when calling convert_to_utf8");
}
- if (not defined $iconv_converters{$charset}) {
- eval {
- $iconv_converters{$charset} = Text::Iconv->new($charset,"UTF-8") or
- die "Unable to create converter for '$charset'";
- };
- if ($@) {
- return undef if $internal_call;
- warn $@;
- # We weren't able to create the converter, so use Encode
- # instead
- return __fallback_convert_to_utf8($data,$charset);
- }
- }
- if (not defined $iconv_converters{$charset}) {
+ my $iconv_converter;
+ eval {
+ $iconv_converter = Text::Iconv->new($charset,"UTF-8") or
+ die "Unable to create converter for '$charset'";
+ };
+ if ($@) {
return undef if $internal_call;
- warn "The converter for $charset wasn't created properly somehow!";
+ warn $@;
+ # We weren't able to create the converter, so use Encode
+ # instead
return __fallback_convert_to_utf8($data,$charset);
}
- my $converted_data = $iconv_converters{$charset}->convert($data);
+ my $converted_data = $iconv_converter->convert($data);
# if the conversion failed, retval will be undefined or perhaps
# -1.
- my $retval = $iconv_converters{$charset}->retval();
+ my $retval = $iconv_converter->retval();
if (not defined $retval or
$retval < 0
) {
# if there's an à (0xC3), it's probably something
# horrible, and we shouldn't try to convert it.
if (defined $call_back_data and $call_back_data !~ /\x{C3}/) {
- warn "failed to convert to utf8 (charset: $charset, data: $data), but succeeded with ISO8859-1: ".encode_utf8($call_back_data);
+ # this warning produces far too much useless output; elminating it
+ # warn "failed to convert to utf8 (charset: $charset, data: $data), but succeeded with ISO8859-1: ".encode_utf8($call_back_data);
return $call_back_data;
}
}