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 return $converter->convert($text);
47 $text = convert($self->{from}, $self->{to}, $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;
64 return shift->{to_is_utf8};
73 SL::Iconv -- Thin layer on top of Text::Iconv including decode_utf8 usage
81 # Conversion without creating objects:
82 my $text_utf8 = SL::Iconv::convert("ISO-8859-15", "UTF-8", $text_iso);
84 # Conversion with an object:
85 my $converter = SL::Iconv->new("ISO-8859-15", "UTF-8");
86 my $text_utf8 = $converter->convert($text_iso);
90 A thin layer on top of L<Text::Iconv>. Special handling is implemented
91 if the target charset is UTF-8: The resulting string has its UTF8 flag
92 set via a call to C<Encode::decode("utf-8-strict", ...)>.
94 =head1 CLASS FUNCTIONS
98 =item C<new $from_charset, $to_charset>
100 Create a new object for conversion from C<$from_charset> to
103 =item C<convert $from_charset, $to_charset, $text>
105 Converts the string C<$text> from charset C<$from_charset> to charset
106 C<$to_charset>. See the instance method C<convert> for further
109 The object used for this conversion is cached. Therefore multiple
110 calls to C<convert> do not result in multiple initializations of the
115 =head1 INSTANCE FUNCTIONS
119 =item C<convert $text>
121 Converts the string C<$text> from one charset to another (see C<new>).
123 Special handling is implemented if the target charset is UTF-8: The
124 resulting string has its UTF8 flag set via a call to
125 C<Encode::decode("utf-8-strict", ...)>. It is also safe to call
126 C<convert> multiple times for the same string in such cases as the
127 conversion is only done if the UTF8 flag hasn't been set yet.
131 Returns true if the handle converts into UTF8.
135 =head1 MODULE AUTHORS
137 Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
139 L<http://linet-services.de>