X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pod%2Fperluniintro.pod;h=751bdc6f02af364b3da93affc456ab8804161cff;hb=a8476e91ee770bd8b0c7183fe1314d9effd435ad;hp=0d840d11d817ca790e902f97c6495f1f1691a38b;hpb=1bfb14c4b8e2fdc2db05afcae1e2a6aee6eb3290;p=p5sagit%2Fp5-mst-13.2.git diff --git a/pod/perluniintro.pod b/pod/perluniintro.pod index 0d840d1..751bdc6 100644 --- a/pod/perluniintro.pod +++ b/pod/perluniintro.pod @@ -132,7 +132,7 @@ operations. Only one case remains where an explicit C is needed: if your Perl script itself is encoded in UTF-8, you can use UTF-8 in your identifier names, and in string and regular expression literals, by saying C. This is not the default because -scripts with legacy 8-bit data in them would break. +scripts with legacy 8-bit data in them would break. See L. =head2 Perl's Unicode Model @@ -150,7 +150,7 @@ character set. Otherwise, it uses UTF-8. A user of Perl does not normally need to know nor care how Perl happens to encode its internal strings, but it becomes relevant when -outputting Unicode strings to a stream without a discipline--one with +outputting Unicode strings to a stream without a PerlIO layer -- one with the "default" encoding. In such a case, the raw bytes used internally (the native character set or UTF-8, as appropriate for each string) will be used, and a "Wide character" warning will be issued if those @@ -165,21 +165,27 @@ as a warning: Wide character in print at ... -To output UTF-8, use the C<:utf8> output discipline. Prepending +To output UTF-8, use the C<:utf8> output layer. Prepending binmode(STDOUT, ":utf8"); to this sample program ensures that the output is completely UTF-8, and removes the program's warning. -If your locale environment variables (C, C, -C, C) contain the strings 'UTF-8' or 'UTF8', -regardless of case, then the default encoding of your STDIN, STDOUT, -and STDERR and of B, is UTF-8. Note that -this means that Perl expects other software to work, too: if Perl has -been led to believe that STDIN should be UTF-8, but then STDIN coming -in from another command is not UTF-8, Perl will complain about the -malformed UTF-8. +You can enable automatic UTF-8-ification of your standard file +handles, default C layer, and C<@ARGV> by using either +the C<-C> command line switch or the C environment +variable, see L for the documentation of the C<-C> switch. + +Note that this means that Perl expects other software to work, too: +if Perl has been led to believe that STDIN should be UTF-8, but then +STDIN coming in from another command is not UTF-8, Perl will complain +about the malformed UTF-8. + +All features that combine Unicode and I/O also require using the new +PerlIO feature. Almost all Perl 5.8 platforms do use PerlIO, though: +you can see whether yours is by running "perl -V" and looking for +C. =head2 Unicode and EBCDIC @@ -323,7 +329,7 @@ and on already open streams, use C: binmode(STDOUT, ":encoding(shift_jis)"); The matching of encoding names is loose: case does not matter, and -many encodings have several aliases. Note that C<:utf8> discipline +many encodings have several aliases. Note that the C<:utf8> layer must always be specified exactly like that; it is I subject to the loose matching of encoding names. @@ -335,7 +341,7 @@ module. Reading in a file that you know happens to be encoded in one of the Unicode or legacy encodings does not magically turn the data into Unicode in Perl's eyes. To do that, specify the appropriate -discipline when opening files +layer when opening files open(my $fh,'<:utf8', 'anything'); my $line_of_unicode = <$fh>; @@ -343,10 +349,10 @@ discipline when opening files open(my $fh,'<:encoding(Big5)', 'anything'); my $line_of_unicode = <$fh>; -The I/O disciplines can also be specified more flexibly with +The I/O layers can also be specified more flexibly with the C pragma. See L, or look at the following example. - use open ':utf8'; # input and output default discipline will be UTF-8 + use open ':utf8'; # input and output default layer will be UTF-8 open X, ">file"; print X chr(0x100), "\n"; close X; @@ -354,9 +360,9 @@ the C pragma. See L, or look at the following example. printf "%#x\n", ord(); # this should print 0x100 close Y; -With the C pragma you can use the C<:locale> discipline +With the C pragma you can use the C<:locale> layer - $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R'; + BEGIN { $ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R' } # the :locale will probe the locale environment variables like LC_ALL use open OUT => ':locale'; # russki parusski open(O, ">koi8"); @@ -366,7 +372,7 @@ With the C pragma you can use the C<:locale> discipline printf "%#x\n", ord(), "\n"; # this should print 0xc1 close I; -or you can also use the C<':encoding(...)'> discipline +or you can also use the C<':encoding(...)'> layer open(my $epic,'<:encoding(iso-8859-7)','iliad.greek'); my $line_of_unicode = <$epic>; @@ -376,8 +382,8 @@ converts data from the specified encoding when it is read in from the stream. The result is always Unicode. The L pragma affects all the C calls after the pragma by -setting default disciplines. If you want to affect only certain -streams, use explicit disciplines directly in the C call. +setting default layers. If you want to affect only certain +streams, use explicit layers directly in the C call. You can switch encodings on an already opened stream by using C; see L. @@ -387,15 +393,15 @@ C and C, only with the C pragma. The C<:utf8> and C<:encoding(...)> methods do work with all of C, C, and the C pragma. -Similarly, you may use these I/O disciplines on output streams to +Similarly, you may use these I/O layers on output streams to automatically convert Unicode to the specified encoding when it is written to the stream. For example, the following snippet copies the contents of the file "text.jis" (encoded as ISO-2022-JP, aka JIS) to the file "text.utf8", encoded as UTF-8: - open(my $nihongo, '<:encoding(iso2022-jp)', 'text.jis'); - open(my $unicode, '>:utf8', 'text.utf8'); - while (<$nihongo>) { print $unicode } + open(my $nihongo, '<:encoding(iso-2022-jp)', 'text.jis'); + open(my $unicode, '>:utf8', 'text.utf8'); + while (<$nihongo>) { print $unicode $_ } The naming of encodings, both by the C and by the C pragma, is similar to the C pragma in that it allows for @@ -410,7 +416,7 @@ C and C operate on byte counts, as do C and C. Notice that because of the default behaviour of not doing any -conversion upon input if there is no default discipline, +conversion upon input if there is no default layer, it is easy to mistakenly write code that keeps on expanding a file by repeatedly encoding the data: @@ -428,9 +434,8 @@ UTF-8 encoded. A C would have avoided the bug, or explicitly opening also the F for input as UTF-8. B: the C<:utf8> and C<:encoding> features work only if your -Perl has been built with the new "perlio" feature. Almost all -Perl 5.8 platforms do use "perlio", though: you can see whether -yours is by running "perl -V" and looking for C. +Perl has been built with the new PerlIO feature (which is the default +on most systems). =head2 Displaying Unicode As Text @@ -446,7 +451,7 @@ displayed as C<\x..>, and the rest of the characters as themselves: sprintf("\\x{%04X}", $_) : # \x{...} chr($_) =~ /[[:cntrl:]]/ ? # else if control character ... sprintf("\\x%02X", $_) : # \x.. - chr($_) # else as themselves + quotemeta(chr($_)) # else quoted or as themselves } unpack("U*", $_[0])); # unpack Unicode characters } @@ -454,9 +459,11 @@ For example, nice_string("foo\x{100}bar\n") -returns: +returns the string + + 'foo\x{0100}bar\x0A' - "foo\x{0100}bar\x0A" +which is ready to be printed. =head2 Special Cases @@ -481,7 +488,7 @@ Peeking At Perl's Internal Encoding Normal users of Perl should never care how Perl encodes any particular Unicode string (because the normal ways to get at the contents of a string with Unicode--via input and output--should always be via -explicitly-defined I/O disciplines). But if you must, there are two +explicitly-defined I/O layers). But if you must, there are two ways of looking behind the scenes. One way of peeking inside the internal encoding of Unicode characters @@ -497,7 +504,7 @@ Yet another way would be to use the Devel::Peek module: That shows the UTF8 flag in FLAGS and both the UTF-8 bytes and Unicode characters in C. See also later in this document -the discussion about the C function of the C module. +the discussion about the C function. =back @@ -588,7 +595,7 @@ than ASCII 0 to 9 (and ASCII a to f for hexadecimal). =over 4 -=item +=item * Will My Old Scripts Break? @@ -600,7 +607,7 @@ produced a character modulo 255. C, for example, was equal to C or "-" (in ASCII), now it is LATIN CAPITAL LETTER I WITH BREVE. -=item +=item * How Do I Make My Scripts Work With Unicode? @@ -608,7 +615,7 @@ Very little work should be needed since nothing changes until you generate Unicode data. The most important thing is getting input as Unicode; for that, see the earlier I/O discussion. -=item +=item * How Do I Know Whether My String Is In Unicode? @@ -618,8 +625,7 @@ didn't get the transparency of Unicode quite right. Okay, if you insist: - use Encode 'is_utf8'; - print is_utf8($string) ? 1 : 0, "\n"; + print utf8::is_utf8($string) ? 1 : 0, "\n"; But note that this doesn't mean that any of the characters in the string are necessary UTF-8 encoded, or that any of the characters have @@ -655,7 +661,7 @@ defined function C: print length($unicode), "\n"; # will also print 2 # (the 0xC4 0x80 of the UTF-8) -=item +=item * How Do I Detect Data That's Not Valid In a Particular Encoding? @@ -679,7 +685,7 @@ warning is produced. The "U0" means "expect strictly UTF-8 encoded Unicode". Without that the C would accept also data like C), similarly to the C as we saw earlier. -=item +=item * How Do I Convert Binary Data Into a Particular Encoding, Or Vice Versa? @@ -734,14 +740,14 @@ B. You can use C for the former, and you can create well-formed Unicode data by C. -=item +=item * How Do I Display Unicode? How Do I Input Unicode? See http://www.alanwood.net/unicode/ and http://www.cl.cam.ac.uk/~mgk25/unicode.html -=item +=item * How Does Unicode Work With Traditional Locales? @@ -859,7 +865,7 @@ If you have the GNU recode installed, you can also use the Perl front-end C for character conversions. The following are fast conversions from ISO 8859-1 (Latin-1) bytes -to UTF-8 bytes, the code works even with older Perl 5 versions. +to UTF-8 bytes and back, the code works even with older Perl 5 versions. # ISO 8859-1 to UTF-8 s/([\x80-\xFF])/chr(0xC0|ord($1)>>6).chr(0x80|ord($1)&0x3F)/eg; @@ -880,6 +886,6 @@ mailing lists for their valuable feedback. =head1 AUTHOR, COPYRIGHT, AND LICENSE -Copyright 2001-2002 Jarkko Hietaniemi +Copyright 2001-2002 Jarkko Hietaniemi Ejhi@iki.fiE This document may be distributed under the same terms as Perl itself.