Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Oldest file using -M

by ehdonhon (Curate)
on Apr 27, 2004 at 14:18 UTC ( #348495=note: print w/replies, xml ) Need Help??

in reply to Oldest file using -M

The thing with -M is that it measures the age of the files, in days since $^T, which is initially set by perl to the epoch time when your program started. When you are running in mod_perl, don't expect $^T to be anywhere near the the present time, because it will be set to the time when your apache child started up.

If you want to make sure that -M always returns a positive value (for all files created in the past), you need to do something like this:

$^T = time(); my $fileage = -M $filename;

However, in your case, it looks like all you care about is finding the oldest file in a list of files. That can be achieved easy enough:

my ($oldest_file) = sort { -M $b <=> -M $a } @filenames;

Update: merlyn correctly pointed out that setting $^T does not guarantee a positive return value for -M when the datestamp on a file is set to some time in the future.

Replies are listed 'Best First'.
•Re: Re: Oldest file using -M
by merlyn (Sage) on Apr 27, 2004 at 14:22 UTC
    If you want to make sure that -M always returns a positive value
    No, that's no guarantee. Using utime, I can set the modtime to any value I want, including far into the future. Or, I might have unpacked a file from a tar-archive from a system with a badly set system clock.

    So, you must always be prepared for a negative -M value. Always.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Re: Oldest file using -M
by Gerard (Pilgrim) on Apr 27, 2004 at 23:37 UTC
    Thanks for your comments ehdonhon, I decided not to go with the sort method as it shouldn't be as efficent as just checking the file size. Unfortunately effeciency is the key in this problem as there may be up to 50,000 files in a given directory.
    Thanks for your help, Gerard

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://348495]
[stevieb]: I am thankful that I fell into Perl years ago in the manner I did. I don't believe there is another language that people care about so much, that they're willing to give everything. Langs come and go, but in Perl, it seems, things remain consistent.
[stevieb]: The people back then (~2000) are the same people now, but with much more experience. I'm appreciative that I fell into the Perl rabbit hole.

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2017-02-25 01:20 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (364 votes). Check out past polls.