in reply to Using File::Tail on a unicode file

The following hack seems to work:

#!/usr/bin/perl -w use strict; use File::Tail; use IO::Handle; use Encode; #use Devel::Peek; my $scan_log_file = "foo.u16"; unless (fork) { # emulate Windows file for testing open my $fh, ">:encoding(UTF-16le):crlf", $scan_log_file or die $! +; $fh->autoflush(1); for (1..5) { print $fh "foo bar\n"; sleep 1; } print $fh "finished\n"; exit; } else { sleep 1; my $o_tail=File::Tail->new( name => $scan_log_file, maxinterval => 1, adjustafter => 1, ignore_nonexistant => 1, reset_tail => -1, tail => -1, ); while( my $line = $o_tail->read() ) { $line =~ s/^\0//; # fix possible char misalignment $line = decode("UTF-16le", $line); $line =~ s/\r$//; # remove \r #Dump $line; print "$line\n"; if( $line =~ m/finished/ ) { print "done.\n"; last; } } }

Replies are listed 'Best First'.
Re^2: Using File::Tail on a unicode file
by chrestomanci (Priest) on Mar 07, 2012 at 15:00 UTC

    Thank you, that was exactly what I was looking for. ++

    Unfortunately I can't use it because I have just discovered that File::Tail does not work under windows, or at least not under Active State perl 5.12. It is not available in their package repository, and when I tried to install it by hand most of the tests failed.

    Instead I have switched to a simpler algorithm that just monitors the file mtime, and if it changes, reads the entire file. It is inelegant but works.