in reply to File::find hack

If you're looking for a way to find the "newest" file, without looking at all the files (or at least, their metadata), there isn't.

You could do something like (untested):

use File::Spec; my %cache; foreach my $file (`find d:\\Teszt -type f`) { my $age = -M $file; my ($vol, $dir, $name) = File::Spec->splitpath($file); if (exists $cache{$vol, $dir}) { next if $age > $cache{$vol,$dir}[1]; } $cache{$vol,$dir} = [$file, $age]; } foreach my $newest_file_in_directory (map {$$_[0]} values %cache) { ... process file ... }
I'm using `find` because I've never had a need to learn the File::Find syntax.

Replies are listed 'Best First'.
Re^2: File::find hack
by nemesdani (Friar) on Feb 25, 2012 at 12:03 UTC
    Thank you for the answer. However maybe I wasn't clear enough. I know that once I reached the subdir, I have to check all the files there. I just don't like using last.

      I just don't like using last.

      As well you shouldn't, at least in this case. 'last' is not a correct way of exiting a subroutine, and Perl should actually throw a warning if you run your code. The correct way of coming back from a sub is 'return', not 'last'.

      -- 
      I hate storms, but calms undermine my spirits.
       -- Bernard Moitessier, "The Long Way"