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

后端 未结 9 859
说谎
说谎 2020-12-13 03:36

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 informati

相关标签:
9条回答
  • 2020-12-13 04:09

    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];

    0 讨论(0)
  • 2020-12-13 04:09

    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.

    0 讨论(0)
  • 2020-12-13 04:14

    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";
    
    0 讨论(0)
提交回复
热议问题