I<code points>.
The Unicode standard prefers using hexadecimal notation for the code
-points. If numbers like "C<0x0041" are unfamiliar to
+points. If numbers like C<0x0041> are unfamiliar to
you, take a peek at a later section, L</"Hexadecimal Notation">.
The Unicode standard uses the notation C<U+0041 LATIN CAPITAL LETTER A>,
to give the hexadecimal code point and the normative name of
With this "whole sequence" view of characters, the total number of
characters is open-ended. But in the programmer's "one unit is one
character" point of view, the concept of "characters" is more
-deterministic. In this document, we take that second point of view: one
-"character" is one Unicode code point, be it a base character or a
-combining character.
+deterministic. In this document, we take that second point of view:
+one "character" is one Unicode code point, be it a base character or
+a combining character.
For some combinations, there are I<precomposed> characters.
C<LATIN CAPITAL LETTER A WITH ACUTE>, for example, is defined as
Multilingual Plane> (BMP). With Unicode 3.1, 17 planes in all are
defined--but nowhere near full of defined characters, yet.
-Another myth is that the 256-character blocks have something to do
-with languages--that each languages is specified inside a block.
-B<This is also untrue.> The division into blocks exists, but it is
-almost completely accidental--an artifact of how the characters have
-been historically allocated. Instead, there is a concept called
-I<scripts>, which is more useful: there is C<Latin> script,
-C<Greek> script, and so on. Scripts usually span varied parts of
-several blocks. For further information see L<Unicode::UCD>.
+Another myth is that the 256-character blocks have something to
+do with languages--that each block would define the characters used
+by a language or a set of languages. B<This is also untrue.>
+The division into blocks exists, but it is almost completely
+accidental--an artifact of how the characters have been and
+still are allocated. Instead, there is a concept called I<scripts>,
+which is more useful: there is C<Latin> script, C<Greek> script, and
+so 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 and
output these abstract numbers, the numbers must be I<encoded> somehow.
is perhaps the most popular. UTF-8 is a variable length encoding that
encodes Unicode characters as 1 to 6 bytes (only 4 with the currently
defined characters). Other encodings include UTF-16 and UTF-32 and their
-big- and little-endian variants (UTF-8 is byte-order independent).
+big- and little-endian variants (UTF-8 is byte-order independent)
The ISO/IEC 10646 defines the UCS-2 and UCS-4 encoding forms.
For more information about encodings--for instance, to learn what
natively. Perl 5.8.0, however, is the first recommended release for
serious Unicode work. The maintenance release 5.6.1 fixed many of the
problems of the initial Unicode implementation, but for example
-regular expressions did not really work with Unicode.
+regular 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 longer
necessary.> In earlier releases the C<utf8> pragma was used to declare
that 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 the
+is now carried with the data, instead of being attached to the
operations. Only one case remains where an explicit C<use utf8> 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<use utf8>. 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<utf8>.
=head2 Perl's Unicode Model
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
perl -e 'print "\x{DF}\n", "\x{0100}\x{DF}\n"'
produces a fairly useless mixture of native bytes and UTF-8, as well
-as a warning.
+as a warning:
-To output UTF-8 always, use the "C<:utf8>" output discipline. Prepending
+ Wide character in print at ...
+
+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 helpfully removes the program's warning.
+and removes the program's warning.
If your locale environment variables (C<LANGUAGE>, C<LC_ALL>,
C<LC_CTYPE>, C<LANG>) contain the strings 'UTF-8' or 'UTF8',
regardless of case, then the default encoding of your STDIN, STDOUT,
and STDERR and of B<any subsequent file open>, is UTF-8. Note that
-this means that Perl expects other software to work, too: if STDIN
-coming in from another command is not UTF-8, Perl will complain about
+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<useperlio=define>.
+
=head2 Unicode and EBCDIC
Perl 5.8.0 also supports Unicode on EBCDIC platforms. There,
Naturally, C<ord()> will do the reverse: it turns a character into
a 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
-older Perls. For arguments of C<0x100> or more, Unicode characters are
+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 older
+Perls. For arguments of C<0x100> or more, Unicode characters are
always produced. If you want to force the production of Unicode
characters regardless of the numeric value, use C<pack("U", ...)>
instead of C<\x..>, C<\x{...}>, or C<chr()>.
C<substr()> will work on the Unicode characters; regular expressions
will work on the Unicode characters (see L<perlunicode> and L<perlretut>).
-Note that Perl considers combining character sequences
-to be characters, so for example
+Note that Perl considers combining character sequences to be
+characters, so for example
use charnames ':full';
print length("\N{LATIN CAPITAL LETTER A}\N{COMBINING ACUTE ACCENT}"), "\n";
use encoding 'latin2'; # ISO 8859-2
-in which case literals (string or regular expressions), C<chr()>, and
-C<ord()> in your whole script are assumed to produce Unicode
+in which case literals (string or regular expressions), C<chr()>,
+and C<ord()> in your whole script are assumed to produce Unicode
characters from ISO 8859-2 code points. Note that the matching for
encoding names is forgiving: instead of C<latin2> you could have
said C<Latin 2>, or C<iso8859-2>, or other variations. With just
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
-must always be specified exactly like that; it is I<not> subject to the
-loose matching of encoding names.
+many encodings have several aliases. Note that the C<:utf8> layer
+must always be specified exactly like that; it is I<not> subject to
+the loose matching of encoding names.
See L<PerlIO> for the C<:utf8> layer, L<PerlIO::encoding> and
L<Encode::PerlIO> for the C<:encoding()> layer, and
module.
Reading in a file that you know happens to be encoded in one of the
-Unicode encodings does not magically turn the data into Unicode in
-Perl's eyes. To do that, specify the appropriate discipline when
-opening files
+Unicode or legacy encodings does not magically turn the data into
+Unicode in Perl's eyes. To do that, specify the appropriate
+layer when opening files
open(my $fh,'<:utf8', 'anything');
my $line_of_unicode = <$fh>;
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<open> pragma. See L<open>, 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;
printf "%#x\n", ord(<Y>); # this should print 0x100
close Y;
-With the C<open> pragma you can use the C<:locale> discipline
+With the C<open> pragma you can use the C<:locale> layer
$ENV{LC_ALL} = $ENV{LANG} = 'ru_RU.KOI8-R';
# the :locale will probe the locale environment variables like LC_ALL
printf "%#x\n", ord(<I>), "\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>;
stream. The result is always Unicode.
The L<open> pragma affects all the C<open()> calls after the pragma by
-setting default disciplines. If you want to affect only certain
-streams, use explicit disciplines directly in the C<open()> call.
+setting default layers. If you want to affect only certain
+streams, use explicit layers directly in the C<open()> call.
You can switch encodings on an already opened stream by using
C<binmode()>; see L<perlfunc/binmode>.
C<:utf8> and C<:encoding(...)> methods do work with all of C<open()>,
C<binmode()>, and the C<open> 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
Common encodings recognized by ISO, MIME, IANA, and various other
standardisation organisations are recognised; for a more detailed
-list see L<Encode>.
+list see L<Encode::Supported>.
C<read()> reads characters and returns the number of characters.
C<seek()> and C<tell()> operate on byte counts, as do C<sysread()>
and C<sysseek()>.
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:
+by repeatedly encoding the data:
# BAD CODE WARNING
open F, "file";
explicitly opening also the F<file> for input as UTF-8.
B<NOTE>: 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<useperlio=define>.
+Perl has been built with the new PerlIO feature.
=head2 Displaying Unicode As Text
Sometimes you might want to display Perl scalars containing Unicode as
simple ASCII (or EBCDIC) text. The following subroutine converts
its argument so that Unicode characters with code points greater than
-255 are displayed as "C<\x{...}>", control characters (like "C<\n>") are
-displayed as "C<\x..>", and the rest of the characters as themselves:
+255 are displayed as C<\x{...}>, control characters (like C<\n>) are
+displayed as C<\x..>, and the rest of the characters as themselves:
sub nice_string {
join("",
Bit Complement Operator ~ And vec()
-The bit complement operator C<~> may produce surprising results if used on
-strings containing characters with ordinal values above 255. In such a
-case, the results are consistent with the internal encoding of the
-characters, but not with much else. So don't do that. Similarly for C<vec()>:
-you will be operating on the internally-encoded bit patterns of the Unicode
-characters, not on the code point values, which is very probably not what
-you want.
+The bit complement operator C<~> may produce surprising results if
+used on strings containing characters with ordinal values above
+255. In such a case, the results are consistent with the internal
+encoding of the characters, but not with much else. So don't do
+that. Similarly for C<vec()>: you will be operating on the
+internally-encoded bit patterns of the Unicode characters, not on
+the code point values, which is very probably not what you want.
=item *
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
Mappings>, http://www.unicode.org/unicode/reports/tr15/ and
http://www.unicode.org/unicode/reports/tr21/
-As of Perl 5.8.0, regular expression case-ignoring matching
-implements only 1:1 semantics: one character matches one character.
-In I<Case Mappings> both 1:N and N:1 matches are defined.
+As of Perl 5.8.0, the "Full" case-folding of I<Case
+Mappings/SpecialCasing> is implemented.
=item *
The short answer is that by default, Perl compares strings (C<lt>,
C<le>, C<cmp>, C<ge>, C<gt>) based only on the code points of the
-characters. In the above case, the answer is "after", since C<0x00C1>
-> C<0x00C0>.
+characters. In the above case, the answer is "after", since
+C<0x00C1> > C<0x00C0>.
The long answer is that "it depends", and a good answer cannot be
given without knowing (at the very least) the language context.
to mean "all alphabetic letters"; not that it does mean that even for
8-bit characters, you should be using C</[[:alpha:]]/> in that case.
-For specifying character classes like that in regular expressions, you can use
-the various Unicode properties--C<\pL>, or perhaps C<\p{Alphabetic}>,
-in this particular case. You can use Unicode code points as the end
-points of character ranges, but there is no magic associated with
-specifying a certain range. For further information--there are dozens
-of Unicode character classes--see L<perlunicode>.
+For specifying character classes like that in regular expressions,
+you can use the various Unicode properties--C<\pL>, or perhaps
+C<\p{Alphabetic}>, in this particular case. You can use Unicode
+code points as the end points of character ranges, but there is no
+magic associated with specifying a certain range. For further
+information--there are dozens of Unicode character classes--see
+L<perlunicode>.
=item *
=over 4
-=item
+=item *
Will My Old Scripts Break?
Very probably not. Unless you are generating Unicode characters
-somehow, old behaviour should be preserved. About the only
-behaviour that has changed and which could start generating Unicode
-is the old behaviour of C<chr()> where supplying an argument more
-than 255 produced a character modulo 255. C<chr(300)>, for example,
-was equal to C<chr(45)>.
+somehow, old behaviour should be preserved. About the only behaviour
+that has changed and which could start generating Unicode is the old
+behaviour of C<chr()> where supplying an argument more than 255
+produced a character modulo 255. C<chr(300)>, for example, was equal
+to C<chr(45)> or "-" (in ASCII), now it is LATIN CAPITAL LETTER I WITH
+BREVE.
-=item
+=item *
How Do I Make My Scripts Work With Unicode?
Very little work should be needed since nothing changes until you
-generate Unicode data. The trick is getting input as Unicode; for
-that, see the earlier I/O discussion.
+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?
-You shouldn't care. No, you really shouldn't. No, really. If you have
-to care--beyond the cases described above--it means that we
+You shouldn't care. No, you really shouldn't. No, really. If you
+have to care--beyond the cases described above--it means that we
didn't get the transparency of Unicode quite right.
Okay, if you insist:
$b = "\x{100}";
print "$a = $b\n";
-the output string will be UTF-8-encoded "C<ab\x80c\x{100}\n>", but note
+the output string will be UTF-8-encoded C<ab\x80c\x{100}\n>, but note
that C<$a> will stay byte-encoded.
Sometimes you might really need to know the byte length of a string
require Encode;
print length(Encode::encode_utf8($unicode)), "\n"; # will print 2
use bytes;
- print length($unicode), "\n"; # will also print 2 (the 0xC4 0x80 of the UTF-8)
+ 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?
use warnings;
@chars = unpack("U0U*", $string_of_bytes_that_I_think_is_utf8);
-If invalid, a C<Malformed UTF-8 character (byte 0x##) in
-unpack> warning is produced. The "U0" means "expect strictly UTF-8
-encoded Unicode". Without that the C<unpack("U*", ...)>
-would accept also data like C<chr(0xFF>), similarly to the
-C<pack> as we saw earlier.
+If invalid, a C<Malformed UTF-8 character (byte 0x##) in unpack>
+warning is produced. The "U0" means "expect strictly UTF-8 encoded
+Unicode". Without that the C<unpack("U*", ...)> would accept also
+data like C<chr(0xFF>), similarly to the C<pack> as we saw earlier.
-=item
+=item *
How Do I Convert Binary Data Into a Particular Encoding, Or Vice Versa?
This probably isn't as useful as you might think.
Normally, you shouldn't need to.
-In one sense, what you are asking doesn't make much sense: Encodings
+In one sense, what you are asking doesn't make much sense: encodings
are for characters, and binary data are not "characters", so converting
"data" into some encoding isn't meaningful unless you know in what
character set and encoding the binary data is in, in which case it's
not just binary data, now is it?
-If you have a raw sequence of bytes that you know should be interpreted via
-a particular encoding, you can use C<Encode>:
+If you have a raw sequence of bytes that you know should be
+interpreted via a particular encoding, you can use C<Encode>:
use Encode 'from_to';
from_to($data, "iso-8859-1", "utf-8"); # from latin-1 to utf-8
-The call to C<from_to()> changes the bytes in C<$data>, but nothing material
-about the nature of the string has changed as far as Perl is concerned.
-Both before and after the call, the string C<$data> contains just a bunch of
-8-bit bytes. As far as Perl is concerned, the encoding of the string
-remains as "system-native 8-bit bytes".
+The call to C<from_to()> changes the bytes in C<$data>, but nothing
+material about the nature of the string has changed as far as Perl is
+concerned. Both before and after the call, the string C<$data>
+contains just a bunch of 8-bit bytes. As far as Perl is concerned,
+the encoding of the string remains as "system-native 8-bit bytes".
You might relate this to a fictional 'Translate' module:
## phrase now contains "Ja"
The contents of the string changes, but not the nature of the string.
-Perl doesn't know any more after the call than before that the contents
-of the string indicates the affirmative.
+Perl doesn't know any more after the call than before that the
+contents of the string indicates the affirmative.
Back to converting data. If you have (or want) data in your system's
native 8-bit encoding (e.g. Latin-1, EBCDIC, etc.), you can use
You can convert well-formed UTF-8 to a sequence of bytes, but if
you just want to convert random binary data into UTF-8, you can't.
-Any random collection of bytes isn't well-formed UTF-8. You can
+B<Any random collection of bytes isn't well-formed UTF-8>. You can
use C<unpack("C*", $string)> for the former, and you can create
well-formed Unicode data by C<pack("U*", 0xff, ...)>.
-=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?
that more clearly shows the division of Unicode into blocks of 256 characters.
Hexadecimal is also simply shorter than decimal. You can use decimal
notation, too, but learning to use hexadecimal just makes life easier
-with the Unicode standard. The "C<U+HHHH>" notation uses hexadecimal,
+with the Unicode standard. The C<U+HHHH> notation uses hexadecimal,
for example.
The C<0x> prefix means a hexadecimal number, the digits are 0-9 I<and>
Perl front-end C<Convert::Recode> 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;