简体   繁体   中英

perl script keeps reading data from STDIN

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.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM