Solving the Chinese Character Encoding Issue in Perl Output
Category Programming Techniques
Many friends often encounter a problem when using the Perl language: Perl does not support Chinese characters? Input or output of Chinese characters in Perl results in garbled text. What should we do then? The following is a guide for friends on how to solve this issue.
Methods/Steps
Take ActivePerl on Windows as an example.
The editor's code is particularly simple, just one line:
print "Hello";
But garbled text appears, as shown in the figure.
What should we do when encountering such a problem?
In fact, it's a problem with Perl encoding, which is related to the encoding of our operating system.
We open the cmd command prompt and enter chcp to obtain the encoding type code.
It can be seen that the default encoding format for the editor's Windows is code 936.
Then we can see that code page 936 corresponds to gb2313 (i.e., Chinese), or gbk.
We know that Windows uses the gb2312 encoding, and at this time, we just need to change the encoding format.
Enter at the beginning of the code:
use utf8;
#Import the utf8 module, strings in the script use utf8 as the encoding format
binmode(STDOUT,":encoding(gbk)");
#Standard output uses gbk as the encoding format, you can also change gbk to gb2312
binmode(STDIN,":encoding(gbk)");
#If it involves the input stream, such as reading a file, not adding this will result in garbled text when reading Chinese files
binmode(STDERR,":encoding(gbk)");
#If there are Chinese characters in the strings of exception errors, please add STDERR, otherwise, it will also result in garbled text
print "Hello";
Can we now output Chinese characters normally?
It is important to note that if the code involves reading a Chinese file, you must add binmode(STDIN,":encoding(gbk)");
.
There is also another method, which is to use a certain encoding format for all our STDIN, STDOUT, and STDERR (i.e., standard input, standard output, and standard error).
It's still that simple program, printing a Chinese character with print.
The code is as follows:
use utf8;
use open ":encoding(gbk)",":std";
#Standard input, output, and error are all encoded in gbk encoding format
print "Hello";
>
Original article link: https://jingyan.baidu.com/article/4f7d5712fd36301a21192744.html
** Click to Share Notes
-
-
-