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

J

James E Keenan

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
 
S

Steve Grazzini

James E Keenan said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top