Will there be data in the queue for pipes to read Perl?

I have a Perl script that runs a lengthy process and watches its command line output (log messages), some of which have multiple lines. After he has a full log message, he sends it for processing and captures the next log message.

open(PS_F, "run.bat |") or die $!;

$logMessage = "";

while (<PS_F>) {
    $lineRead = $_;

    if ($lineRead =~ m!(\d{4}-\d{2}-\d{2}\ \d{2}:\d{2}:\d{2})!) {   
        #process the previous log message

        $logMessage = $lineRead;
    }
    else {
        $logMessage = $logMessage.$_;
    }
}

close(PS_F);

In its current form, do I have to worry about reading and processing "backup" strings? For example, if I get a new log message every 1 second, and it takes 5 seconds to complete all the processing (random numbers that I pulled out), do I need to worry about skipping log messages or memory problems?

+3
3

, . , (.. ), . , Linux ( ) 65536 .

, , - .

+9

, . , .

+4

Strictly speaking, this should be a comment: Please consider rewriting your code as

# use lexical filehandle and 3 arg open

open my $PS_F, '-|', 'run.bat'
    or die "Cannot open pipe to 'run.bat': $!";

# explicit initialization not needed
# limit scope

my $logMessage;

while (<$PS_F>) { 
    # you probably meant to anchor the pattern
    # and no need to capture if you are not going to use
    # captured matches
    # there is no need to escape a space, although you might
    # want to use [ ] for clarity

    $logMessage = '' if m!^\d{4}-\d{2}-\d{2}[ ]\d{2}:\d{2}:\d{2}!;
    $logMessage .= $_;
}

close $PS_F
    or die "Cannot close pipe: $!";
+4
source

Source: https://habr.com/ru/post/1722040/


All Articles