4 use English qw(-no_match_vars);
9 use vars qw(%converters);
15 my $self = bless { }, $class;
23 my ($from_charset, $to_charset) = @_;
25 my $index = join $SUBSCRIPT_SEPARATOR, $from_charset, $to_charset;
26 $converters{$index} ||= SL::Iconv->new($from_charset, $to_charset);
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 return $converter->convert($text);
47 $text = $self->{handle}->convert($text) if !$self->{to_is_utf8} || !Encode::is_utf8($text);
48 $text = decode("utf-8-strict", $text) if $self->{to_is_utf8} && !Encode::is_utf8($text);
55 $self->{from} = shift;
57 $self->{to} = 'UTF-8' if lc $self->{to} eq 'unicode';
58 $self->{to_is_utf8} = $self->{to} =~ m/^utf-?8$/i;
59 $self->{handle} = Text::Iconv->new($self->{from}, $self->{to}) || die;
65 return shift->{to_is_utf8};
74 SL::Iconv -- Thin layer on top of Text::Iconv including decode_utf8 usage
82 # Conversion without creating objects:
83 my $text_utf8 = SL::Iconv::convert("ISO-8859-15", "UTF-8", $text_iso);
85 # Conversion with an object:
86 my $converter = SL::Iconv->new("ISO-8859-15", "UTF-8");
87 my $text_utf8 = $converter->convert($text_iso);
91 A thin layer on top of L<Text::Iconv>. Special handling is implemented
92 if the target charset is UTF-8: The resulting string has its UTF8 flag
93 set via a call to C<Encode::decode("utf-8-strict", ...)>.
95 =head1 CLASS FUNCTIONS
99 =item C<new $from_charset, $to_charset>
101 Create a new object for conversion from C<$from_charset> to
104 =item C<convert $from_charset, $to_charset, $text>
106 Converts the string C<$text> from charset C<$from_charset> to charset
107 C<$to_charset>. See the instance method C<convert> for further
110 The object used for this conversion is cached. Therefore multiple
111 calls to C<convert> do not result in multiple initializations of the
116 =head1 INSTANCE FUNCTIONS
120 =item C<convert $text>
122 Converts the string C<$text> from one charset to another (see C<new>).
124 Special handling is implemented if the target charset is UTF-8: The
125 resulting string has its UTF8 flag set via a call to
126 C<Encode::decode("utf-8-strict", ...)>. It is also safe to call
127 C<convert> multiple times for the same string in such cases as the
128 conversion is only done if the UTF8 flag hasn't been set yet.
132 Returns true if the handle converts into UTF8.
136 =head1 MODULE AUTHORS
138 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
140 L<http://linet-services.de>