perl文件句柄while while循环不会在没有其他行的情况下终止(perl filehandle while loop does not terminate on no further lines)
以下设置:
linux debian based 4.4.14-v7+ armv7l GNU/Linux perl version is v5.20.2 built for arm-linux-gnueabihf-thread-multi-64int
一个Perl脚本应该读取数据流(十六进制字符,每行不同长度)示例流输出:
00 AA BB 11 22 33 44 ... 00 AB BB 11 22 33 44 ...
根据具体值,脚本应执行特定操作。 工作正常但是当流停止发送数据时,即流完成发送数据,while循环不会停止。 并且脚本等待更多行。
流本身发送fe 5秒的数据,然后分析脚本应该do_stuff;
一旦分析脚本完成计算,它将再次启动流。
但是,我无法弄清楚为什么“下一个命令”没有被执行,或者while循环没有在没有更多的行上正确终止。
如果我再次手动启动流处理,分析脚本将继续正常,直到再没有更多行。
简化代码
#!/usr/bin/perl #script to analyse data use warnings; use strict; use POSIX (); sub stop_stream($){ my $pid = shift; my $cmd = "/bin/kill -9 $pid"; system($cmd); } while (1){ my $steampid = open( STREAM, "/usr/local/bin/stream |" ); STREAM: while ( my $row = <STREAM> ) { chomp $row; my @o = split /\s+/, $row; #do_stuff; #.. #this is the row on which the script hangs until it get's new lines in the filehandle. next STREAM if $o[1] ne "AA"; #... #do_other_stuff; #... } stop_stream( $steampid ); close STREAM; }
资源我试图找出问题:
http://perldoc.perl.org/perlsyn.html#Compound-Statements
http://www.perlmonks.org/?node_id=1065701
和许多其他人。
我尝试使用“perl while loop close filehandle”的某种组合进行stackoverflow,但没有成功。
following setup:
linux debian based 4.4.14-v7+ armv7l GNU/Linux perl version is v5.20.2 built for arm-linux-gnueabihf-thread-multi-64int
A Perl script which should read a stream of data (hex chars, different length per line) example stream output:
00 AA BB 11 22 33 44 ... 00 AB BB 11 22 33 44 ...
Depending on specific values the script should do specific actions. Works fine however when the stream stops sending data, i.e. stream is finished sending data, the while loop does not get stopped. and the script waits for more lines.
The stream itself sends f.e. 5 sec of data, and then the analyse script should do_stuff;
once the analyse script is finished with the calculations, it will start the stream again.
However i am unable to figure out the reason why either the "next command" is not executed, or the while loop does not terminate correctly on no further lines.
If i manually start the stream process again the analyse script continues just fine until there are no more lines again.
simplified Code
#!/usr/bin/perl #script to analyse data use warnings; use strict; use POSIX (); sub stop_stream($){ my $pid = shift; my $cmd = "/bin/kill -9 $pid"; system($cmd); } while (1){ my $steampid = open( STREAM, "/usr/local/bin/stream |" ); STREAM: while ( my $row = <STREAM> ) { chomp $row; my @o = split /\s+/, $row; #do_stuff; #.. #this is the row on which the script hangs until it get's new lines in the filehandle. next STREAM if $o[1] ne "AA"; #... #do_other_stuff; #... } stop_stream( $steampid ); close STREAM; }
Resources i tried to figure out the issue:
http://perldoc.perl.org/perlsyn.html#Compound-Statements
http://www.perlmonks.org/?node_id=1065701
and numerous others.
I tried stackoverflow with some combination of "perl while loop close filehandle" to no success.