Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/perl/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在Perl中测试查询字符串unicode处理_Perl_Testing_Unicode_Query String - Fatal编程技术网

在Perl中测试查询字符串unicode处理

在Perl中测试查询字符串unicode处理,perl,testing,unicode,query-string,Perl,Testing,Unicode,Query String,当我在Unicode问题上遇到难题时,我正试图编写一个测试查询字符串解析的示例。简而言之,字母“Omega”(Ω)似乎没有被正确解码 Unicode:U+2126 3字节序列:\xe2\x84\xa6 URI编码:%E2%84%A6 所以我编写了这个测试程序来验证我可以用URI::Encode“解码”unicode查询字符串 use strict;

当我在Unicode问题上遇到难题时,我正试图编写一个测试查询字符串解析的示例。简而言之,字母“Omega”(Ω)似乎没有被正确解码

  • Unicode:U+2126
  • 3字节序列:\xe2\x84\xa6
  • URI编码:%E2%84%A6
所以我编写了这个测试程序来验证我可以用URI::Encode“解码”unicode查询字符串

use strict;                                                                                                                                                                    
use warnings;
use utf8::all;    # use before Test::Builder clones STDOUT, etc.
use URI::Encode 'uri_decode';
use Test::More;

sub parse_query_string {
    my $query_string = shift;
    my @pairs = split /[&;]/ => $query_string;

    my %values_for;
    foreach my $pair (@pairs) {
        my ( $key, $value ) = split( /=/, $pair );
        $_ = uri_decode($_) for $key, $value;
        $values_for{$key} ||= [];
        push @{ $values_for{$key} } => $value;
    }
    return \%values_for;
}

my $omega = "\N{U+2126}";
my $query = parse_query_string('alpha=%E2%84%A6');
is_deeply $query, { alpha => [$omega] }, 'Unicode should decode correctly';

diag $omega;
diag $query->{alpha}[0];

done_testing;
以及测试的输出:

query.t .. 
not ok 1 - Unicode should decode correctly
#   Failed test 'Unicode should decode correctly'
#   at query.t line 23.
#     Structures begin differing at:
#          $got->{alpha}[0] = 'â¦'
#     $expected->{alpha}[0] = 'Ω'
# Ω
# â¦
1..1
# Looks like you failed 1 test of 1.
Dubious, test returned 1 (wstat 256, 0x100)
Failed 1/1 subtests 

Test Summary Report
-------------------
query.t (Wstat: 256 Tests: 1 Failed: 1)
  Failed test:  1
  Non-zero exit status: 1
Files=1, Tests=1,  0 wallclock secs ( 0.03 usr  0.01 sys +  0.05 cusr  0.00 csys =  0.09 CPU)
Result: FAIL

在我看来,URI::Encode可能在这里被破坏,但是切换到URI::Escape并使用URI_unescape函数会报告相同的错误。我遗漏了什么?

URI编码的字符只是表示utf-8序列,而URI::Encode和URI::Escape只是将它们解码为utf-8字节字符串,它们都没有将ByTestRing解码为utf-8(这是通用URI解码库的正确行为)

换句话说,您的代码基本上可以:
是“\N{U+2126}”,“\xe2\x84\xa6”
,这将失败,因为经过比较,perl将后者升级为3个字符长的拉丁-1字符串


您必须在
uri\u decode
之后使用
Encode::decode\u utf8
手动解码输入值,或者比较编码的utf8字节序列。

uri转义表示八位字节,对字符编码一无所知,因此您必须自己将UTF-8八位字节解码为字符,例如:

$_ = decode_utf8(uri_decode($_)) for $key, $value;

我建议你仔细研究一下这个话题

我想补充一下这里的讨论:

  • 你会注意到页面上有很多奇怪的字形。这是作者故意的
  • 我试过线程中推荐的Symbola字体,在Win7上看起来很糟糕。YMMV
  • 阅读为什么现代Perl默认避免UTF-8?过于频繁可能会导致抑郁和对你的生活选择的长期怀疑

您可以在自己对问题的解释中看到错误的细节。你要处理的是:

  • Unicode码点:U+2126
  • 代码点的UTF-8编码:\xe2\x84\xa6
  • 代码点UTF-8编码的URI编码:%E2%84%A6
问题是您只取消了其中一个编码


已经提出了解决办法。我只是想提出另一种解释。

CGI模块提供了。这正如预期的那样工作:
perl-e'use CGI qw(-utf8);my$cgi=cgi->new(“alpha=%E2%84%A6”);使用Devel::Peek;Dump$cgi->param(“alpha”)”
注意文档中提到的警告。我以前读过,我认为tchrist的反应非常好。