Perl Unicode Cookbook: Enable UTF-8 Literals

℞ 3: Declare source in UTF-8 for identifiers and literals

Without the all-critical use utf8 declaration, putting UTF‑8 in your literals and identifiers won’t work right. If you used the standard Perl Unicode preamble, this already happened. If you did, you can do things like this:

use utf8;

 my $measure   = "Ångström";
 my @μsoft     = qw( cp852 cp1251 cp1252 );
 my @ὑπέρμεγας = qw( ὑπέρ  μεγας );
 my @鯉        = qw( koi8-f koi8-u koi8-r );
 my $motto     = "👪 💗 🐪"; # FAMILY, GROWING HEART, DROMEDARY CAMEL

If you forget use utf8, high bytes will be misunderstood as separate characters, and nothing will work right. Remember that this pragma only affects the interpretation of literal UTF-8 in your source code.

Previous: ℞ 2: Fine-Tuning Unicode Warnings

Series Index: The Standard Preamble

Next: ℞ 4: Characters and Their Numbers



Something wrong with this article? Help us out by opening an issue or pull request on GitHub