Tutorial by Examples

The following examples use the UTF-8 encoding to represent filenames (and directory names) on disk. If you want to use another encoding, you should use Encode::encode(...). use v5.14; # Make Perl recognize UTF-8 encoded characters in literal strings. # For this to work: Make sure your text-editor...
Perl does not attempt to decode filenames returned by builtin functions or modules. Such strings representing filenames should always be decoded explicitly, in order for Perl to recognize them as Unicode. use v5.14; use Encode qw(decode_utf8); # Ensure that possible error messages printed to sc...
Enable utf8 pragma In order to enable utf8 pragma in one-liner, perl interpreter should be called with -Mutf8 option: perl -Mutf8 -E 'my $人 = "human"; say $人' Unicode handling with -C switch The -C command line flag lets you control Unicode features. It can be followed by a list of o...
The encoding to be used for the standard I/O filehandles (STDIN, STDOUT, and STDERR), can be set separately for each handle using binmode: binmode STDIN, ':encoding(utf-8)'; binmode STDOUT, ':utf8'; binmode STDERR, ':utf8'; Note: when reading one would in general prefer :encoding(utf-8) over :...
Setting encoding with open() When opening a text file, you may specify it's encoding explicitly with a three-argument open(). This en-/decoder attached to a file handle is called an "I/O layer": my $filename = '/path/to/file'; open my $fh, '<:encoding(utf-8)', $filename or die "...
The utf8 pragma indicates that the source code will be interpreted as UTF-8. Of course, this will only work if your text editor is also saving the source as UTF-8 encoded. Now, string literals can contain arbitrary Unicode characters; identifiers can also contain Unicode but only word-like characte...
Reading invalid UTF-8 When reading UTF-8 encoded data, it is important to be aware of the fact the UTF-8 encoded data can be invalid or malformed. Such data should usually not be accepted by your program (unless you know what you are doing). When unexpectedly encountering malformed data, different ...

Page 1 of 1