views:

104

answers:

1

I have an application that is being expanded to the UK and I will need to add support for Latin-9 Unicode. I have done some Googling but found nothing solid as to what is involved in the process. Any tips?

Here is some code (Just the bits for Unicode stuff)

use Unicode::String qw(utf8 latin1 utf16);

# How to call
$encoded_txt = $self->unicode_encode($item->{value});

# Function part
sub unicode_encode {

    shift() if ref($_[0]);
    my $toencode = shift();
    return undef unless defined($toencode);

    Unicode::String->stringify_as("utf8");
    my $unicode_str = Unicode::String->new();


    # encode Perl UTF-8 string into latin1 Unicode::String
    #  - currently only Basic Latin and Latin 1 Supplement
    #    are supported here due to issues with Unicode::String .
    $unicode_str->latin1( $toencode );
    ...

Any help would be great and thanks.

EDIT: I did find this post: http://czyborra.com/charsets/iso8859.html

+4  A: 

Unicode::String is ancient, and designed to add Unicode support to older Perls. Modern versions of Perl (5.8.0 and up) have native Unicode support. Look at the Encode module and the :encoding layer. You can get a list of the supported encodings in your Perl with perldoc Encode::Supported.

Basically, you just need to decode/encode to Latin-9 on input & output. The rest of the time, you should use Perl's native UTF-8 strings.

# Read a Latin-9 file:
open(my $in, '<:encoding(Latin9)', 'some/file');
my $line = <$in>; # Automatically converts Latin9 to UTF-8

# Write a Latin-9 file:
open(my $out, '>:encoding(Latin9)', 'other/file');
print $out $line; # Automatically converts UTF-8 to Latin9
cjm
Thanks, I did look at the reference you gave but I didn't see Latin-9. Any other references/advice?
Phill Pafford
Thanks again, I do have one other issue as some of the client running the old software might not be able to upgrade to a new version of Perl. Is there any support for Latin-9 using the Unicode::String way instead of :encoding? Just want to make it as easy for clients to upgrade as possible.
Phill Pafford
Try `perldoc Encode::Supported` to get a list of supported encodings. search.cpan.org doesn't seem to find the current version (because it got moved to a different place in the tarball).
cjm
Perl 5.6.1 is 9 years old now. (Even 5.6.2 is almost 7.) It's time for them to upgrade. I doubt it would be hard to add Latin9 support to Unicode::String, but you'd probably have to do it yourself.
cjm
hmm trying to use encode for this and it's not working. UK € Code: encode("iso-8859-15", "UK €") any thoughts as to why?
Phill Pafford