Dennis Nolte Dennis Nolte - 1 year ago 68
Perl Question

perl filehandle while loop does not terminate on no further lines

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

#script to analyse data
use warnings;
use strict;
use POSIX ();

sub stop_stream($){
my $pid = shift;
my $cmd = "/bin/kill -9 $pid";

while (1){

my $steampid = open( STREAM, "/usr/local/bin/stream |" );

STREAM: while ( my $row = <STREAM> ) {
chomp $row;
my @o = split /\s+/, $row;

#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";

stop_stream( $steampid );
close STREAM;


Resources i tried to figure out the issue:

and numerous others.

I tried stackoverflow with some combination of "perl while loop close filehandle" to no success.

Answer Source

OK, the root of the problem here is that a while ( <FILEHANDLE> ) { loop will block if the file handle is open but there is no data to be read.

So it's likely that /usr/local/bin/stream continues piping data until killed - and so your reads block.

The easy solution is to use something like IO::Select which has can_read as an option:

use IO::Select;
open ( my $stream, '-|', '/usr/local/bin/stream' ) or die $!; 
#register this filehandle with a new select object
my $select = IO::Select -> new ( $stream );

#2s stream timeout. 
#works because can_read returns a list of filehandles ready for reading
#with only one filehandle registered, it can be used as a simple logic test. 
while ( $select -> can_read(2) ) { 
    my $row = <$stream>; 

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download