4 use English qw(-no_match_vars);
15 my $self = bless { }, $class;
23 my ($from_charset, $to_charset) = @_;
25 my $index = join $SUBSCRIPT_SEPARATOR, $from_charset, $to_charset;
26 $converters{$index} ||= Text::Iconv->new($from_charset, $to_charset) || die;
28 return $converters{$index};
32 return _convert(@_) if ref $_[0];
34 my ($from_charset, $to_charset, $text) = @_;
36 $from_charset ||= Common::DEFAULT_CHARSET;
37 $to_charset ||= Common::DEFAULT_CHARSET;
39 my $converter = _get_converter($from_charset, $to_charset);
40 $text = $converter->convert($text);
41 $text = decode("utf-8-strict", $text) if ($to_charset =~ m/^utf-?8$/i) && !Encode::is_utf8($text);
50 $text = convert($self->{from}, $self->{to}, $text) if !$self->{to_is_utf8} || !Encode::is_utf8($text);
51 $text = decode("utf-8-strict", $text) if $self->{to_is_utf8} && !Encode::is_utf8($text);
58 $self->{from} = shift;
60 $self->{to} = 'UTF-8' if lc $self->{to} eq 'unicode';
61 $self->{to_is_utf8} = $self->{to} =~ m/^utf-?8$/i;
67 return shift->{to_is_utf8};
76 SL::Iconv -- Thin layer on top of Text::Iconv including decode_utf8 usage
84 # Conversion without creating objects:
85 my $text_utf8 = SL::Iconv::convert("ISO-8859-15", "UTF-8", $text_iso);
87 # Conversion with an object:
88 my $converter = SL::Iconv->new("ISO-8859-15", "UTF-8");
89 my $text_utf8 = $converter->convert($text_iso);
93 A thin layer on top of L<Text::Iconv>. Special handling is implemented
94 if the target charset is UTF-8: The resulting string has its UTF8 flag
95 set via a call to C<Encode::decode("utf-8-strict", ...)>.
97 =head1 CLASS FUNCTIONS
101 =item C<new $from_charset, $to_charset>
103 Create a new object for conversion from C<$from_charset> to
106 =item C<convert $from_charset, $to_charset, $text>
108 Converts the string C<$text> from charset C<$from_charset> to charset
109 C<$to_charset>. See the instance method C<convert> for further
112 The object used for this conversion is cached. Therefore multiple
113 calls to C<convert> do not result in multiple initializations of the
118 =head1 INSTANCE FUNCTIONS
122 =item C<convert $text>
124 Converts the string C<$text> from one charset to another (see C<new>).
126 Special handling is implemented if the target charset is UTF-8: The
127 resulting string has its UTF8 flag set via a call to
128 C<Encode::decode("utf-8-strict", ...)>. It is also safe to call
129 C<convert> multiple times for the same string in such cases as the
130 conversion is only done if the UTF8 flag hasn't been set yet.
134 Returns true if the handle converts into UTF8.
138 =head1 MODULE AUTHORS
140 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
142 L<http://linet-services.de>