我正在尝试使用"utf8"编译指示编写Perl脚本,并且我得到了意想不到的结果.我正在使用Mac OS X 10.5(Leopard),我正在使用TextMate进行编辑.我的编辑器和操作系统的所有设置都默认为以utf-8格式写入文件.
但是,当我在文本文件中输入以下内容时,将其保存为".pl"并执行它,我得到友好的"带问号的菱形"代替非ASCII字符.
#!/usr/bin/env perl -w
use strict;
use utf8;
my $str = 'Çirçös';
print( "$str\n" );
Run Code Online (Sandbox Code Playgroud)
知道我做错了什么吗?我希望在输出中得到"Çirçös",但我得到' ir s'.
重新提出问题,因为
评论:这个问题已经获得了"流行的问题徽章",所以可能我不是唯一没有希望的人.:)
不幸的是,展示完整的问题堆栈导致了一个非常长的问题,这是非常梅森特定的.
首先,只有意见的部分:)
我使用HTML :: Mason多年,现在尝试使用Mason2.在诗人和梅森 都在CPAN最先进的框架.没有找到任何比较,开箱即用的东西允许写得如此干净/但非常黑客:)/ web-apps,包括许多电池(记录,缓存,配置管理,基于原生PGSI等......)
不幸的是,作者并不关心其余部分,例如默认情况下,它只是基于ascii, 没有任何手册,常见问题或建议:如何使用unicode
现在的事实.演示.创建一个诗人应用程序:
poet new my #the "my" directory is the $poet_root
mkdir -p my/comps/xls
cd my/comps/xls
Run Code Online (Sandbox Code Playgroud)
并添加到dhandler.mc下面(将说明两个基本问题)
<%class>
has 'dwl';
use Excel::Writer::XLSX;
</%class>
<%init>
my $file = $m->path_info;
$file =~ s/[^\w\.]//g;
my $cell = lc join ' ', "ÅNGSTRÖM", "in the", $file;
if( $.dwl ) …Run Code Online (Sandbox Code Playgroud) 例如,匹配"民族报"在""国际化"没有额外的模块,是否有可能在新的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) 为什么我得到"autodie"不同的输出?
#!/usr/bin/env perl
use warnings;
use 5.012;
use utf8;
use open ':encoding(utf-8)';
use open ':std';
open my $fh, '>', 'test.txt' or die $!;
say $fh 'käse';
close $fh;
open my $fh1, '<', 'test.txt' or die $!;
while ( my $row = readline( $fh1 ) ) {
print $row;
}
close $fh1;
use autodie;
open my $fh2, '<', 'test.txt';
while ( my $row = readline( $fh2 ) ) {
print $row;
}
close $fh2;
# Output:
# käse
# käse
Run Code Online (Sandbox Code Playgroud) 如何在python 3中使用正则表达式匹配任何语言的字母?
re.match([a-zA-Z]) 将匹配英语字符,但我希望同时支持所有语言.
我不希望匹配'in can't或underscores或任何其他类型的格式.我真希望我的正则表达式匹配:c,a,n,t,Å,é,和?.
在tchrists broilerplate中,我发现在END块中明确关闭了STDOUT.
END { close STDOUT }
Run Code Online (Sandbox Code Playgroud)
我知道结束并关闭,但我想知道为什么需要它.
当开始搜索它时,在perlfaq8中找到以下内容:
例如,您可以使用它来确保您的过滤器程序设法完成其输出而不填满磁盘:
END {
close(STDOUT) || die "stdout close failed: $!";
}
Run Code Online (Sandbox Code Playgroud)
并且无论如何也不理解.:(
有人可以解释(可能有一些代码示例):
我想为我自己的"默认使用"制作一个模块,例如:
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) 我在这里看到一条评论说所有解决方案charAt都是错误的.我无法完全理解并charAt在互联网上找到一些东西.当我查看源代码时,它只返回char数组中的一个元素.所以我的问题是,如果有任何问题或使用问题charAt?
评论就是这样
严格来说,所有基于charAt的解决方案都是错误的,因为charAt不会给你"字符",而是"代码单位",而且代码单元不是需要多个代码单元的字符和字符.
这里是一个很好的问题及精彩tchrist的答案 7 + 24条+ 52建议和意见,如何使Perl程序UTF8安全.
但这里是19k CPAN模块.什么是可以做的区分"好"和"坏"?(从utf8的角度来看)
例如:File::Slurp如果您将阅读该文件
#use strict encoding warnings utf8 autodie... etc....
my $str = read_file($file, binmode => ':utf8');
Run Code Online (Sandbox Code Playgroud)
您将根据命令行开关获得不同的结果,并且perl -CSDA不起作用.伤心.(是的,我知道比Encode :: decode("utf8",read_file($ file,binmode =>':raw'));会有所帮助,但无论如何都是SAD.
我的问题:
据我所知,CPAN模块很多都不需要了解utf8.但这里有zilion其他应该是什么.
拜托,不要误解我.我喜欢Perl语言.我知道perl具有非常强大的utf8功能.(特别是5.14).以上并不意味着perl批判 - 但我(也可能是其他一些人)需要知道什么是CPAN模块,以及如何对它们进行分类......)
在使用多个CPAN模块进行开发时,最初一切顺利,但在最终测试中,您发现某些模块不支持utf8,因此您的部分工作无用 - 这实际上可能会导致一点幻灭.:(
编辑:
据我所知,unicode周围的所有复杂事物都有两个根源:
我唯一的希望:perl6.是一种全新的,不同的语言.不需要保持任何向后兼容性.所以我希望,在perl6中默认一些事情是perl5中不可能做的事情,所有utf8事情都会更加直观.
但是,回到模块:@daxim告诉:"作者甚至不会透露他们的模块是否是污点安全的,这个功能存在了几十年!" - 这是一场灾难.也许(很可能,老实说也不知道怎么做),但也许我们到了那个时候,需要对CPAN提交提出更多更严格的限制.
在一方面,我对CPAN作者的志愿者作品非常满意.另一方面,发布源代码不仅仅是一个"正确"的言论自由 …
与此问题和此答案(另一个问题)相关,我仍然无法使用JSON处理UTF-8.
我已经尝试确保根据最好的专家的建议调用所有必需的巫术,并且据我所知,该字符串尽可能有效,标记并标记为UTF-8.但仍然perl死于其中任何一个
Uncaught exception: malformed UTF-8 character in JSON string
Run Code Online (Sandbox Code Playgroud)
要么
Uncaught exception: Wide character in subroutine entry
Run Code Online (Sandbox Code Playgroud)
我在这做错了什么?
(hlovdal) localhost:/work/2011/perl_unicode>cat json_malformed_utf8.pl
#!/usr/bin/perl -w -CSAD
### BEGIN ###
# Apparently the very best perl unicode boiler template code that exist,
# https://stackoverflow.com/questions/6162484/why-does-modern-perl-avoid-utf-8-by-default/6163129#6163129
# Slightly modified.
use v5.12; # minimal for unicode string feature
#use v5.14; # optimal for unicode string feature
use utf8; # Declare that this source unit is encoded as UTF?8. Although
# once …Run Code Online (Sandbox Code Playgroud)