Jay Savage wrote:
> On 5/2/07, Rob Dixon <[EMAIL PROTECTED]> wrote:
>> Chris E. Rempola wrote:
>> >
>> > Could someone point me in the right direction to write out a simple
>> Perl
>> > script to check for old files in a particular directory that are older
>> > than 20 mins. Is there a module to grab current timestamp? Thanks.
>>
>> Check out
>>
>> perldoc -f -x
>>
>> and look at the -M option. It gives the age of the file in days in
>> floating point, so if its greater than 20/(24*60) your file is older than
>> twenty minutes.
>
> Not quite. -M reports "Script start time minus file modification time,
> in days." To put it another way, -M reports how old the file was when
> the script started running. Or more appropriately, how old the file
> would have been when the script started running, assuming its current
> mtime. That's not the same thing as how old the file is when the test
> is executed.
>
> For short-lived scripts, the difference is mainly a technicality For
> long-running programs, though, -M's behavior has serious consequences.
> -M on its own is useless in, say, a daemon that runs for days or
> months--if you're lucky--or even in a program that just takes a while
> to process all its data. The math to correct for running time is
> complicated by -M returning fractional days. To use -M effectively,
> you need to do something like:
>
> my $minutes = 20;
> if ( (-M "file") + ((time - $^T) / (24 * 60 * 60)) >
> $limit_minutes/(24*60) )
> { do something }
>
> Usually it's easier to just use the mtime from stat():
>
> if (time - (stat "file")[9] > $minutes * 60)
> { do something }
Or you could simply do:
$^T = time;
Before you do the file test which will put the current time into $^T which is
what -M uses to calculate the file time:
$ perl -le'
$file = shift;
print -M $file;
$^T = time() - 86400;
print -M $file;
' yourfile.txt
8.75521990740741
7.75521990740741
John
--
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order. -- Larry Wall
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/