perlunifaq (1)
Leading comments
Automatically generated by Pod::Man 4.09 (Pod::Simple 3.35) Standard preamble: ========================================================================
NAME
perlunifaq - Perl Unicode FAQQ and A
This is a list of questions and answers about Unicode in Perl, intended to be read after perlunitut.perlunitut isn't really a Unicode tutorial, is it?
No, and this isn't really a UnicodePerl has an abstracted interface for all supported character encodings, so this is actually a generic "Encode" tutorial and "Encode"
What character encodings does Perl support?
To find out which character encodings your Perl supports, run:
perl -MEncode -le "print for Encode->encodings(':all')"
Which version of perl should I use?
Well, if you can, upgrade to the most recent, but certainly 5.8.1 or newer. The tutorial andYou should also check your modules, and upgrade them if necessary. For example, HTML::Entities requires version >= 1.32 to function correctly, even though the changelog is silent about this.
What about binary data, like images?
Well, apart from a bare "binmode $fh", you shouldn't treat them specially. (The binmode is needed because otherwise Perl may convert line endings on Win32 systems.)Be careful, though, to never combine text strings with binary strings. If you need text in a binary stream, encode your text strings first using the appropriate encoding, then join them with binary strings. See also: ``What if I don't encode?''.
When should I decode or encode?
Whenever you're communicating text with anything that is external to your perl process, like a database, a text file, a socket, or another program. Even if the thing you're communicating with is also written in Perl.What if I don't decode?
Whenever your encoded, binary string is used together with a text string, Perl will assume that your binary string was encoded withThis silent implicit decoding is known as ``upgrading''. That may sound positive, but it's best to avoid it.
What if I don't encode?
Your text string will be sent using the bytes in Perl's internal format. In some cases, Perl will warn you that you're doing something wrong, with a friendly warning:
Wide character in print at example.pl line 2.
Because the internal format is often
Is there a way to automatically decode or encode?
If all data that comes from a certain handle is encoded in exactly the same way, you can tell the PerlIO system to automatically decode everything, with the "encoding" layer. If you do this, you can't accidentally forget to decode or encode anymore, on things that use the layered handle.You can provide this layer when "open"ing the file:
open my $fh, '>:encoding(UTF-8)', $filename; # auto encoding on write open my $fh, '<:encoding(UTF-8)', $filename; # auto decoding on read
Or if you already have an open filehandle:
binmode $fh, ':encoding(UTF-8)';
Some database drivers for
What if I don't know which encoding was used?
Do whatever you can to find out, and if you have to: guess. (Don't forget to document your guess with a comment.)You could open the document in a web browser, and change the character set or character encoding until you can visually confirm that all characters look the way they should.
There is no way to reliably detect the encoding automatically, so if people keep sending you data without charset indication, you may have to educate them.
Can I use Unicode in my Perl sources?
Yes, you can! If your sources are
use utf8;
This doesn't do anything to your input, or to your output. It only influences the way your sources are read. You can use Unicode in string literals, in identifiers (but they still have to be ``word characters'' according to "\w"), and even in custom delimiters.
Data::Dumper doesn't restore the UTF8 flag; is it broken?
No, Data::Dumper's Unicode abilities are as they should be. There have been
some complaints that it should restore the Here's what happens: when Perl reads in a string literal, it sticks to 8 bit encoding as long as it can. (But perhaps originally it was internally encoded as
If you properly encode your strings for output, none of this is of your concern, and you can just "eval" dumped data as always.
Why do regex character classes sometimes match only in the ASCII range?
Starting in Perl 5.14 (and partially in Perl 5.12), just put a
"use feature 'unicode_strings'" near the beginning of your program.
Within its lexical scope you shouldn't have this problem. It also is
automatically enabled under "use feature ':5.12'" or "use v5.12" or
using "-E" on the command line for Perl 5.12 or higher.
The rationale for requiring this is to not break older programs that rely on the way things worked before Unicode came along. Those older programs knew only about the
However, on earlier Perls, or if you pass strings to subroutines outside the feature's scope, you can force Unicode rules by changing the encoding to
For a more detailed discussion, see Unicode::Semantics on
Why do some characters not uppercase or lowercase correctly?
See the answer to the previous question.How can I determine if a string is a text string or a binary string?
You can't. Some use theThis is something you, the programmer, has to keep track of; sorry. You could consider adopting a kind of ``Hungarian notation'' to help with this.
How do I convert from encoding FOO to encoding BAR?
By first converting the FOO-encoded byte string to a text string, and then the
text string to a BAR-encoded byte string:
my $text_string = decode('FOO', $foo_string); my $bar_string = encode('BAR', $text_string);
or by skipping the text string part, and going directly from one binary encoding to the other:
use Encode qw(from_to); from_to($string, 'FOO', 'BAR'); # changes contents of $string
or by letting automatic decoding and encoding do all the work:
open my $foofh, '<:encoding(FOO)', 'example.foo.txt'; open my $barfh, '>:encoding(BAR)', 'example.bar.txt'; print { $barfh } $_ while <$foofh>;
What are decode_utf8 and encode_utf8?
These are alternate syntaxes for "decode('utf8', ...)" and "encode('utf8', ...)".What is a wide character?
This is a term used for characters occupying more than one byte.The Perl warning ``Wide character in ...'' is caused by such a character. With no specified encoding layer, Perl tries to fit things into a single byte. When it can't, it emits this warning (if warnings are enabled), and uses
To avoid this warning and to avoid having different output encodings in a single stream, always specify an encoding explicitly, for example with a PerlIO layer:
binmode STDOUT, ":encoding(UTF-8)";
INTERNALS
What is the UTF8 flag?
Please, unless you're hacking the internals, or debugging weirdness, don't
think about the The
One of Perl's internal formats happens to be
What about the use bytes pragma?
Don't use it. It makes no sense to deal with bytes in a text string, and it makes no sense to deal with characters in a byte string. Do the proper conversions (by decoding/encoding), and things will work out well: you get character counts for decoded data, and byte counts for encoded data."use bytes" is usually a failed attempt to do something useful. Just forget about it.
What about the use encoding pragma?
Don't use it. Unfortunately, it assumes that the programmer's environment and that of the user will use the same encoding. It will use the same encoding for the source code and forIf you need non-ASCII characters in your source code, make it a
If you need to set the encoding for
What is the difference between :encoding and :utf8?
BecauseInstead of ":encoding(UTF-8)", you can simply use ":utf8", which skips the encoding step if the data was already represented as
Instead of "decode" and "encode", you could use "_utf8_on" and "_utf8_off", but this is considered bad style. Especially "_utf8_on" can be dangerous, for the same reason that ":utf8" can.
There are some shortcuts for oneliners; see -C in perlrun.
What's the difference between UTF-8 and utf8?
"UTF-8" is the official standard. "utf8" is Perl's way of being liberal in what it accepts. If you have to communicate with things that aren't so liberal, you may want to consider using "UTF-8". If you have to communicate with things that are too liberal, you may have to use "utf8". The full explanation is in Encode."UTF-8" is internally known as "utf-8-strict". The tutorial uses
For example, utf8 can be used for code points that don't exist in Unicode, like 9999999, but if you encode that to
Okay, if you insist: the ``internal format'' is utf8, not
I lost track; what encoding is the internal format really?
It's good that you lost track, because you shouldn't depend on the internal format being any specific encoding. But since you asked: by default, the internal format is eitherPerl knows how it stored the string internally, and will use that knowledge when you "encode". In other words: don't try to find out what the internal encoding for a certain string is, but instead just encode it into the encoding that you want.