php - 管道输入到perl进程自动解码url编码的字符串
问题描述:
我使用proc_open
管道一些文本到perl脚本进行更快的处理。该文本包含url编码的字符串以及字面空格。当一个URL编码的空间出现在原始文本中时,它在到达perl脚本时似乎被解码为一个文字空间。在perl脚本中,我依赖于文字空间的位置,所以这些不需要的空间会混淆我的输出。php - 管道输入到perl进程自动解码url编码的字符串
这是怎么发生的,有没有办法阻止它发生?
相关的代码片段:
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
);
$cmd = "perl script.pl";
$process = proc_open($cmd, $descriptorspec, $pipes);
$output = "";
if (is_resource($process)) {
fwrite($pipes[0], $raw_string);
fclose($pipes[0]);
while (!feof($pipes[1])) {
$output .= fgets($pipes[1]);
}
fclose($pipes[1]);
proc_close($process);
}
和原始文本输入的行看起来是这样的:
key url\tvalue1\tvalue2\tvalue3
我也许可以通过转换我输入的格式,以避免这个问题,但由于各种原因,这是不可取的,并绕过而不是解决,关键问题。
此外,我知道问题发生在php脚本和perl脚本之间,因为在将它写入perl脚本STDIN管道之前,我已经检查了原始文本(使用echo
),并且测试了我的perl脚本直接使用url编码的原始字符串。
我已经在下面添加了perl脚本。它基本上归结为一个迷你地图减少工作。
use strict;
my %rows;
while(<STDIN>) {
chomp;
my @line = split(/\t/);
my $key = $line[0];
if (defined @rows{$key}) {
for my $i (1..$#line) {
$rows{$key}->[$i-1] += $line[$i];
}
} else {
my @new_row;
for my $i (1..$#line) {
push(@new_row, $line[$i]);
}
$rows{$key} = [ @new_row ];
}
}
my %newrows;
for my $key (keys %rows) {
my @temparray = split(/ /, $key);
pop(@temparray);
my $newkey = join(" ", @temparray);
if (defined @newrows{$newkey}) {
for my $i (0..$#{ $rows{$key}}) {
$newrows{$newkey}->[$i] += $rows{$key}->[$i] > 0 ? 1 : 0;
}
} else {
my @new_row;
for my $i (0..$#{ $rows{$key}}) {
push(@new_row, $rows{$key}->[$i] > 0 ? 1 : 0);
}
$newrows{$newkey} = [ @new_row ];
}
}
for my $key (keys %newrows) {
print "$key\t", join("\t", @{ $newrows{$key} }), "\n";
}
答
注意自我:总是检查你的假设。事实证明,在我的数亿行输入中的某个地方,实际上是字面空格,其中应该有url编码的空格。花了一段时间才找到它们,因为有数亿个正确的字面空间,但它们在那里。
对不起!
'fwrite'调用之前'echo($ raw_string)'看看它说了什么 – mob
我已经完成了,就像我在上一段中提到的那样。虽然谢谢!我会更加清楚的是,我在写作之前立即检查了原始字符串。 – Cyan
perl脚本是做什么的?你能展示它如何读取输入数据吗? – xxfelixxx