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

hello, i there a way to glob by timestamp instead of by filename? it seems like a fairly straightforward thing to want to do. thanks for your help.

Replies are listed 'Best First'.
Re: globbing by timestamp
by tadman (Prior) on Aug 10, 2001 at 04:43 UTC
    I'm not exactly sure what you mean, but perhaps this is along the lines of what you were thinking:
    foreach (sort {-M $a <=> -M $b} <*>) { # ... }
    This will, in theory anyway, sort based on last modification time. -M returns a floating point value that is "days since last modification", and can be something like 0.2551 depending on your file.

    If, instead, you mean you want to get all files within a certain modification period, you might want to go about it this way:
    $oldest = 5; # Days $newest = 1; foreach (<*>) { next unless (-M $_ < $oldest && -M $_ > $newest); # Do stuff }
    Of course, you could condense this into a list with grep, something like:
    $oldest = 5; # Days $newest = 1; foreach (grep {-M $_ < $oldest && -M $_ > $newest} <*>) { # Do stuff }
    Typically, though, you would likely use readdir instead of a glob for this kind of read.
Re: globbing by timestamp
by trantor (Chaplain) on Aug 10, 2001 at 10:24 UTC

    Globbing implemented in Perl and many other shells works with file names only.

    So you can say <*.c> to match all the files ending in .c there's no direct notation to match, say, all the files created in 2001.

    Use File::Find instead, you'll be able to match files according to their timestamps (three timestamps per file, actually) and much more.

    -- TMTOWTDI