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
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");
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,
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<not> subject to
the loose matching of encoding names.
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>;
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
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 the data:
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
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
=over 4
-=item
+=item *
Will My Old Scripts Break?
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?
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?
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?
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?
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?