1. Perl
  2. Module
  3. here

utf8 - Converts a string written in UTF - 8 of the source code to an decoded string

The utf8 module allows you to convert a string written in UTF-8 in your source code to an decoded string.

# Convert a string written in UTF-8 in the source code to an decoded string
use utf8;
my $message = 'aiueo';

Save the source code in UTF-8. The string'aiueo'is automatically converted to an decoded string.

FAQ about utf8 pragma

Q, Regarding the function of the utf8 pragma, can you think that the UTF-8 byte string is converted to an decoded string using the decode function of the Encode module?

A. Yes. Think of it as doing the following:

# Write the same process using the Encode module
my $str = 'aiueo';
$str = decode('UTF-8', $str);

Q. I've heard about the UTF-8 flag, but can I think that the utf8 pragma does the process of setting the UTF-8 flag?

A. The interpretation is wrong. If the string in the source code consists of characters in the ASCII range, the utf8 pragma will not set the UTF-8 flag on the string, and if it contains characters outside the ASCII range, it will be in the UTF-8 flag. Stand up.

This makes it very difficult to understand what you are doing from the programmer's point of view. So don't be afraid to remember that the utf8 pragma converts a UTF-8 byte string into an decoded string.

Q. Should I forget about the UTF-8 flag?

A. Yes, basically forget about it. But there is one thing that is useful to remember, so let me tell you. That is, "If the UTF-8 flag is set, it can be said that it is an decoded string." The opposite is not true. There is a utf8::is_utf8 function as a function to check if the UTF-8 flag is set. If you use this function to check that the UTF-8 flag is set, you can say that it is an decoded string.

# Internal string if UTF-8 flag is set
my $utf8_flagged = utf8::is_utf8 ($str);

if ($utf8_flagged) {
  # $str is an decoded string
}
else {
  # $str is an decoded string or a byte string
}

Related Informatrion