in reply to File::Tail issue..

If read returns with an error when interrupted by the signal, the loop exits so you never read from the new file.

If read doesn't return when interrupted by the signal, you don't use the new value of $file so you never read from the new file.

Ideally, the former occurs. Then you could do something like

use Errno qw( EINTR ); do { while (defined($line=$file->read)) { print $line; } } while $! != EINTR; # Go read from new file.

I don't have an obvious solution if the read doesn't return. Is this what happens?

Replies are listed 'Best First'.
Re^2: File::Tail issue..
by tuxtoti (Initiate) on Mar 20, 2009 at 17:54 UTC
    I think the problem is  read doesn't return. Because if it had retured the program would have terminated(becuase it must have exited the loop). But the program never ends. It does what File::Tail should be doing but just doesn't update it with the new file's contents.

      Searching the docs for "timeout" found a solution.

      # Check for new File::Tail object every $timeout seconds. my $timeout = 60; for (;;) { my (undef, undef, @pending) = File::Tail::select(undef, undef, undef, $timeout, $file); for (@pending) { my $line = $_->read(); print $line; } }

      Update: You could even simplify your signal handler (always a good thing) and your code in general as follows:

      use File::Tail; # Check for new File::Tail object every $timeout seconds. my $timeout = 60; my $qfn = $ARGV[0]; my $new = 1; my $changed = 0; my $tail; $SIG{INT} = sub { $changed = 1 }; for (;;) { if ($changed) { $changed = 0; open(my $fh, '<', '/tmp/filename') or die("Cannot open file: $!\n"); chomp( $qfn = <$fh> ); $new = 1; } if ($new) { $new = 0; $tail = File::Tail->new( name => $qfn, maxinterval => 3, adjustafter => 2, tail => -1, ); } my (undef, undef, @pending) = File::Tail::select(undef, undef, undef, $timeout, $tail); for (@pending) { my $line = $_->read(); print $line; } }
        Thanks ikegami. That worked neat. But I'm still wary of this disclaimer in the docs:

        "Note: Select functionality was added in version 0.9, and it required some reworking of all routines. ***PLEASE*** let me know if you see anything strange happening."
        Anyways thanks for helping me out. I'm going forward with this :)