| Start |
|---|
| The utf8 pragma: using Unicode in your sources The Now, string literals can contain arbitrary Unicode characters; identifiers can also contain Unicode but only word-like characters (see perldata (opens new window) and perlrecharclass (opens new window) for more information): Note: When printing text to the terminal, make sure it supports UTF-8.* There may be complex and counter-intuitive relationships between output and source encoding. Running on a UTF-8 terminal, you may find that adding the In the first case, Perl treats the string as raw bytes and prints them like that. As these bytes happen to be valid UTF-8, they look correct even though Perl doesn't really know what characters they are (e.g.
|
| Create filenames 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 |
| Read filenames 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. Note: if you are concerned about invalid UTF-8 in the filenames, the use of |
Command line switches for one-liners Enable utf8 pragmaIn order to enable Unicode handling with -C switchThe Standard I/O
Script's arguments
Default PerlIO layer
|
| Standard I/O The encoding to be used for the standard I/O filehandles ( Note: when reading one would in general prefer Alternatively, you can use the Alternatively, to set all filehandles (both those yet to be opened and also the standard ones) to use |
File handles Setting encoding with open()When opening a text file, you may specify it's encoding explicitly with a three-argument See Remarks (opens new window) for a discussion of the differences between Setting encoding with binmode()Alternatively, you may use binmode() to set the encoding for individual file handle: open pragmaTo avoid setting encoding for each file handle separately, you may use the Setting encoding with command line -C flagFinally, it is also possible to run the perl interpreter with a |
Handling invalid UTF-8 Reading invalid UTF-8When 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 actions can be considered:
By default, Perl will The following example writes 3 bytes in encoding ISO 8859-1 to disk. It then tries to read the bytes back again as UTF-8 encoded data. One of the bytes, The program will abort with a fatal warning: Line 10 is here the second last line, and the error occurs in the part of the line with If you don't make warnings fatal in the above program, Perl will still print the warning. However, in this case it will try to recover from the malformed byte RemarksA Warning on Filename EncodingIt should be worth mentioning that Filename Encoding is not only platform specific but also filesystem specific. It is never entirely safe to assume (but often usually is) that just because you can encode and write to a given filename, that when you later try to open that same filename for reading, it will still be called the same thing. For instance, if you write to a filesystem such as But it is even less safe to assume that a file you can create, read and write to by explicit naming will be called the same thing when queried through other calls, for instance, On some systems such as VAX, you can't even always assume that Also, on UNIX, there is a very liberal set of legal characters for filenames that the OS permits, excluding only Exercise much caution here, avoid fancy tricks with filenames if you have a choice, and always have tests to make sure any fancy tricks you do use are consistent. Exercise doubly as much caution if you're writing code intended to be run on platforms outside your control, such as if you're writing code that is intended for :encoding(utf8) vs :utf8Since UTF-8 is one of the internal formats for representation of strings in Perl, the encoding/decoding step may often be skipped. Instead of More details: What is the difference between :encoding and :utf8 (opens new window) UTF-8 vs utf8 vs UTF8As of Perl For example, Since encoding names are case insensitive, More details: UTF-8 vs. utf8 vs. UTF8 (opens new window) More ReadingDetails about Perl's Unicode handling is described in more detail in the following sources:
Posts from stackoverflow.com (caveat: might not be up-to-date): Youtube videos:
|
Read Article
https://develop.horje.com/learn/1434/reference