kivitendo/SL/Iconv.pm @ master
a200453a | Moritz Bunkus | package SL::Iconv;
|
||
ad7353df | Moritz Bunkus | use Encode;
|
||
use English qw(-no_match_vars);
|
||||
a200453a | Moritz Bunkus | use Text::Iconv;
|
||
use SL::Common;
|
||||
cbd1249e | Sven Schöling | my %converters;
|
||
a200453a | Moritz Bunkus | |||
c510d88b | Sven Schöling | use strict;
|
||
ad7353df | Moritz Bunkus | sub new {
|
||
my $class = shift;
|
||||
my $self = bless { }, $class;
|
||||
$self->_init(@_);
|
||||
return $self;
|
||||
}
|
||||
cd01768d | Sven Schöling | sub _get_converter {
|
||
a200453a | Moritz Bunkus | my ($from_charset, $to_charset) = @_;
|
||
ad7353df | Moritz Bunkus | my $index = join $SUBSCRIPT_SEPARATOR, $from_charset, $to_charset;
|
||
cbd1249e | Sven Schöling | $converters{$index} ||= Text::Iconv->new($from_charset, $to_charset) || die;
|
||
a200453a | Moritz Bunkus | |||
return $converters{$index};
|
||||
}
|
||||
sub convert {
|
||||
ad7353df | Moritz Bunkus | return _convert(@_) if ref $_[0];
|
||
a200453a | Moritz Bunkus | my ($from_charset, $to_charset, $text) = @_;
|
||
dbda14c2 | Moritz Bunkus | $from_charset ||= 'UTF-8';
|
||
$to_charset ||= 'UTF-8';
|
||||
a200453a | Moritz Bunkus | |||
cd01768d | Sven Schöling | my $converter = _get_converter($from_charset, $to_charset);
|
||
30e7f787 | Moritz Bunkus | $text = $converter->convert($text);
|
||
$text = decode("utf-8-strict", $text) if ($to_charset =~ m/^utf-?8$/i) && !Encode::is_utf8($text);
|
||||
return $text;
|
||||
a200453a | Moritz Bunkus | }
|
||
ad7353df | Moritz Bunkus | sub _convert {
|
||
my $self = shift;
|
||||
my $text = shift;
|
||||
cbd1249e | Sven Schöling | $text = convert($self->{from}, $self->{to}, $text) if !$self->{to_is_utf8} || !Encode::is_utf8($text);
|
||
$text = decode("utf-8-strict", $text) if $self->{to_is_utf8} && !Encode::is_utf8($text);
|
||||
ad7353df | Moritz Bunkus | |||
return $text;
|
||||
}
|
||||
sub _init {
|
||||
my $self = shift;
|
||||
$self->{from} = shift;
|
||||
$self->{to} = shift;
|
||||
$self->{to} = 'UTF-8' if lc $self->{to} eq 'unicode';
|
||||
$self->{to_is_utf8} = $self->{to} =~ m/^utf-?8$/i;
|
||||
return $self;
|
||||
}
|
||||
sub is_utf8 {
|
||||
return shift->{to_is_utf8};
|
||||
}
|
||||
a200453a | Moritz Bunkus | 1;
|
||
ad7353df | Moritz Bunkus | __END__
|
||
=head1 NAME
|
||||
SL::Iconv -- Thin layer on top of Text::Iconv including decode_utf8 usage
|
||||
=head1 SYNOPSIS
|
||||
Usage:
|
||||
use SL::Iconv;
|
||||
# Conversion without creating objects:
|
||||
my $text_utf8 = SL::Iconv::convert("ISO-8859-15", "UTF-8", $text_iso);
|
||||
# Conversion with an object:
|
||||
my $converter = SL::Iconv->new("ISO-8859-15", "UTF-8");
|
||||
my $text_utf8 = $converter->convert($text_iso);
|
||||
=head1 DESCRIPTION
|
||||
A thin layer on top of L<Text::Iconv>. Special handling is implemented
|
||||
if the target charset is UTF-8: The resulting string has its UTF8 flag
|
||||
set via a call to C<Encode::decode("utf-8-strict", ...)>.
|
||||
=head1 CLASS FUNCTIONS
|
||||
=over 4
|
||||
=item C<new $from_charset, $to_charset>
|
||||
Create a new object for conversion from C<$from_charset> to
|
||||
C<$to_charset>.
|
||||
=item C<convert $from_charset, $to_charset, $text>
|
||||
Converts the string C<$text> from charset C<$from_charset> to charset
|
||||
C<$to_charset>. See the instance method C<convert> for further
|
||||
discussion.
|
||||
The object used for this conversion is cached. Therefore multiple
|
||||
calls to C<convert> do not result in multiple initializations of the
|
||||
iconv library.
|
||||
=back
|
||||
=head1 INSTANCE FUNCTIONS
|
||||
=over 4
|
||||
=item C<convert $text>
|
||||
Converts the string C<$text> from one charset to another (see C<new>).
|
||||
Special handling is implemented if the target charset is UTF-8: The
|
||||
resulting string has its UTF8 flag set via a call to
|
||||
C<Encode::decode("utf-8-strict", ...)>. It is also safe to call
|
||||
C<convert> multiple times for the same string in such cases as the
|
||||
conversion is only done if the UTF8 flag hasn't been set yet.
|
||||
=item C<is_utf8>
|
||||
Returns true if the handle converts into UTF8.
|
||||
=back
|
||||
=head1 MODULE AUTHORS
|
||||
Moritz Bunkus E<lt>m.bunkus@linet-services.deE<gt>
|
||||
L<http://linet-services.de>
|