perluniintro.pod

来自「视频监控网络部分的协议ddns,的模块的实现代码,请大家大胆指正.」· POD 代码 · 共 886 行 · 第 1/3 页

POD
886
字号
=head1 NAMEperluniintro - Perl Unicode introduction=head1 DESCRIPTIONThis document gives a general idea of Unicode and how to use Unicodein Perl.=head2 UnicodeUnicode is a character set standard which plans to codify all of thewriting systems of the world, plus many other symbols.Unicode and ISO/IEC 10646 are coordinated standards that provide codepoints for characters in almost all modern character set standards,covering more than 30 writing systems and hundreds of languages,including all commercially-important modern languages.  All charactersin the largest Chinese, Japanese, and Korean dictionaries are alsoencoded. The standards will eventually cover almost all characters inmore than 250 writing systems and thousands of languages.Unicode 1.0 was released in October 1991, and 4.0 in April 2003.A Unicode I<character> is an abstract entity.  It is not bound to anyparticular integer width, especially not to the C language C<char>.Unicode is language-neutral and display-neutral: it does not encode thelanguage of the text and it does not define fonts or other graphicallayout details.  Unicode operates on characters and on text built fromthose characters.Unicode defines characters like C<LATIN CAPITAL LETTER A> or C<GREEKSMALL LETTER ALPHA> and unique numbers for the characters, in thiscase 0x0041 and 0x03B1, respectively.  These unique numbers are calledI<code points>.The Unicode standard prefers using hexadecimal notation for the codepoints.  If numbers like C<0x0041> are unfamiliar to you, take a peekat a later section, L</"Hexadecimal Notation">.  The Unicode standarduses the notation C<U+0041 LATIN CAPITAL LETTER A>, to give thehexadecimal code point and the normative name of the character.Unicode also defines various I<properties> for the characters, like"uppercase" or "lowercase", "decimal digit", or "punctuation";these properties are independent of the names of the characters.Furthermore, various operations on the characters like uppercasing,lowercasing, and collating (sorting) are defined.A Unicode character consists either of a single code point, or aI<base character> (like C<LATIN CAPITAL LETTER A>), followed by one ormore I<modifiers> (like C<COMBINING ACUTE ACCENT>).  This sequence ofbase character and modifiers is called a I<combining charactersequence>.Whether to call these combining character sequences "characters"depends on your point of view. If you are a programmer, you probablywould tend towards seeing each element in the sequences as one unit,or "character".  The whole sequence could be seen as one "character",however, from the user's point of view, since that's probably what itlooks like in the context of the user's language.With this "whole sequence" view of characters, the total number ofcharacters is open-ended. But in the programmer's "one unit is onecharacter" point of view, the concept of "characters" is moredeterministic.  In this document, we take that second  point of view:one "character" is one Unicode code point, be it a base character ora combining character.For some combinations, there are I<precomposed> characters.C<LATIN CAPITAL LETTER A WITH ACUTE>, for example, is defined asa single code point.  These precomposed characters are, however,only available for some combinations, and are mainlymeant to support round-trip conversions between Unicode and legacystandards (like the ISO 8859).  In the general case, the composingmethod is more extensible.  To support conversion betweendifferent compositions of the characters, various I<normalizationforms> to standardize representations are also defined.Because of backward compatibility with legacy encodings, the "a uniquenumber for every character" idea breaks down a bit: instead, there is"at least one number for every character".  The same character couldbe represented differently in several legacy encodings.  Theconverse is also not true: some code points do not have an assignedcharacter.  Firstly, there are unallocated code points withinotherwise used blocks.  Secondly, there are special Unicode controlcharacters that do not represent true characters.A common myth about Unicode is that it would be "16-bit", that is,Unicode is only represented as C<0x10000> (or 65536) characters fromC<0x0000> to C<0xFFFF>.  B<This is untrue.>  Since Unicode 2.0 (July1996), Unicode has been defined all the way up to 21 bits (C<0x10FFFF>),and since Unicode 3.1 (March 2001), characters have been definedbeyond C<0xFFFF>.  The first C<0x10000> characters are called theI<Plane 0>, or the I<Basic Multilingual Plane> (BMP).  With Unicode3.1, 17 (yes, seventeen) planes in all were defined--but they arenowhere near full of defined characters, yet.Another myth is that the 256-character blocks have something todo with languages--that each block would define the characters usedby a language or a set of languages.  B<This is also untrue.>The division into blocks exists, but it is almost completelyaccidental--an artifact of how the characters have been andstill are allocated.  Instead, there is a concept called I<scripts>,which is more useful: there is C<Latin> script, C<Greek> script, andso on.  Scripts usually span varied parts of several blocks.For further information see L<Unicode::UCD>.The Unicode code points are just abstract numbers.  To input andoutput these abstract numbers, the numbers must be I<encoded> orI<serialised> somehow.  Unicode defines several I<character encodingforms>, of which I<UTF-8> is perhaps the most popular.  UTF-8 is avariable length encoding that encodes Unicode characters as 1 to 6bytes (only 4 with the currently defined characters).  Other encodingsinclude UTF-16 and UTF-32 and their big- and little-endian variants(UTF-8 is byte-order independent) The ISO/IEC 10646 defines the UCS-2and UCS-4 encoding forms.For more information about encodings--for instance, to learn whatI<surrogates> and I<byte order marks> (BOMs) are--see L<perlunicode>.=head2 Perl's Unicode SupportStarting from Perl 5.6.0, Perl has had the capacity to handle Unicodenatively.  Perl 5.8.0, however, is the first recommended release forserious Unicode work.  The maintenance release 5.6.1 fixed many of theproblems of the initial Unicode implementation, but for exampleregular expressions still do not work with Unicode in 5.6.1.B<Starting from Perl 5.8.0, the use of C<use utf8> is no longernecessary.> In earlier releases the C<utf8> pragma was used to declarethat operations in the current block or file would be Unicode-aware.This model was found to be wrong, or at least clumsy: the "Unicodeness"is now carried with the data, instead of being attached to theoperations.  Only one case remains where an explicit C<use utf8> isneeded: if your Perl script itself is encoded in UTF-8, you can useUTF-8 in your identifier names, and in string and regular expressionliterals, by saying C<use utf8>.  This is not the default becausescripts with legacy 8-bit data in them would break.  See L<utf8>.=head2 Perl's Unicode ModelPerl supports both pre-5.6 strings of eight-bit native bytes, andstrings of Unicode characters.  The principle is that Perl tries tokeep its data as eight-bit bytes for as long as possible, but as soonas Unicodeness cannot be avoided, the data is transparently upgradedto Unicode.Internally, Perl currently uses either whatever the native eight-bitcharacter set of the platform (for example Latin-1) is, defaulting toUTF-8, to encode Unicode strings. Specifically, if all code points inthe string are C<0xFF> or less, Perl uses the native eight-bitcharacter set.  Otherwise, it uses UTF-8.A user of Perl does not normally need to know nor care how Perlhappens to encode its internal strings, but it becomes relevant whenoutputting Unicode strings to a stream without a PerlIO layer -- one withthe "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 thosestrings contain a character beyond 0x00FF.For example,      perl -e 'print "\x{DF}\n", "\x{0100}\x{DF}\n"'produces a fairly useless mixture of native bytes and UTF-8, as wellas a warning:     Wide character in print at ...To output UTF-8, use the C<:encoding> or 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.You can enable automatic UTF-8-ification of your standard filehandles, default C<open()> layer, and C<@ARGV> by using eitherthe C<-C> command line switch or the C<PERL_UNICODE> environmentvariable, see L<perlrun> 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 thenSTDIN coming in from another command is not UTF-8, Perl will complainabout the malformed UTF-8.All features that combine Unicode and I/O also require using the newPerlIO feature.  Almost all Perl 5.8 platforms do use PerlIO, though:you can see whether yours is by running "perl -V" and looking forC<useperlio=define>.=head2 Unicode and EBCDICPerl 5.8.0 also supports Unicode on EBCDIC platforms.  There,Unicode support is somewhat more complex to implement sinceadditional conversions are needed at every step.  Some problemsremain, see L<perlebcdic> for details.In any case, the Unicode support on EBCDIC platforms is better thanin the 5.6 series, which didn't work much at all for EBCDIC platform.On EBCDIC platforms, the internal Unicode encoding form is UTF-EBCDICinstead of UTF-8.  The difference is that as UTF-8 is "ASCII-safe" inthat ASCII characters encode to UTF-8 as-is, while UTF-EBCDIC is"EBCDIC-safe".=head2 Creating UnicodeTo create Unicode characters in literals for code points above C<0xFF>,use the C<\x{...}> notation in double-quoted strings:    my $smiley = "\x{263a}";Similarly, it can be used in regular expression literals    $smiley =~ /\x{263a}/;At run-time you can use C<chr()>:    my $hebrew_alef = chr(0x05d0);See L</"Further Resources"> for how to find all these numeric codes.Naturally, C<ord()> will do the reverse: it turns a character intoa code point.Note that C<\x..> (no C<{}> and only two hexadecimal digits), C<\x{...}>,and C<chr(...)> for arguments less than C<0x100> (decimal 256)generate an eight-bit character for backward compatibility with olderPerls.  For arguments of C<0x100> or more, Unicode characters arealways produced. If you want to force the production of Unicodecharacters regardless of the numeric value, use C<pack("U", ...)>instead of C<\x..>, C<\x{...}>, or C<chr()>.You can also use the C<charnames> pragma to invoke charactersby name in double-quoted strings:    use charnames ':full';    my $arabic_alef = "\N{ARABIC LETTER ALEF}";And, as mentioned above, you can also C<pack()> numbers into Unicodecharacters:   my $georgian_an  = pack("U", 0x10a0);Note that both C<\x{...}> and C<\N{...}> are compile-time stringconstants: you cannot use variables in them.  if you want similarrun-time functionality, use C<chr()> and C<charnames::vianame()>.If you want to force the result to Unicode characters, use the specialC<"U0"> prefix.  It consumes no arguments but causes the following bytesto be interpreted as the UTF-8 encoding of Unicode characters:   my $chars = pack("U0W*", 0x80, 0x42);Likewise, you can stop such UTF-8 interpretation by using the specialC<"C0"> prefix.=head2 Handling UnicodeHandling Unicode is for the most part transparent: just use thestrings as usual.  Functions like C<index()>, C<length()>, andC<substr()> will work on the Unicode characters; regular expressionswill work on the Unicode characters (see L<perlunicode> and L<perlretut>).Note that Perl considers combining character sequences to beseparate characters, so for example    use charnames ':full';    print length("\N{LATIN CAPITAL LETTER A}\N{COMBINING ACUTE ACCENT}"), "\n";will print 2, not 1.  The only exception is that regular expressionshave C<\X> for matching a combining character sequence.Life is not quite so transparent, however, when working with legacyencodings, I/O, and certain special cases:=head2 Legacy EncodingsWhen you combine legacy data and Unicode the legacy data needsto be upgraded to Unicode.  Normally ISO 8859-1 (or EBCDIC, ifapplicable) is assumed.The C<Encode> module knows about many encodings and has interfacesfor doing conversions between those encodings:    use Encode 'decode';    $data = decode("iso-8859-3", $data); # convert from legacy to utf-8=head2 Unicode I/ONormally, writing out Unicode data    print FH $some_string_with_unicode, "\n";produces raw bytes that Perl happens to use to internally encode theUnicode string.  Perl's internal encoding depends on the system as

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?