gpt4 book ai didi

perl - 不同 utf8 的开放式编译指示有何不同?

转载 作者:行者123 更新时间:2023-12-02 06:19:34 25 4
gpt4 key购买 nike

这三个版本的行为是否都不同?

use open qw( :encoding(UTF-8) :std );  
use open qw( :encoding(UTF8) :std );
use open qw( :utf8 :std );

最佳答案

首先,:utf8 仅将文本标记为 UTF-8,它不检查它是否有效。见 this post on PerlMonks供引用。

:encoding 是 PerlIO 的扩展层,perl perldoc perliol

":encoding" use Encoding; makes this layer available, although PerlIO.pm "knows" where to find it. It is an example of a layer which takes an argument as it is called thus: open( $fh, "<:encoding(iso-8859-7)", $pathname );

其他两个问题在常见问题解答中得到解答perldoc perlunifaq

What is the difference between ":encoding" and ":utf8"? Because UTF-8 is one of Perl's internal formats, you can often just skip the encoding or decoding step, and manipulate the UTF8 flag directly. Instead of ":encoding(UTF-8)", you can simply use ":utf8", which skips the encoding step if the data was already represented as UTF8 internally. This is widely accepted as good behavior when you're writing, but it can be dangerous when reading, because it causes internal inconsistency when you have invalid byte sequences. Using ":utf8" for input can sometimes result in security breaches, so please use ":encoding(UTF-8)" instead. Instead of "decode" and "encode", you could use "_utf8_on" and "_utf8_off", but this is considered bad style. Especially "_utf8_on" can be dangerous, for the same reason that ":utf8" can. There are some shortcuts for oneliners; see "-C" in perlrun.

What's the difference between "UTF-8" and "utf8"? "UTF-8" is the official standard. "utf8" is Perl's way of being liberal in what it accepts. If you have to communicate with things that aren't so liberal, you may want to consider using "UTF-8". If you have to communicate with things that are too liberal, you may have to use "utf8". The full explanation is in Encode. "UTF-8" is internally known as "utf-8-strict". The tutorial uses UTF-8 consistently, even where utf8 is actually used internally, because the distinction can be hard to make, and is mostly irrelevant. For example, utf8 can be used for code points that don't exist in Unicode, like 9999999, but if you encode that to UTF-8, you get a substitution character (by default; see "Handling Malformed Data" in Encode for more ways of dealing with this.) Okay, if you insist: the "internal format" is utf8, not UTF-8. (When it's not some other encoding.)

open pragma(即,use open)只设置默认的 PerlIO 层用于输入和输出:std 执行以下操作,

The ":std" subpragma on its own has no effect, but if combined with the ":utf8" or ":encoding" subpragmas, it converts the standard filehandles (STDIN, STDOUT, STDERR) to comply with encoding selected for input/output handles. For example, if both input and out are chosen to be ":encoding(utf8)", a ":std" will mean that STDIN, STDOUT, and STDERR are also in ":encoding(utf8)". On the other hand, if only output is chosen to be in ":encoding(koi8r)", a ":std" will cause only the STDOUT and STDERR to be in "koi8r". The ":locale" subpragma implicitly turns on ":std".

所以 :std 是一个 subpragma(特定于 open.pm),它设置标准流以接收 Unicode 输入 perl :utf8 如上所述。

关于perl - 不同 utf8 的开放式编译指示有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14566460/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com