在我的新 Arch 安装中,perl
Unicode 似乎不太适合。例如,给定这个输入文件:
??? ??
???
Run Code Online (Sandbox Code Playgroud)
这个命令应该给我每行的最后两个字符:
$ perl -CIO -pe 's/.*(..)$/$1/' file
ε
º¢
Run Code Online (Sandbox Code Playgroud)
然而,正如你在上面看到的,我得到了胡言乱语。正确的输出是:
??
??
Run Code Online (Sandbox Code Playgroud)
我知道我的终端 ( gnome-terminator
) 支持 UTF-8,因为它们都按预期工作:
$ cat file
??? ??
???
$ perl -pe '' file
??? ??
???
Run Code Online (Sandbox Code Playgroud)
不幸的是,没有-CIO
,perl
也不能正确处理文件:
$ perl -pe 's/.*(..)$/$1/' file
?
??
Run Code Online (Sandbox Code Playgroud)
它也不应该是语言环境问题:
$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=
Run Code Online (Sandbox Code Playgroud)
我猜我需要安装一些 Perl 包,但我不知道是哪些。一些相关资料:
$ perl --version | grep subversion
This is perl 5, version 22, subversion 0 (v5.22.0) built for x86_64-linux-thread-multi
$ pacman -Qs unicode
local/fribidi 0.19.7-1
A Free Implementation of the Unicode Bidirectional Algorithm
local/icu 55.1-1
International Components for Unicode library
local/libunistring 0.9.6-1
Library for manipulating Unicode strings and C strings
local/perl 5.22.0-1 (base)
A highly capable, feature-rich programming language
local/perl-unicode-stringprep 1.105-1
Preparation of Internationalized Strings (RFC 3454)
local/perl-unicode-utf8simple 1.06-5
Conversions to/from UTF8 from/to characterse
local/ttf-arphic-uming 0.2.20080216.1-5
CJK Unicode font Ming style
Run Code Online (Sandbox Code Playgroud)
如何让我的 perl 安装与 Unicode 兼容?
您所描述的问题是我测试过的系统上的标准行为。I
并O
影响标准输入和标准输出,所以这应该有效:
? cat data | perl -CIO -pe 's/.*(..)$/$1/'
??
??
Run Code Online (Sandbox Code Playgroud)
而这可能不会:
? perl -CIO -pe 's/.*(..)$/$1/' data
ε
º¢
Run Code Online (Sandbox Code Playgroud)
还有两个选项可以perl -C
产生您想要的行为。
i 8 UTF-8 is the default PerlIO layer for input streams
o 16 UTF-8 is the default PerlIO layer for output streams
Run Code Online (Sandbox Code Playgroud)
这基本上是对 perl 说,使用文件打开形式:
open(F, "<:utf8", "data");
Run Code Online (Sandbox Code Playgroud)
或者你可以使用perl -CSD
which 的简写perl -CIOEio
S 7 I + O + E
D 24 i + o
Run Code Online (Sandbox Code Playgroud)
然后你得到
? perl -CSD -pe 's/.*(..)$/$1/' data
??
??
Run Code Online (Sandbox Code Playgroud)
如果PERLIO
设置了环境变量并包含:utf8
此行为,则也将启用此行为。
看起来默认行为perl
在配置/编译时也是不可修改的(下面的 cuonglm 评论)。Arch当然不会设置任何东西。我怀疑 debian perl 包会修改默认行为。
归档时间: |
|
查看次数: |
740 次 |
最近记录: |