多么“$!”在 Perl 中,文本将以 UTF-8 格式返回?

How "$!" text will be returned in UTF-8 in Perl?

我阅读了有关 "$!" text will be returned in UTF-8 when appropriate

的 Perl 文档

The stringification of $! and $^E will have the UTF-8 flag set when the text is actually non- ASCII UTF-8. This will enable programs that are set up to be locale-aware to properly output messages in the user's native language. Code that needs to continue the 5.20 and earlier behavior can do the stringification within the scopes of both use bytes and use locale ":messages". Within these two scopes, no other Perl operations will be affected by locale; only $! and $^E stringification. The bytes pragma causes the UTF-8 flag to not be set, just as in previous Perl releases.

你能举个例子吗?感谢您的帮助。

这意味着 $! 将包含 Unicode 代码点而不是自 5.22 以来的编码文本。

例如,与 errno 1 关联的消息在该系统的法语语言环境中为 Opération non permise,其中包含非 ASCII 字符 U+E9 带尖音符号的拉丁文小写字母 E ( "é").

$ LC_ALL=fr_CA.UTF-8 5.20t/bin/perl -E'use locale; say sprintf "%vX", $!=1'
4F.70.C3.A9.72.61.74.69.6F.6E.20.6E.6F.6E.20.70.65.72.6D.69.73.65
      ^^^^^
      U+E9 encoded using UTF-8

$ LC_ALL=fr_CA.UTF-8 5.22t/bin/perl -E'use locale; say sprintf "%vX", $!=1'
4F.70.E9.72.61.74.69.6F.6E.20.6E.6F.6E.20.70.65.72.6D.69.73.65
      ^^
      U+E9 as Unicode Code Point

因此,当您向输出添加编码层时,消息将正确输出。

$ LC_ALL=fr_CA.UTF-8 5.20t/bin/perl -we'use open ":std", ":locale"; use locale; die($!=1);'
Opération non permise at -e line 1.

$ LC_ALL=fr_CA.UTF-8 5.22t/bin/perl -we'use open ":std", ":locale"; use locale; die($!=1);'
Opération non permise at -e line 1.