in reply to how to list the files in dir with respect to time

You could sort by mtime from stat or, better, apply List::Util::reduce() to the filename list. We'll insert a Schwartzian Transform-like mapping to reduce the number of stat calls.

use List::Util 'reduce'; my $newest = ( reduce { $a->[1] < $b->[1] ? $b : $a } map { [ $_, (stat)[9] ] } glob '/path/to/*.log' )->[0];
Untested.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: how to list the files in dir with respect to time
by graff (Chancellor) on Aug 02, 2005 at 03:18 UTC
    The List::Util stuff is definitely cool, but I don't quite understand why you say it reduces the number of stat calls. I would have thought that you need to stat every candidate file just once, whether you use List::Util 'reduce' or something like the more mundane hash:
    my %file_date; for ( glob '/path/to/*.log' ) { $file_date{$_} = (stat)[9]; } my $newest = ( sort { $file_date{$b} <=> $file_date{$a} } keys %file_a +ge )[0];

    (update: changed name of hash to be consistent with what it stores)

      The map reduces the number of stat calls by half. Compare to:

      use List::Util 'reduce'; my $newest = reduce { (stat $a)[9] < (stat $b)[9] ? $b : $a } glob '/path/to/*.log';

      It's not possible to use the *_ handle for a cached stat call there, because we don't know that the currently reduced name was the subject of the most recent stat call.

      Your hash approach is fine, too, but it would also benefit from reduce() over sort.

      After Compline,
      Zaxo

      Thanks zaxo! You beat me to the punch :)

      I was confused too why zaxo said it will reduce number of stat calls. I am yet to familiarize myself with ST so i will read up on that before I ask any questions regarding that :)

      Seems like the OP wanted the last modified file. So why store all file and then sort instead of a simple max?

      Modifying graff's code here

      Update : Added logic to update $max. Thanks graff. my oversight.

      my %file_date; my $max = -99999999; # set it to first file's mtime would be better # but just for demonstration here my $mtime; my $file; for ( glob '/path/to/*.log' ) { $mtime = (stat)[9]; if ($max <= $mtime) { $file = $_; $max = $mtime; } print ("file = $file\n"); # my $newest = ( sort { $file_date{$b} <=> $file_date{$a} } keys %file +_age )[0];
        Good point about just keeping the most recent file seen so far. But you need to make sure that you update $max each time you reset the value of $file.
Re^2: how to list the files in dir with respect to time
by xdg (Monsignor) on Aug 02, 2005 at 11:29 UTC

    Regarding OP's question about finding the modification time, in addition to stat, also worth knowing about File::stat for a more readable wrapper and not having to remember or lookup that 9 means mtime.

    map { [ $_, stat($_)->mtime ] }

    This is, of course, an efficiency/readability tradeoff, but this may be acceptable in many cases.

    OP may want to see Understanding transformation sorts (ST, GRT), the details if unfamiliar with the Schwartzian Transform<./p>

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.