develooper Front page | perl.perl5.porters | Postings from February 2007

Re: Future Perl development

Juerd Waalboer
February 5, 2007 13:47
Re: Future Perl development
Message ID:
Gerard Goossen skribis 2007-02-05 22:35 (+0100):
> > Why would you care about the representation in memory? Will the string
> > be passed to a C function that expects bytes, and not UTF-8?
> Yes, that is a perfect example.

Don't pass unicode strings to things that aren't your Perl program!

Communicate with the outside world (including i/o and C library calls)
using binary strings.

Option 1: Find out which encoding the receiving end requires, and encode
into that encoding. For example, if the receiver requires UTF-16
encoding, use:

    use Encode qw(encode);
    foo(encode("UTF-16", $string));

Option 2: Find out which encodings the receiving end allows, and encode
into any of those encodings. Specify the encoding using the designated
header or footer sequences:

    use Encode qw(encode);
    my $enc = "UTF-8";
    print "Content-Type: text/html; charset=$enc\n\n";

    print encode($enc, $string);

Of course, if your communication goes via a filehandle, you'd use a
PerlIO layer instead of explicitly encoding every single string:

    my $enc = "UTF-8";
    print "Content-Type: text/html; charset=$enc\n\n";

    binmode STDOUT, ":encoding($enc)";
    print $string;
korajn salutojn,

  juerd waalboer:  perl hacker  <>  <>
  convolution:     ict solutions and consultancy <>

Ik vertrouw stemcomputers niet.
Zie <>. Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About