Action Based on File's Last Modification Time: Is One Approach Better?

Discussion in 'Perl Misc' started by James E Keenan, Oct 14, 2003.

  1. Let's say that I wish to process every file in a given directory that was
    last modified more than 14 days ago. From various parts of the Camel book,
    it appears I could test the age of the file in 1 of 2 ways.

    ### approach using 'stat' ### Camel pp 800-801
    my ($file, $days_ago, $DAY, $earlier);
    $DAY = 60 * 60 * 24;
    $days_ago = 14;
    $earlier = time() - ($days_ago * $DAY);
    ....
    if ( (stat($file))[9] < $earlier ) {
    # process $file
    }

    ### approach using file test operator '-M' ### Camel pp 98-100
    if (-M $file > 14) {
    # process $file
    }

    ### [end code samples] ###
    For the purpose of argument, let's assume that the "at the point the Perl
    script started running" proviso for the file test operator is not meaningful
    from the current time. Given that assumption, is there any particular
    reason to prefer the more verbose approach using 'stat' to the simple one
    using the '-M'?

    jimk
    James E Keenan, Oct 14, 2003
    #1
    1. Advertising

  2. James E Keenan <> wrote:
    > Let's say that I wish to process every file in a given directory
    > that was last modified more than 14 days ago. From various parts
    > of the Camel book, it appears I could test the age of the file in
    > 1 of 2 ways.


    [ using stat() and -M ]

    > For the purpose of argument, let's assume that the "at the point
    > the Perl script started running" proviso for the file test operator
    > is not meaningful from the current time.


    This is what trips people, though, when they use it in long-running
    daemons, mod_perl handlers, etc.

    > Given that assumption, is there any particular reason to prefer the
    > more verbose approach using 'stat' to the simple one using the '-M'?


    No. -M is simpler, easier to read, and probably a bit faster.

    --
    Steve
    Steve Grazzini, Oct 14, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manuel Bastioni

    Get the date of last modification of a file

    Manuel Bastioni, Sep 8, 2003, in forum: Python
    Replies:
    6
    Views:
    2,028
    Manuel Bastioni
    Sep 9, 2003
  2. John
    Replies:
    0
    Views:
    842
  3. Replies:
    18
    Views:
    543
    Matthew Woodcraft
    Apr 28, 2009
  4. Panagiotis Atmatzidis
    Replies:
    1
    Views:
    127
    Robert Klemme
    May 2, 2011
  5. Bart Van der Donck

    Last "real" modification date of file

    Bart Van der Donck, Jun 18, 2008, in forum: Perl Misc
    Replies:
    3
    Views:
    158
    Bart Van der Donck
    Jun 18, 2008
Loading...

Share This Page