我想知道为什么使用Perl构建的大多数现代解决方案默认情况下不启用UTF-8.
我知道核心Perl脚本存在许多遗留问题,可能会破坏它们.但是,从我的角度来看,在21 日的世纪,新的大项目(或具有大的方面讲项目)应该从头开始他们的软件UTF-8的证明.我仍然没有看到它发生.例如,Moose启用严格和警告,但不启用Unicode.Modern :: Perl也减少了样板,但没有UTF-8处理.
为什么?是否有一些理由在2011年的现代Perl项目中避免使用UTF-8?
评论@tchrist太长了,所以我在这里添加它.
似乎我没有说清楚.让我尝试添加一些东西.
tchrist和我看到情况非常相似,但我们的结论完全是相反的.我同意,Unicode的情况很复杂,但这就是为什么我们(Perl用户和编码人员)需要一些层(或编译指示),这使得UTF-8处理变得像现在一样容易.
tchrist指出要涵盖的许多方面,我会阅读并思考它们几天甚至几周.不过,这不是我的观点.tchrist试图证明没有一种方法"启用UTF-8".我没有太多的知识可以与之争辩.所以,我坚持住实例.
我和Rakudo一起玩,UTF-8就在我需要的地方.我没有任何问题,它只是奏效了.也许在某些地方存在一些限制,但一开始,我测试的所有工作都按照我的预期进行.
这不应该是现代Perl 5的目标吗?我更强调一点:我不是建议将UTF-8作为核心Perl的默认字符集,我建议可以为那些开发新项目的人快速触发它.
另一个例子,但更负面的语气.框架应该使开发更容易.几年前,我尝试过Web框架,但只是把它们扔掉了,因为"启用UTF-8"是如此模糊.我没有找到如何以及在何处挂钩Unicode支持.这是非常耗时的,我发现它更容易走老路.现在我看到这里有一个赏金来处理与梅森 2 相同的问题:如何让Mason2 UTF-8干净?.因此,它是一个非常新的框架,但使用UTF-8需要深入了解其内部.这就像一个大红色标志:停止,不要使用我!
我真的很喜欢Perl.但处理Unicode是痛苦的.我仍然发现自己在墙上奔跑.某种方式tchrist是正确的,并回答我的问题:新项目不吸引UTF-8,因为它在Perl 5中太复杂了.
例如,匹配"民族报"在""国际化"没有额外的模块,是否有可能在新的Perl版本(5.14,5.15等)?
我找到了答案!感谢tchrist
与UCA匹配的Rigth解决方案(thnx到/sf/users/32989071/).
# found start/end offsets for matched utf-substring (without intersections)
use 5.014;
use strict;
use warnings;
use utf8;
use Unicode::Collate;
binmode STDOUT, ':encoding(UTF-8)';
my $str = "Îñ?érñå?îöñå?îžå?îöñ" x 2;
my $look = "Nation";
my $Collator = Unicode::Collate->new(
normalization => undef, level => 1
);
my @match = $Collator->match($str, $look);
if (@match) {
my $found = $match[0];
my $f_len = length($found);
say "match result: $found (length is $f_len)";
my $offset = 0;
while ((my $start = …Run Code Online (Sandbox Code Playgroud) 我正在帮助客户将他们的Perl平面文件公告板网站从ISO-8859-1转换为Unicode.
由于这是我第一次,我想知道以下"清单"是否完整.一切都在测试中很好用,但我可能会遗漏一些只会在极少数情况下发生的事情.
这是我到目前为止所做的事情(请原谅我只包括"摘要"代码示例):
确保文件始终以UTF-8读写:
use open ':utf8';
Run Code Online (Sandbox Code Playgroud)确保收到CGI输入为UTF-8(该站点未使用CGI.pm):
s{%([a-fA-F0-9]{2})}{ pack ("C", hex ($1)) }eg; # Kept from existing code
s{%u([0-9A-F]{4})}{ pack ('U*', hex ($1)) }eg; # Added
utf8::decode $_;
Run Code Online (Sandbox Code Playgroud)确保文本打印为UTF-8:
binmode STDOUT, ':utf8';
Run Code Online (Sandbox Code Playgroud)确保浏览器将我的内容解释为UTF-8:
Content-Type: text/html; charset=UTF-8
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
Run Code Online (Sandbox Code Playgroud)确保表单发送UTF-8(只要设置了页面编码,可能不需要):
accept-charset="UTF-8"
Run Code Online (Sandbox Code Playgroud)不要认为我需要以下内容,因为内联文本(菜单,标题等)仅以ASCII格式显示:
use utf8;
Run Code Online (Sandbox Code Playgroud)这看起来合理还是我错过了什么?
编辑:我可能还应该提到,我们将运行一次性批处理来读取所有现有的文本数据文件并将其保存为UTF-8编码.
我想为我自己的"默认使用"制作一个模块,例如:
use My::perldefs;
Run Code Online (Sandbox Code Playgroud)
具有以下内容(主要基于tchrist的帖子.)
use 5.014;
use strict;
use features qw(switch say state);
no warnings;
use warnings qw(FATAL closed threads internal debugging pack substr malloc
unopened portable prototype inplace io pipe unpack regexp
deprecated exiting glob digit printf utf8 layer
reserved parenthesis taint closure semicolon);
no warnings qw(exec newline);
use utf8;
use open qw(:std :utf8);
use charnames qw(:full);
use feature qw(unicode_strings);
use Encode qw(encode decode);
use Unicode::Normalize qw(NFD NFC);
use Carp qw(carp croak confess cluck);
use autodie; …Run Code Online (Sandbox Code Playgroud) 我正在开发一个处理外语数据的项目.我的Perl脚本运行正常.
然后我想使用Tie :: File,因为这是一个简洁的概念(并节省时间和编码).
看起来Tie:File在Unicode/UTF-8下失败了(除非我遗漏了什么).
这是一个描述问题的程序:(数据是英语,希腊语和希伯来语的混合):
use strict;
use warnings;
use 5.014;
use Win32::Console;
use autodie;
use warnings qw< FATAL utf8 >;
use Carp;
use Carp::Always;
use utf8;
use feature qw< unicode_strings>;
use charnames qw< :full>;
use Tie::File;
my ($i);
my ( $FileName);
my (@Tied);
binmode STDOUT, ':unix:utf8';
binmode STDERR, ':unix:utf8';
binmode $DB::OUT, ':unix:utf8' if $DB::OUT; # for the debugger
Win32::Console::OutputCP(65001); # Set the console code page to UTF8
$FileName = 'E:\\My Documents\\Technical\\Perl\\Eclipse workspace\\Work\\'.
'Tie File test res.txt';
tie @Tied, …Run Code Online (Sandbox Code Playgroud)