How do I get a file's last modified time in Pe

2019-01-21 18:25发布

Suppose I have a filehandle $fh. I can check its existence with -e $fh or its file size with -s $fh or a slew of additional information about the file. How can I get its last modified time stamp?

标签: perl file-io
9条回答
相关推荐>>
2楼-- · 2019-01-21 19:01

This is very old thread, but I tried using the solution and could not get the information out of File::stat. (Perl 5.10.1)

I had to do the following:

my $f_stats = stat($fh);
my $timestamp_mod = localtime($f_stats->mtime);
print "MOD_TIME = $timestamp_mod \n";

Just thought I share in case anyone else had the same trouble.

查看更多
做自己的国王
3楼-- · 2019-01-21 19:07

I think you're looking for the stat function (perldoc -f stat)

In particular, the 9th field (10th, index #9) of the returned list is the last modify time of the file in seconds since the epoch.

So:

my $last_modified = (stat($fh))[9];

查看更多
何必那么认真
4楼-- · 2019-01-21 19:08

On my FreeBSD system, stat just returns a bless.

$VAR1 = bless( [
                 102,
                 8,
                 33188,
                 1,
                 0,
                 0,
                 661,
                 276,
                 1372816636,
                 1372755222,
                 1372755233,
                 32768,
                 8
               ], 'File::stat' );

You need to extract mtime like this:

my @ABC = (stat($my_file));

print "-----------$ABC['File::stat'][9] ------------------------\n";

or

print "-----------$ABC[0][9] ------------------------\n";
查看更多
登录 后发表回答