Gossamer Forum
Home : General : Perl Programming :

Perl stat function

Quote Reply
Perl stat function
Hi

I'm using the following code to return information about a given file in Perl.

Code:
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime,$ctime,$blksize,$blocks) = stat($_);

The information I'm trying to extract is the file size and the file modified date. The documentation tells me that:

Code:
7 size total size of file, in bytes
9 mtime last modify time in seconds since the epoch

(The epoch was at 00:00 January 1, 1970 GMT.)

Therefore a check on a random file gives $mtime the value of 981541679 which I presume is 981541679 seconds since epoch.

How can I convert Jan 01 1970 + 981541679 into a date string? I would like this to be outputted in the mysql format 2001-10-01.

Thanks

- wil
Quote Reply
Re: [Wil] Perl stat function In reply to
Edited.

You may want to use POSIX:

For example, using time():

use POSIX qw(strftime);
print strftime "%Y-%m-%d", localtime;

Last edited by:

RedRum: Jan 2, 2002, 6:07 AM
Quote Reply
Re: [RedRum] Perl stat function In reply to
In Reply To:
>>I presume is 981541679 seconds since epoch.<<

I doubt it seeing as time() is seconds since epoch and it currently stands at 1009979496 Cool

And where did I state that the directory in question was created today? My initial satement makes perfect sense.

- wil

Last edited by:

Wil: Jan 2, 2002, 6:06 AM
Quote Reply
Re: [Wil] Perl stat function In reply to
So it does. Anyway the code still works :)
Quote Reply
Re: [RedRum] Perl stat function In reply to
Hmm. Therefore I need to conver 01,01,1970 into a second string? And add the number of seconds to it, and then use your code to convert that string into a readable date.

I'll give it a try.

- wil
Quote Reply
Re: [Wil] Perl stat function In reply to
All you need is the seconds since epoch and do something like:

use POSIX qw(strftime);
print strftime "%Y-%m-%d", (localtime $mtime);
Quote Reply
Re: [RedRum] Perl stat function In reply to
How would I put the result of that into a variable?

- wil
Quote Reply
Re: [Wil] Perl stat function In reply to
Right. Finally found the answer. Thanks for your help. For future reference, and lurkers:

Code:
use strict;
use File::stat;
use POSIX qw(strftime);

my $file = '/etc/passwd';

my $s = stat($file) || die "Can't stat($file) $!\n";

my $modtime = strftime('%Y-%m-%d', localtime($s->mtime));

print "Modification time of $file is $modtime\n";

- wil

Last edited by:

Wil: Jan 2, 2002, 6:50 AM
Quote Reply
Re: [Wil] Perl stat function In reply to
Or simply:

my $modtime = localtime( (stat("/etc/passwd"))[9] );

Cheers,

Alex
--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Perl stat function In reply to
Yep, I guess that would ne neat.

But I am extracting the file size, too, so that would mean I needed 2 calls to stat() a given file? Surely this would slow it down a bit?

Cheers

- wil

Last edited by:

Wil: Jan 3, 2002, 1:21 AM
Quote Reply
Re: [Wil] Perl stat function In reply to
Well, if you need the other info, then you can use your original first line and then do:

my $modtime = localtime ($mtime);

Also, you can get the other info using _ which returns the info on the last stat'd file, i.e.:

if (-r $file and -w _ )

will return true if a file is readable and writeable, but only does one check. This is better then writing:

if (-r $file and -w $file )

So if you want the size, you can just add:

my $size = -s _;

Cheers,

Alex


--
Gossamer Threads Inc.
Quote Reply
Re: [Alex] Perl stat function In reply to
Very handy! Why can't I never find these tricks documented anywhere?!

Thanks

- wil