On Fri, Jun 28, 2002 at 06:58:26PM +0100, Andy Wardley wrote:
>> Or is that a problem related to the Perl interpreter rather than to a 
>> module? 
> Yep, I guess that's the case.  There's nothing in TT that explicitly handles
> this.  It all relies on the underlying behaviour of Perl.

If you do anything with XML::Parser, you'll get utf-8 strings, and all
output will be in utf-8. I suppose there are many other things that will
trigger this behaviour. Anyway, this has bugged me many, many times, so
I made a small filehandle-filter to convert output back to iso-8859-1. 
I also made a tiny special-case module that handles stdout, which is
what I need most of the time.

Usage is pretty simple, just use Filter::UTF8::STDOUT in a program, and
all output is converted from utf-8 to iso-8859-1. Filtering any other
handle is handled by the Filter::UTF8 module.

You need Filter::Handle and Unicode::MapUTF8 installed.

--8<--
package Filter::UTF8;

use 5.006;
use strict;
use warnings;
use Filter::Handle;
use Unicode::MapUTF8 qw/from_utf8/;

require Exporter;

our @ISA = qw(Exporter);

our @EXPORT_OK = qw(
    &utf8_filter
    &utf8_unfilter
);

our $VERSION = '0.01';

sub utf8_filter {
    my ($fh, $charset) = @_;
    $charset ||= 'ISO-8859-1';
    Filter::Handle::Filter($fh, sub {from_utf8({-string  => join ($, => @_), 
                                                -charset => $charset})}
                           );
}

sub utf8_unfilter {
    my $fh = shift;
    Filter::Handle::UnFilter($fh);
}
--8<--

--8<--
package Filter::UTF8::STDOUT;

use 5.006;
use strict;
use warnings;
use Filter::UTF8;

our $VERSION = '0.01';

sub import { Filter::UTF8::utf8_filter(\*STDOUT) }
--8<--

-- 
Trond Michelsen



Reply via email to