perl中的无限循环

时间:2012-10-24 20:17:17

标签: perl

我想从tcp流文件中提取除http头之外的内容 内容如下 当满足两个^ M时,http标头结束

HTTP/1.1 200 OK^M 
Last-Modified: Sat, 20 Mar 2010 09:43:12 GMT^M 
Content-Type: video/x-flv^M 
Date: Wed, 24 Oct 2012 14:34:13 GMT^M 
Expires: Wed, 24 Oct 2012 14:34:13 GMT^M 
Cache-Control: private, max-age=22124^M 
Accept-Ranges: bytes^M 
Content-Length: 29833281^M 
Connection: close^M 
X-Content-Type-Options: nosniff^M 
Server: gvs 1.0^M 
^M 
FLV^A^E^@^@^@   ^@^@^@^@^R^@^CK^@^@^@^@^@^@^@^B^@ 
onMetaData^H^@^@^@^O^@^Hduration^@@i<97> 
=p£×^@  starttime^@^@^@^@^@^@^@^@^@^@^Mtotalduration^@@i<97>

我的提取代码如下, 然后我运行:extract.pl&lt; tcp.flow 但似乎循环是无止境的, 这些代码有什么问题?谢谢!

#!/usr/bin/perl 
$start=0; 
$data=""; 
while(<STDIN>) 
{ 
    if ( $start eq 0 && $_ =~ /^\r\n/) { $start = 1; } 
    elsif ( $start eq 1 ) { $data = $data . $_; } 
} 
open(FH, ">sample.flv"); 
print FH $data; 
close(FH);

2 个答案:

答案 0 :(得分:1)

在读取数据之前在STDIN上调用binmode(),文件内容可能会干扰文件读取。在写入数据之前,您也希望在FH上使用它。 HTH

答案 1 :(得分:1)

这是一个单行。然而,我认为没有理由无限循环。

perl -00 -lne '$i++ and print' file > sample.flv

哪个堕落者看起来像这样:

>perl -MO=Deparse -00 -lne '$i++ and print' input.txt
BEGIN { $/ = ""; $\ = "\n\n"; }        # from -l and -00
LINE: while (defined($_ = <ARGV>)) {   # from -n
    chomp $_;                          # from -l, removes "\n\n" now
    print $_ if $i++;                  # skips the first line
}
-e syntax OK

如果您需要先清理文件,请执行

perl -pi -le 's/[\r\n]+$//' input.txt
相关问题