=item Input and Output Disciplines
Perl knows when a filehandle uses Perl's internal Unicode encodings
-(UTF-8 or UTF-EBCDIC) if the filehandle is opened with the ":utf8"
-layer. Other encodings can be converted to Perl's encoding on input
-or from Perl's encoding on output by use of the ":encoding(...)"
-layer. See L<open>.
+(UTF-8, or UTF-EBCDIC if in EBCDIC) if the filehandle is opened with
+the ":utf8" layer. Other encodings can be converted to Perl's
+encoding on input or from Perl's encoding on output by use of the
+":encoding(...)" layer. See L<open>.
To indicate that Perl source itself is using a particular encoding,
see L<encoding>.
As a compatibility measure, the C<use utf8> pragma must be explicitly
included to enable recognition of UTF-8 in the Perl scripts themselves
-on ASCII-based machines or to recognize UTF-EBCDIC on EBCDIC-based
+(in string or regular expression literals, or in identifier names) on
+ASCII-based machines or to recognize UTF-EBCDIC on EBCDIC-based
machines. B<These are the only times when an explicit C<use utf8>
is needed.>
${^WIDE_SYSTEM_CALLS} global flag is set to C<1>, all system calls
will use the corresponding wide-character APIs. This feature is
available only on Windows to conform to the API standard already
-established for that platform.
+established for that platform--and there are very few non-Windows
+platforms that have Unicode-aware APIs.
The C<bytes> pragma will always, regardless of platform, force byte
semantics in a particular lexical scope. See L<bytes>.
as such and converted to Perl's internal representation only if the
appropriate L<encoding> is specified.
-Unicode characters can also be added to a string by using the C<\x{...}>
-notation. The Unicode code for the desired character, in
+Unicode characters can also be added to a string by using the
+C<\x{...}> notation. The Unicode code for the desired character, in
hexadecimal, should be placed in the braces. For instance, a smiley
face is C<\x{263A}>. This encoding scheme only works for characters
with a code of 0x100 or above.
use charnames ':full';
-you can use the C<\N{...}> notation and put the official Unicode character
-name within the braces, such as C<\N{WHITE SMILING FACE}>.
+you can use the C<\N{...}> notation and put the official Unicode
+character name within the braces, such as C<\N{WHITE SMILING FACE}>.
=item *
=item *
-Regular expressions match characters instead of bytes.
-"." matches a character instead of a byte. The C<\C> pattern
-is provided to force a match a single byte--a "C<char>" in C, hence C<\C>.
+Regular expressions match characters instead of bytes. "." matches
+a character instead of a byte. The C<\C> pattern is provided to force
+a match a single byte--a C<char> in C, hence C<\C>.
=item *
Character classes in regular expressions match characters instead of
bytes and match against the character properties specified in the
-Unicode properties database. C<\w> can be used to match an
+Unicode properties database. C<\w> can be used to match a Japanese
ideograph, for instance.
=item *
Named Unicode properties, scripts, and block ranges may be used like
character classes via the C<\p{}> "matches property" construct and
the C<\P{}> negation, "doesn't match property".
-For instance, C<\p{Lu}> matches any
-character with the Unicode "Lu" (Letter, uppercase) property, while
-C<\p{M}> matches any character with an "M" (mark--accents and such)
-property. Brackets are not required for single letter properties, so
-C<\p{M}> is equivalent to C<\pM>. Many predefined properties are
-available, such as C<\p{Mirrored}> and C<\p{Tibetan}>.
+
+For instance, C<\p{Lu}> matches any character with the Unicode "Lu"
+(Letter, uppercase) property, while C<\p{M}> matches any character
+with an "M" (mark--accents and such) property. Brackets are not
+required for single letter properties, so C<\p{M}> is equivalent to
+C<\pM>. Many predefined properties are available, such as
+C<\p{Mirrored}> and C<\p{Tibetan}>.
The official Unicode script and block names have spaces and dashes as
separators, but for convenience you can use dashes, spaces, or
-underbars, and case is unimportant. It is
-recommended, however, that for consistency you use the following naming:
-the official Unicode script, property, or block name (see below for the
-additional rules that apply to block names) with whitespace and dashes
-removed, and the words "uppercase-first-lowercase-rest". "C<Latin-1
-Supplement>" thus becomes "C<Latin1Supplement>".
+underbars, and case is unimportant. It is recommended, however, that
+for consistency you use the following naming: the official Unicode
+script, property, or block name (see below for the additional rules
+that apply to block names) with whitespace and dashes removed, and the
+words "uppercase-first-lowercase-rest". C<Latin-1 Supplement> thus
+becomes C<Latin1Supplement>.
You can also use negation in both C<\p{}> and C<\P{}> by introducing a caret
(^) between the first brace and the property name: C<\p{^Tamil}> is
C<L&> is a special case, which is an alias for C<Ll>, C<Lu>, and C<Lt>.
Because Perl hides the need for the user to understand the internal
-representation of Unicode characters, there is no need to implement the
-somewhat messy concept of surrogates. C<Cs> is therefore not
+representation of Unicode characters, there is no need to implement
+the somewhat messy concept of surrogates. C<Cs> is therefore not
supported.
Because scripts differ in their directionality--Hebrew is
Common Any character (or unassigned code point)
not explicitly assigned to a script
-For backward compatibility, all properties mentioned so far may have C<Is>
-prepended to their name, so C<\P{IsLu}>, for example, is equal to C<\P{Lu}>.
+For backward compatibility (with Perl 5.6), all properties mentioned
+so far may have C<Is> prepended to their name, so C<\P{IsLu}>, for
+example, is equal to C<\P{Lu}>.
=head2 Blocks
-In addition to B<scripts>, Unicode also defines B<blocks> of characters.
-The difference between scripts and blocks is that the concept of
-scripts is closer to natural languages, while the concept of blocks
-is more of an artificial grouping based on groups of around 256
+In addition to B<scripts>, Unicode also defines B<blocks> of
+characters. The difference between scripts and blocks is that the
+concept of scripts is closer to natural languages, while the concept
+of blocks is more of an artificial grouping based on groups of 256
Unicode characters. For example, the C<Latin> script contains letters
-from many blocks but does not contain all the characters from those
+from many blocks but does not contain all the characters from those
blocks. It does not, for example, contain digits, because digits are
shared across many scripts. Digits and similar groups, like
punctuation, are in a category called C<Common>.
Katakana block is referenced via C<\p{InKatakana}>. The C<In>
prefix may be omitted if there is no naming conflict with a script
or any other property, but it is recommended that C<In> always be used
-to avoid confusion.
+for block tests to avoid confusion.
These block names are supported:
=item *
-The C<pack()>/C<unpack()> letters "C<c>" and "C<C>" do I<not> change,
+The C<pack()>/C<unpack()> letters C<c> and C<C> do I<not> change,
since they are often used for byte-oriented formats. Again, think
-"C<char>" in the C language. There is a new "C<U>" specifier
-that converts between Unicode characters and integers.
+C<char> in the C language.
+
+There is a new C<U> specifier that converts between Unicode characters
+and code points.
=item *
But in this particular example, you probably really want
- \p{Greek}
+ \p{GreekAndCoptic}
which will match assigned characters known to be part of the Greek script.
UTF-16, UTF-16BE, UTF16-LE, Surrogates, and BOMs (Byte Order Marks)
-The followings items are mostly for reference, Perl doesn't
-use them internally.
+The followings items are mostly for reference and general Unicode
+knowledge, Perl doesn't use these constructs internally.
UTF-16 is a 2 or 4 byte encoding. The Unicode code points
-C<U+0000..U+FFFF> are stored in a single 16-bit unit, and the code points
-C<U+10000..U+10FFFF> in two 16-bit units. The latter case is
+C<U+0000..U+FFFF> are stored in a single 16-bit unit, and the code
+points C<U+10000..U+10FFFF> in two 16-bit units. The latter case is
using I<surrogates>, the first 16-bit unit being the I<high
surrogate>, and the second being the I<low surrogate>.
The way this trick works is that the character with the code point
C<U+FFFE> is guaranteed not to be a valid Unicode character, so the
sequence of bytes C<0xFF 0xFE> is unambiguously "BOM, represented in
-little-endian format" and cannot be "C<U+FFFE>, represented in big-endian
+little-endian format" and cannot be C<U+FFFE>, represented in big-endian
format".
=item *
more letters according to your language and country.
In the second case, the C<\w> set of characters is much, much larger.
-Most importantly, even in the set of the first 256 characters, it
-will probably match different characters: unlike most locales,
-which are specific to a language and country pair, Unicode classifies all
-the characters that are letters as C<\w>. For example, your locale might
-not think that LATIN SMALL LETTER ETH is a letter (unless you happen
-to speak Icelandic), but Unicode does.
+Most importantly, even in the set of the first 256 characters, it will
+probably match different characters: unlike most locales, which are
+specific to a language and country pair, Unicode classifies all the
+characters that are letters I<somewhere> as C<\w>. For example, your
+locale might not think that LATIN SMALL LETTER ETH is a letter (unless
+you happen to speak Icelandic), but Unicode does.
As discussed elsewhere, Perl has one foot (two hooves?) planted in
-each of two worlds: the old world of bytes and the new
-world of characters, upgrading from bytes to characters when necessary.
+each of two worlds: the old world of bytes and the new world of
+characters, upgrading from bytes to characters when necessary.
If your legacy code does not explicitly use Unicode, no automatic
switch-over to characters should happen. Characters shouldn't get
-downgraded to bytes, either. It is possible to accidentally mix
-bytes and characters, however (see L<perluniintro>), in which case
-C<\w> in regular expressions might start behaving differently. Review
-your code.
+downgraded to bytes, either. It is possible to accidentally mix bytes
+and characters, however (see L<perluniintro>), in which case C<\w> in
+regular expressions might start behaving differently. Review your
+code. Use warnings and the C<strict> pragma.
=back
=item *
-C<DO_UTF8(sv)> returns true if the C<UTF8> flag is on and the bytes pragma
-is not in effect. C<SvUTF8(sv)> returns true is the C<UTF8> flag is on; the
-bytes pragma is ignored. The C<UTF8> flag being on does B<not> mean that
-there are any characters of code points greater than 255 (or 127) in
-the scalar or that there are even any characters in the scalar.
-What the C<UTF8> flag means is that the sequence of octets in the
-representation of the scalar is the sequence of UTF-8 encoded
-code points of the characters of a string. The C<UTF8> flag being
-off means that each octet in this representation encodes a single
-character with code point 0..255 within the string. Perl's Unicode
-model is not to use UTF-8 until it is absolutely necessary.
+C<DO_UTF8(sv)> returns true if the C<UTF8> flag is on and the bytes
+pragma is not in effect. C<SvUTF8(sv)> returns true is the C<UTF8>
+flag is on; the bytes pragma is ignored. The C<UTF8> flag being on
+does B<not> mean that there are any characters of code points greater
+than 255 (or 127) in the scalar or that there are even any characters
+in the scalar. What the C<UTF8> flag means is that the sequence of
+octets in the representation of the scalar is the sequence of UTF-8
+encoded code points of the characters of a string. The C<UTF8> flag
+being off means that each octet in this representation encodes a
+single character with code point 0..255 within the string. Perl's
+Unicode model is not to use UTF-8 until it is absolutely necessary.
=item *
-C<uvuni_to_utf8(buf, chr>) writes a Unicode character code point into a
-buffer encoding the code point as UTF-8, and returns a pointer
+C<uvuni_to_utf8(buf, chr>) writes a Unicode character code point into
+a buffer encoding the code point as UTF-8, and returns a pointer
pointing after the UTF-8 bytes.
=item *
C<sv_uni_display(dsv, ssv, pvlim, flags)> are useful for debugging the
output of Unicode strings and scalars. By default they are useful
only for debugging--they display B<all> characters as hexadecimal code
-points--but with the flags C<UNI_DISPLAY_ISPRINT> and
-C<UNI_DISPLAY_BACKSLASH> you can make the output more readable.
+points--but with the flags C<UNI_DISPLAY_ISPRINT>,
+C<UNI_DISPLAY_BACKSLASH>, and C<UNI_DISPLAY_QQ> you can make the
+output more readable.
=item *
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
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 discipline. 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.
=head2 Unicode and EBCDIC
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
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.
+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
+discipline when opening files
open(my $fh,'<:utf8', 'anything');
my $line_of_unicode = <$fh>;
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()>
Notice that because of the default behaviour of not doing any
conversion upon input if there is no default discipline,
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";
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 *
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 *
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
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
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
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
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, ...)>.
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>