Using name_changes in File::Tail

For those wondering how to use the name_changes parameter of the File::Tail module, here is how I did.

First, define a variable holding your sub. Here, I just get the file that was modified last, matching some pattern.
[sourcecode language=’perl’]
my $meslog = sub {
my $logfile = `ls -ltr $dir2mon |grep messrv_2 |tail -1 |awk {‘print $9′}`;
return $dir2mon . “/” . $logfile;
};
[/sourcecode]

Then, in the initialization of the File::Tail object, specify it as such (with other parameters if you wish).
[sourcecode language=’perl’]
my $file2mon = File::Tail->new(
name=>&$meslog,
resetafter=>20,
name_changes=>&$meslog ) || warn $!;
[/sourcecode]

Perl is then giving you a code reference that you can use.

That way, you should not get any “Not a CODE reference” error anymore, or other “Can’t use string (“/some/file/some/where”…) as a subroutine ref while “strict refs” in use”.