Strawberry perl - input file streaming issue

87 Views Asked by At

I have some perl scripts that stream input file and parse it in live.

It worked well until Strawberry perl version 5.32.1.1, but it does not work anymore with 5.38.0.1.

I wrote a basic example showing the issue:

#!/usr/bin/perl -w

use strict;
use warnings;
my $IN_STREAM;

open( $IN_STREAM, "<", "file_in.txt" ) or die "Fail to open file_in.txt";

while (1) {
    while ( !($_ = <$IN_STREAM>)) {
        sleep(1);
    }
    print "$_";
}

When I run this script with version version 5.32.1.1, I can see the print each time I update file_in.txt.

But with 5.38.0.1, updates in file_in.txt are NOT detected.

Is there a different method to do what I want ?

2

There are 2 best solutions below

0
ikegami On

Use File::Tail.

my $tail = File::Tail->new( name => "file_in.txt" );

while ( defined( my $line = $tail->read() ) ) {
   print $line;
}
1
Jean-Marc On

Another solution from https://github.com/StrawberryPerl/Perl-Dist-Strawberry/issues/166 Need to add clearerr()

#!/usr/bin/perl -w

use strict;
use warnings;

open( my $IN_STREAM, "<", "file_in.txt" ) or die "Fail to open file_in.txt";

$| = 1;

my $str;
while (1) {
    while ( !($str = <$IN_STREAM>)) {
        $IN_STREAM->clearerr();
        sleep(1);
    }
    print "$str" . ':';
}