diff options
author | Audrey Tang <cpan@audreyt.org> | 2003-12-10 04:39:16 +0800 |
---|---|---|
committer | Rafael Garcia-Suarez <rgarciasuarez@gmail.com> | 2003-12-09 21:33:22 +0000 |
commit | 990e18f721a7d2ee48d50ea4262bd5d109e9f89c (patch) | |
tree | 4334f1521da4188b6bebdae304b622e37030f8ca | |
parent | 4e2344ada78d8742c0023d545c1baed6597bae39 (diff) | |
download | perl-990e18f721a7d2ee48d50ea4262bd5d109e9f89c.tar.gz |
Implicit upgrading docs
Message-ID: <20031209123915.GA1454@not.autrijus.org>
p4raw-id: //depot/perl@21873
-rw-r--r-- | ext/Encode/encoding.pm | 19 | ||||
-rw-r--r-- | pod/perlunicode.pod | 27 |
2 files changed, 40 insertions, 6 deletions
diff --git a/ext/Encode/encoding.pm b/ext/Encode/encoding.pm index f203cb3d7e..93662524fa 100644 --- a/ext/Encode/encoding.pm +++ b/ext/Encode/encoding.pm @@ -192,6 +192,25 @@ not "\x{99F1}\x{99DD} is the symbol of perl.\n". You can override this by giving extra arguments; see below. +=head2 Implicit upgrading for byte strings + +By default, if strings operating under byte semantics and strings +with Unicode character data are concatenated, the new string will +be created by decoding the byte strings as I<ISO 8859-1 (Latin-1)>. + +The B<encoding> pragma changes this to use the specified encoding +instead. For example: + + use encoding 'utf8'; + my $string = chr(20000); # a Unicode string + utf8::encode($string); # now it's a UTF-8 encoded byte string + # concatenate with another Unicode string + print length($string . chr(20000)); + +Will print C<2>, because C<$string> is upgraded as UTF-8. Without +C<use encoding 'utf8';>, it will print C<4> instead, since C<$string> +is three octets when interpreted as Latin-1. + =head1 FEATURES THAT REQUIRE 5.8.1 Some of the features offered by this pragma requires perl 5.8.1. Most diff --git a/pod/perlunicode.pod b/pod/perlunicode.pod index 190247aea7..b6d00d1660 100644 --- a/pod/perlunicode.pod +++ b/pod/perlunicode.pod @@ -42,6 +42,21 @@ is needed.> See L<utf8>. You can also use the C<encoding> pragma to change the default encoding of the data in your script; see L<encoding>. +=item C<use encoding> needed to upgrade non-Latin-1 byte strings + +By default, there is a fundamental asymmetry in Perl's unicode model: +implicit upgrading from byte strings to Unicode strings assumes that +they were encoded in I<ISO 8859-1 (Latin-1)>, but Unicode strings are +downgraded with UTF-8 encoding. This happens because the first 256 +codepoints in Unicode happens to agree with Latin-1. + +If you wish to interpret byte strings as UTF-8 instead, use the +C<encoding> pragma: + + use encoding 'utf8'; + +See L</"Byte and Character Semantics"> for more details. + =back =head2 Byte and Character Semantics @@ -86,12 +101,12 @@ Otherwise, byte semantics are in effect. The C<bytes> pragma should be used to force byte semantics on Unicode data. If strings operating under byte semantics and strings with Unicode -character data are concatenated, the new string will be upgraded to -I<ISO 8859-1 (Latin-1)>, even if the old Unicode string used EBCDIC. -This translation is done without regard to the system's native 8-bit -encoding, so to change this for systems with non-Latin-1 and -non-EBCDIC native encodings use the C<encoding> pragma. See -L<encoding>. +character data are concatenated, the new string will be created by +decoding the byte strings as I<ISO 8859-1 (Latin-1)>, even if the +old Unicode string used EBCDIC. This translation is done without +regard to the system's native 8-bit encoding. To change this for +systems with non-Latin-1 and non-EBCDIC native encodings, use the +C<encoding> pragma. See L<encoding>. Under character semantics, many operations that formerly operated on bytes now operate on characters. A character in Perl is |