in reply to Does Perl support unicode-16?
In the case of base64 or uuencode, the desired properties are that the result is a stream of printable ascii characters, suitable for transmission via email, etc. In the case of utf-8, the desired properties are:
As mentioned previously, Perl 5.8 core does include support for all versions of unicode; it uses utf-8 internally, but can read and write data as utf-16 (BE or LE, regardless of what machine you use), by using the "decode" and "encode" functions of Encode.pm, or by using the PerlIO support for character encodings -- you can open a utf-16 file for input or output as follows (not tested):
I've needed a simple script like this when porting certain text data from a wintel (LE) machine to any sort of big-endian box -- cpu dependence is one of the down-sides to the fixed-width 16-bit form of unicode, especially when there happens to be no byte-order-mark (BOM) at the start of the file...# a fancy version of "byte-swapping", combined with "wc" # (not suitable unless you know the input is UTF-16LE): open( INP, "<:UTF-16LE", "input.file" ); open( OUT, ">:UTF-16BE", "output.file" ); my ( $lines, $words, $chars ); while (<INP>) { $lines++; $words += scalar( split ); # we're using utf-8 now... $chars += length(); # counts _characters_ -- NOT BYTES print OUT; } printf( "%7d %7d %7d\n", $lines, $words, $chars );
(update: fixed the file-handle name in the while() statement, so it matched the file-handle name in the first open statement)
|
|---|