Jean-Marc
Jean-Marc

Reputation: 183

Strawberry perl - input file streaming issue

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 ?

Upvotes: 3

Views: 99

Answers (2)

Jean-Marc
Jean-Marc

Reputation: 183

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" . ':';
}

Upvotes: 2

ikegami
ikegami

Reputation: 385779

Use File::Tail.

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

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

Upvotes: 2

Related Questions