softcraft has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks,
I am currently working on a perl script to tail files located in a directory. The script tails a specific file at the beginning and changes to another file (file name is YYYMMDD) once the system date changes. I wrote the following script:
my $file=File::Tail->new(name=>$name, name_changes=>&change, interval +=>1, maxinterval=>5, adjustafter=>7); while (defined($line=$file->read)){ .. } sub change{ my $date = sprintf("%04d%02d%02d", (localtime(time))[5] + 1900,(localt +ime(time))[4]+1, (localtime(time))[3]); return $date; }

But the script doesn't work. I wonder whether I have not use correctly name_changes in File::Tail?

Thank you

Replies are listed 'Best First'.
Re: Using File::Tail with name_changes
by FunkyMonk (Bishop) on Jul 30, 2007 at 11:55 UTC
    From File::Tail:

    Instead of trying to implement various clever detection methods, File::Tail will call the code reference defined in name_changes. The code reference should return the string which is the new name of the file to try opening.
    You're using a sub call name_changes => &change and you should be using a code ref: name_changes => \&change (note the backslash).

      Thanks, it's working now!
      I got another problem when I use strict refs. The error came out was "Can't use string as a symbol ref while "strict refs" in use. It came from the ref used in name_changes=>\$change. Is there alternative to this?
        Is \$ a typo? It was supposed to be \& -- a backslash followed by an ampersand. I don't see how that (or any of code in your OP) would give you that error message though.