开发者

perl script keeps reading data from STDIN

开发者 https://www.devze.com 2023-02-01 02:00 出处:网络
I have 2 scripts for a task. The 1st outputs lines of data (terminated with RT/LF) to STDOUT now and then.

I have 2 scripts for a task.

The 1st outputs lines of data (terminated with RT/LF) to STDOUT now and then.

The 2nd keeps reading data from STDIN for further processing in the following way:

use strict; 
my $dataline; 
while(1) { 
    $dtaline = ""; 
    $dataline = <STDIN>; 
    开发者_高级运维until( $dataline ne "") { 
        sleep(1); 
        $dataline = <STDIN>; 
    }

    #further processing with a non-empty data line follows   
} 

print "quitting...\n";

I redirect the output from the 1st to the 2nd using pipe as following: perl scrt1 |perl scpt2.

But the problem I'm having with these 2 scpts is that it looks like that the 2nd scpt keeps getting the initial load of lines of data from the 1st scpt if there's no data anymore after the initial load.

Wonder if anybody having similar experiences can kindly help a bit?

Thanks.


You seem to be making this much more complicated than it needs to be. Perl normally uses blocking I/O, which means that <STDIN> won't return until there's a complete line of input.

use strict; 
use warnings; # use this too

while (my $dataline = <STDIN>) {
  #further processing with a non-empty data line follows   
}

print "quitting...\n";

When there's no more input (in your example, when scrt1 exits), <STDIN> returns undef, which will exit the while loop. (Perl adds an implicit defined test to while ($var = <>) loops.)

0

精彩评论

暂无评论...
验证码 换一张
取 消