首页 \ 问答 \ perl文件句柄while while循环不会在没有其他行的情况下终止(perl filehandle while loop does not terminate on no further lines)

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.

更新时间:2022-07-28 20:07

相关文章

更多

最新问答

更多
  • 如何在Laravel 5.2中使用paginate与关系?(How to use paginate with relationships in Laravel 5.2?)
  • linux的常用命令干什么用的
  • 由于有四个新控制器,Auth刀片是否有任何变化?(Are there any changes in Auth blades due to four new controllers?)
  • 如何交换返回集中的行?(How to swap rows in a return set?)
  • 在ios 7中的UITableView部分周围绘制边界线(draw borderline around UITableView section in ios 7)
  • 使用Boost.Spirit Qi和Lex时的空白队长(Whitespace skipper when using Boost.Spirit Qi and Lex)
  • Java中的不可变类(Immutable class in Java)
  • WordPress发布查询(WordPress post query)
  • 如何在关系数据库中存储与IPv6兼容的地址(How to store IPv6-compatible address in a relational database)
  • 是否可以检查对象值的条件并返回密钥?(Is it possible to check the condition of a value of an object and JUST return the key?)
  • GEP分段错误LLVM C ++ API(GEP segmentation fault LLVM C++ API)
  • 绑定属性设置器未被调用(Bound Property Setter not getting Called)
  • linux ubuntu14.04版没有那个文件或目录
  • 如何使用JSF EL表达式在param中迭代变量(How to iterate over variable in param using JSF EL expression)
  • 是否有可能在WPF中的一个单独的进程中隔离一些控件?(Is it possible to isolate some controls in a separate process in WPF?)
  • 使用Python 2.7的MSI安装的默认安装目录是什么?(What is the default installation directory with an MSI install of Python 2.7?)
  • 寻求多次出现的表达式(Seeking for more than one occurrence of an expression)
  • ckeditor config.protectedSource不适用于editor.insertHtml上的html元素属性(ckeditor config.protectedSource dont work for html element attributes on editor.insertHtml)
  • linux只知道文件名,不知道在哪个目录,怎么找到文件所在目录
  • Actionscript:检查字符串是否包含域或子域(Actionscript: check if string contains domain or subdomain)
  • 将CouchDB与AJAX一起使用是否安全?(Is it safe to use CouchDB with AJAX?)
  • 懒惰地初始化AutoMapper(Lazily initializing AutoMapper)
  • 使用hasclass为多个div与一个按钮问题(using hasclass for multiple divs with one button Problems)
  • Windows Phone 7:检查资源是否存在(Windows Phone 7: Check If Resource Exists)
  • 无法在新线程中从FREContext调用getActivity()?(Can't call getActivity() from FREContext in a new thread?)
  • 在Alpine上升级到postgres96(/ usr / bin / pg_dump:没有这样的文件或目录)(Upgrade to postgres96 on Alpine (/usr/bin/pg_dump: No such file or directory))
  • 如何按部门显示报告(How to display a report by Department wise)
  • Facebook墙贴在需要访问令牌密钥后无法正常工作(Facebook wall post not working after access token key required)
  • Javascript - 如何在不擦除输入的情况下更改标签的innerText(Javascript - how to change innerText of label while not wiping out the input)
  • WooCommerce / WordPress - 不显示具有特定标题的产品(WooCommerce/WordPress - Products with specific titles are not displayed)