【发布时间】:2016-11-16 06:49:31
【问题描述】:
我想编写一个 Perl 脚本,它从管道中捕获二进制数据并读取 Perl 内部的二进制数据并将接收到的二进制内容作为文件处理程序处理。
我能够从管道接收二进制内容,问题在于从 Perl 读取数据时二进制格式未正确保存。在 Perl 环境中,不保留 NUL 字符并将其转换为换行符。以下是命令行参数和示例
>more D:\Sample_binary.zip| perl readpipe.pl D:\sample_output.txt
readpipe.pl
local $/;
my $lines = <STDIN>; # Read the binary data from pipe
open my $IN, "+<", \$lines; # Load the content as file handler
$zip = Archive::Zip->new;
$zip->readFromFileHandle ($IN); # Read ZIP file from the received binary data
【问题讨论】:
-
这是一个useless use of
more(其中more甚至比cat更无用,因为它仅对交互使用有用)。 Perl cam 无需使用外部程序就可以很好地读取标准输入。 -
但是从 Perl 中读取存档可能会简单得多。只需将输入文件名参数传递给 Perl 脚本。
-
是的。当我们使用
catcommand 时,能够读取二进制内容。我目前正在 WINDOWS 平台上检查这个。所以我尝试使用type命令并获取原始二进制数据。谢谢!! -
>type D:\Sample_binary.zip| perl readpipe.pl D:\sample_output.txt。现在我可以从管道加载 zip 文件了。 -
管道只是通过 STDIN 传入数据的一种方式。你也可以这样做:
perl readpipe.pl D:\sample_output.txt < D:\Sample_binary.zip