That "-M" is a perl file test operator, it will take the string after it as name of a file automatically.

Tor.

Paul Harwood wrote:

One question I have:

With this statement:

@files = sort { -M $a <=> -M $b } @files;

How does Perl understand that these are files and not just text entries?
Did using the readdir beforehand make this possible?

-----Original Message-----
From: Rob Dixon [mailto:[EMAIL PROTECTED] Posted At: Saturday, December 13, 2003 6:27 AM
Posted To: Perl
Conversation: Getting the most recent file
Subject: Re: Getting the most recent file


<[EMAIL PROTECTED]> wrote:


I am trying to write some code to read the most recent log file in a
directory. I wrote some code below. This works but I was wondering if
there was a more efficient method to do this. Ideally I would like to
include hours, minutes and seconds but that's not necessary at this
point.

foreach $iislog (@files) {

($WRITETIME) = (stat("$iislogs\\$iislog"))[9];

print scalar localtime ($WRITETIME);

($seconds, $minutes, $hours, $day, $month, $year, $wday, $yday,


$isdst) = localtime();


($seconds2, $minutes2, $hours2, $day2, $month2, $year2, $wday2,


$yday2, $isdst2) = localtime($WRITETIME);


if ($day == $day2 && $month == $month2) {

print "\n\n";
print "The file was last modified on: ";
print scalar localtime ($WRITETIME);
print "\n\n";
}
}



Hi Paul.


First of all,

 use strict;   # And declare all of your variables
 use warnings;

# And indent your code!

Then I'm not sure what you need. You say you want to read the most
recent log file,
but your code just prints out a list of modification times. Do you need
this
as well,or do you just want to find the latest file?

(stat $file)[9]

gives you the modification date, while

-M $file

gives you the age of the file. So you could just write

 @files = sort { -M $a <=> -M $b } @files;
 print $files[-1], "\n";

Or do you need anything more?

HTH,

Rob







--
<!---------------------------------------------
                          Victor
                          Development Engineer
                          Outblaze Ltd
---------------------------------------------->



--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>




Reply via email to