Search code examples
perltail

Perl File::Tail with a symbolic link


BACKGROUND

I am using File::Tail to tail a log file symbolic link. The symbolic link gets updated after midnight to include a new date stamp, which unfortunately my script does not tail the new file after the symbolic link is updated. Otherwise, my script works as intended.

#!/usr/bin/env perl

use strict;
use warnings;
use utf8;
use Data::Dumper;
use charnames':full';
use Cwd 'abs_path';
use File::Tail;

my $symlink = sub {
my($log) = '/home/user/log'; 
};

my $file=File::Tail->new(
        name=>&$symlink,
        ignore_nonexistant=>1,
        tail=>0,
        interval=>0,
        maxinterval=>1,
        name_changes=>\&$symlink
        ) || warn $!;
print Dumper $file;

while (defined($_=$file->read)) {
# do a bunch of stuff;
}

QUESTION

How do I get perl to follow the updated symbolic link?


Solution

  • I was missing a return from the sub

    my $symlink = sub {
    my($log) = '/home/user/log'; 
    return $log;
    };
    

    Works perfectly now!